给定代表天数的整数N,任务是根据以下条件找到N天后的分数总和:
- 在第一个星期一,分数设置为1 。
- 在每个星期一,分数比上一个星期一的分数大1 。
- 每隔一天,分数变得比前一天的分数大1 。
例子:
Input: N=4
Output: 10
Explanation:
Scores on each day of the four days are as follows:
Monday: 1
Tuesday: 2
Wednesday: 3
Thursday: 4
Total sum of scores = 1 + 2 + 3 + 4 = 10
Input: N=8
Output: 30
Explanation:
Scores on each day of the 8 days are as follows:
Monday: 1
Tuesday: 2
Wednesday: 3
Thursday: 4
Friday: 5
Saturday: 6
Sunday: 7
Monday: 2
Total sum of scores = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 2 = 30
天真的方法:解决问题的最简单方法是遍历[1,N]范围,并继续每天添加分数。最后,打印获得的所有分数的总和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to c sum of calculate
// sum of scores after n days
void findScoreSum(int n)
{
// Store the required sum
int total = 0;
// Store the score on previous
// monday and current day respectively
int prev_monday = 0, curr_day = 0;
// Iterate over the range [1, n]
for (int day = 1; day <= n; day++) {
// If the current day is monday
if (day % 7 == 1) {
// Increment score of
// prev_monday by 1
prev_monday++;
// Update score of current day
curr_day = prev_monday;
}
// Add score of current day and
// increment score for next day
total += curr_day++;
}
// Print the result
cout << total;
}
// Driver Code
int main()
{
int N = 8;
findScoreSum(N);
return 0;
}
Java
// Java Program to implement
// the above approach
import java.io.*;
class GFG
{
// Function to c sum of calculate
// sum of scores after n days
static void findScoreSum(int n)
{
// Store the required sum
int total = 0;
// Store the score on previous
// monday and current day respectively
int prev_monday = 0, curr_day = 0;
// Iterate over the range [1, n]
for (int day = 1; day <= n; day++)
{
// If the current day is monday
if (day % 7 == 1)
{
// Increment score of
// prev_monday by 1
prev_monday++;
// Update score of current day
curr_day = prev_monday;
}
// Add score of current day and
// increment score for next day
total += curr_day++;
}
// Print the result
System.out.println(total);
}
// Driver Code
public static void main (String[] args)
{
int N = 8;
findScoreSum(N);
}
}
// This code is contributed by avanitrachhadiya2155
Python3
# Python3 program for the above approach
# Function to c sum of calculate
# sum of scores after n days
def findScoreSum(n):
# Store the required sum
total = 0
# Store the score on previous
# monday and current day respectively
prev_monday, curr_day = 0, 0
# Iterate over the range [1, n]
for day in range(1, n + 1):
# If the current day is monday
if (day % 7 == 1):
# Increment score of
# prev_monday by 1
prev_monday += 1
# Update score of current day
curr_day = prev_monday
# Add score of current day and
# increment score for next day
total += curr_day
curr_day += 1
# Print the result
print(total)
# Driver Code
if __name__ == '__main__':
N = 8
findScoreSum(N)
# This code is contributed by mohit kumar 29
C#
// C# Program to implement
// the above approach
using System;
class GFG
{
// Function to c sum of calculate
// sum of scores after n days
static void findScoreSum(int n)
{
// Store the required sum
int total = 0;
// Store the score on previous
// monday and current day respectively
int prev_monday = 0, curr_day = 0;
// Iterate over the range [1, n]
for (int day = 1; day <= n; day++) {
// If the current day is monday
if (day % 7 == 1) {
// Increment score of
// prev_monday by 1
prev_monday++;
// Update score of current day
curr_day = prev_monday;
}
// Add score of current day and
// increment score for next day
total += curr_day++;
}
// Print the result
Console.Write(total);
}
// Driver Code
public static void Main(String[] args)
{
int N = 8;
findScoreSum(N);
}
}
// This code is contributed by code_hunt.
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate sum
// of scores after n days
void findScoreSum(int n)
{
// Store the number
// of full weeks
int F = n / 7;
// Stores the remaining
// days in the last week
int D = n % 7;
// Store the sum of scores
// in the first F full weeks
int fullWeekScore
= (49 + 7 * F) * F / 2;
// Store the sum of scores
// in the last week
int lastNonFullWeekScore
= (2 * F + D + 1) * D / 2;
// Print the result
cout << fullWeekScore + lastNonFullWeekScore;
}
// Driver Code
int main()
{
int N = 8;
findScoreSum(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to calculate sum
// of scores after n days
static void findScoreSum(int n)
{
// Store the number
// of full weeks
int F = n / 7;
// Stores the remaining
// days in the last week
int D = n % 7;
// Store the sum of scores
// in the first F full weeks
int fullWeekScore = (49 + 7 * F) * F / 2;
// Store the sum of scores
// in the last week
int lastNonFullWeekScore = (2 * F + D + 1) * D / 2;
// Print the result
System.out.println(fullWeekScore
+ lastNonFullWeekScore);
}
// Driver Code
public static void main(String[] args)
{
int N = 8;
findScoreSum(N);
}
}
// This code is contributed by Kingash.
Python3
# Python3 program for the above approach
# Function to calculate sum
# of scores after n days
def findScoreSum(n):
# Store the number
# of full weeks
F = n // 7
# Stores the remaining
# days in the last week
D = n % 7
# Store the sum of scores
# in the first F full weeks
fullWeekScore = (49 + 7 * F) * F // 2
# Store the sum of scores
# in the last week
lastNonFullWeekScore = (2 * F + D + 1) * D // 2
# Print the result
print(fullWeekScore + lastNonFullWeekScore)
# Driver Code
if __name__ == '__main__':
N = 8
findScoreSum(N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG{
// Function to calculate sum
// of scores after n days
static void findScoreSum(int n)
{
// Store the number
// of full weeks
int F = n / 7;
// Stores the remaining
// days in the last week
int D = n % 7;
// Store the sum of scores
// in the first F full weeks
int fullWeekScore = (49 + 7 * F) * F / 2;
// Store the sum of scores
// in the last week
int lastNonFullWeekScore = (2 * F + D + 1) * D / 2;
// Print the result
Console.WriteLine(fullWeekScore +
lastNonFullWeekScore);
}
// Driver Code
static public void Main()
{
int N = 8;
findScoreSum(N);
}
}
// This code is contributed by rag2127
30
时间复杂度: O(N)
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
Let the number of full weeks be F and the remaining days be D.
Sum of scores after N days = Sum of scores in the first F weeks + Sum of scores in the remaining D days
Sum of scores in the first F weeks:
Sum of scores in the first week = 1 + 2 + 3 + … + 7
Sum of scores in the second week = 2 + 3 + 4 + … + 8 = 7*1 + (1 + 2 + 3 + … + 7)
Sum of scores in the third week = 3 + 4 + 5 + … + 9 = 7*2 + (1 + 2 + 3 + … + 7)
…
Sum of scores in Fth week = 7*(F – 1) + (1 + 2 + 3 + … + 7)
Total sum of scores in the first F weeks = F * (1 + 2 + 3 + … + 7) + 7(1 + 2 + … + (F – 1))
= F * ((7 * 8) / 2) + 7 * (F * (F – 1) / 2)
= F / 2 * (49 + 7 * F)
Sum of scores for the remaining D days:
Sum of scores in the remaining D days = (F + 1) + (F + 2) + … + (F + D)
= F * D + D * (D + 1) / 2
= D / 2 * (2 * F + D + 1)
因此,总得分为[F / 2 *(49 + 7 * F)] + [D / 2 *(2 * F + D + 1)] 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate sum
// of scores after n days
void findScoreSum(int n)
{
// Store the number
// of full weeks
int F = n / 7;
// Stores the remaining
// days in the last week
int D = n % 7;
// Store the sum of scores
// in the first F full weeks
int fullWeekScore
= (49 + 7 * F) * F / 2;
// Store the sum of scores
// in the last week
int lastNonFullWeekScore
= (2 * F + D + 1) * D / 2;
// Print the result
cout << fullWeekScore + lastNonFullWeekScore;
}
// Driver Code
int main()
{
int N = 8;
findScoreSum(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to calculate sum
// of scores after n days
static void findScoreSum(int n)
{
// Store the number
// of full weeks
int F = n / 7;
// Stores the remaining
// days in the last week
int D = n % 7;
// Store the sum of scores
// in the first F full weeks
int fullWeekScore = (49 + 7 * F) * F / 2;
// Store the sum of scores
// in the last week
int lastNonFullWeekScore = (2 * F + D + 1) * D / 2;
// Print the result
System.out.println(fullWeekScore
+ lastNonFullWeekScore);
}
// Driver Code
public static void main(String[] args)
{
int N = 8;
findScoreSum(N);
}
}
// This code is contributed by Kingash.
Python3
# Python3 program for the above approach
# Function to calculate sum
# of scores after n days
def findScoreSum(n):
# Store the number
# of full weeks
F = n // 7
# Stores the remaining
# days in the last week
D = n % 7
# Store the sum of scores
# in the first F full weeks
fullWeekScore = (49 + 7 * F) * F // 2
# Store the sum of scores
# in the last week
lastNonFullWeekScore = (2 * F + D + 1) * D // 2
# Print the result
print(fullWeekScore + lastNonFullWeekScore)
# Driver Code
if __name__ == '__main__':
N = 8
findScoreSum(N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG{
// Function to calculate sum
// of scores after n days
static void findScoreSum(int n)
{
// Store the number
// of full weeks
int F = n / 7;
// Stores the remaining
// days in the last week
int D = n % 7;
// Store the sum of scores
// in the first F full weeks
int fullWeekScore = (49 + 7 * F) * F / 2;
// Store the sum of scores
// in the last week
int lastNonFullWeekScore = (2 * F + D + 1) * D / 2;
// Print the result
Console.WriteLine(fullWeekScore +
lastNonFullWeekScore);
}
// Driver Code
static public void Main()
{
int N = 8;
findScoreSum(N);
}
}
// This code is contributed by rag2127
30
时间复杂度: O(1)
辅助空间: O(1)