📌  相关文章
📜  根据给定条件计算N天后的分数总和

📅  最后修改于: 2021-04-17 14:50:27             🧑  作者: Mango

给定代表天数的整数N,任务是根据以下条件找到N天后的分数总和:

  • 在第一个星期一,分数设置为1
  • 在每个星期一,分数比上一个星期一的分数大1
  • 每隔一天,分数变得比前一天的分数大1

例子:

天真的方法:解决问题的最简单方法是遍历[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)

高效的方法:可以基于以下观察来优化上述方法:

因此,总得分为[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)