📜  根据给定条件计算 N 天后放在盒子里的钱

📅  最后修改于: 2022-05-13 01:56:07.619000             🧑  作者: Mango

根据给定条件计算 N 天后放在盒子里的钱

给定 7 个空盒子b1、b2、b3、b4、b5、b6、b7和一个整数N ,任务是根据以下条件找出N天后可以放入盒子中的总金额:

  1. 每天,钱只能以循环方式放入一个盒子b1、b2、b3、b4、b5、b6、b7、b1、b2、......等等。
  2. 在方框b1中,比方框b1中已经存在的钱多放1
  3. 在除b1之外的每个盒子里,比前一个盒子里的钱多放1

例子:

方法:按照以下步骤解决问题

  1. i天花的钱是((i – 1)/ 7) + ((i – 1) % 7 + 1),其中i[1, N]范围内
  2. 模拟相同的天数[1, N]
  3. 打印总成本。

下面是上述方法的实现:

C++
// C++ program for
// the above approach
#include 
using namespace std;
 
// Function to find the total money
// placed in boxes after N days
int totalMoney(int N)
{
     
    // Stores the total money
    int ans = 0;
 
    // Iterate for N days
    for(int i = 0; i < N; i++)
    {
         
        // Adding the Week number
        ans += i / 7;
 
        // Adding previous amount + 1
        ans += (i % 7 + 1);
    }
 
    // Return the total amount
    return ans;
}
 
// Driver code
int main()
{  
     
    // Input
    int N = 15;
 
    // Function call to find
    // total money placed
    cout << totalMoney(N);
}
 
// This code is contributed khushboogoyal499


Java
// Java program for
// the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to find the total money
    // placed in boxes after N days
    public static int totalMoney(int N)
    {
 
        // Stores the total money
        int ans = 0;
 
        // Iterate for N days
        for (int i = 0; i < N; i++) {
 
            // Adding the Week number
            ans += i / 7;
 
            // Adding previous amount + 1
            ans += (i % 7 + 1);
        }
 
        // Return the total amount
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Input
        int N = 15;
 
        // Function call to find
        // total money placed
        System.out.println(
            totalMoney(N));
    }
}


Python
# Python program for
# the above approach
 
# Function to find the total money
# placed in boxes after N days
def totalMoney(N):
     
    # Stores the total money
    ans = 0
 
    # Iterate for N days
    for i in range(0, N):
      
        # Adding the Week number
        ans += i / 7
 
        # Adding previous amount + 1
        ans += (i % 7 + 1)
      
    # Return the total amount
    return ans
  
# Driver code
 
# Input
N = 15
 
# Function call to find
# total money placed
print(totalMoney(N))
 
# This code is contributed by shivanisinghss2110


C#
// C# program for
// the above approach
using System;
 
class GFG{
     
// Function to find the total money
// placed in boxes after N days
public static int totalMoney(int N)
{
     
    // Stores the total money
    int ans = 0;
 
    // Iterate for N days
    for(int i = 0; i < N; i++)
    {
         
        // Adding the Week number
        ans += i / 7;
 
        // Adding previous amount + 1
        ans += (i % 7 + 1);
    }
 
    // Return the total amount
    return ans;
}
 
// Driver code
static public void Main()
{
     
    // Input
    int N = 15;
 
    // Function call to find
    // total money placed
    Console.WriteLine(totalMoney(N));
}
}
 
// This code is contributed by offbeat


Javascript


C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find total
// money placed in the box
int totalMoney(int N)
{
    // Number of complete weeks
    int CompWeeks = N / 7;
 
    // Remaining days in
    // the last week
    int RemDays = N % 7;
 
    int X = 28 * CompWeeks
            + 7 * (CompWeeks
                   * (CompWeeks - 1) / 2);
 
    int Y = RemDays
                * (RemDays + 1) / 2
            + CompWeeks * RemDays;
 
    int cost = X + Y;
 
    cout << cost << '\n';
}
 
// Driver Code
int main()
{
    // Input
    int N = 15;
 
    // Function call to find
    // the total money placed
    totalMoney(N);
 
    return 0;
}


Java
// Java program for above approach
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
// Function to find total
// money placed in the box
static void totalMoney(int N)
{
    // Number of complete weeks
    int CompWeeks = N / 7;
   
    // Remaining days in
    // the last week
    int RemDays = N % 7;
   
    int X = 28 * CompWeeks
            + 7 * (CompWeeks
                   * (CompWeeks - 1) / 2);
   
    int Y = RemDays
                * (RemDays + 1) / 2
            + CompWeeks * RemDays;
   
    int cost = X + Y;
   
    System.out.print(cost);
}
 
 
    // Driver Code
    public static void main(String[] args)
    {
    // Input
    int N = 15;
   
    // Function call to find
    // the total money placed
    totalMoney(N);
    }
}
 
// This code is contributed by souravghosh0416.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find total
// money placed in the box
static void totalMoney(int N)
{
     
    // Number of complete weeks
    int CompWeeks = N / 7;
    
    // Remaining days in
    // the last week
    int RemDays = N % 7;
    
    int X = 28 * CompWeeks + 7 *
    (CompWeeks * (CompWeeks - 1) / 2);
    
    int Y = RemDays * (RemDays + 1) / 2 +
          CompWeeks * RemDays;
    
    int cost = X + Y;
    
    Console.WriteLine(cost);
}   
 
// Driver Code
public static void Main()
{
     
    // Input
    int N = 15;
    
    // Function call to find
    // the total money placed
    totalMoney(N);
}
}
 
// This code is contriobuted by sanjoy_62


Javascript


输出:
66

时间复杂度: O(N)
辅助空间: O(1)

高效方法:上述方法可以通过找出已完成的周数上周剩余的天数来优化。

请按照以下步骤解决问题:

  1. 初始化变量XY ,分别存储可以在完整周和部分周中放置的金额。
  2. 每周的钱可以这样计算:
    • 第一周:1 2 3 4 5 6 7 = 28 + (7 x 0)
    • 第 2:2 3 4 5 6 7 8 = 28 + (7 x 1)
    • 第三周:3 4 5 6 7 8 9 = 28 + (7 x 2)
    • 第 4:4 5 6 7 8 9 10 = 28 + (7 x 3) 等等。
  3. 因此,更新:
    X = 28 + 7 x(完成周数 - 1)
    Y =剩余天数总和 +(完整周数 * 上周剩余天数)
  4. 因此,总量等于X + Y 。打印放置的总金额

下面是上述方法的实现:

C++

// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find total
// money placed in the box
int totalMoney(int N)
{
    // Number of complete weeks
    int CompWeeks = N / 7;
 
    // Remaining days in
    // the last week
    int RemDays = N % 7;
 
    int X = 28 * CompWeeks
            + 7 * (CompWeeks
                   * (CompWeeks - 1) / 2);
 
    int Y = RemDays
                * (RemDays + 1) / 2
            + CompWeeks * RemDays;
 
    int cost = X + Y;
 
    cout << cost << '\n';
}
 
// Driver Code
int main()
{
    // Input
    int N = 15;
 
    // Function call to find
    // the total money placed
    totalMoney(N);
 
    return 0;
}

Java

// Java program for above approach
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
// Function to find total
// money placed in the box
static void totalMoney(int N)
{
    // Number of complete weeks
    int CompWeeks = N / 7;
   
    // Remaining days in
    // the last week
    int RemDays = N % 7;
   
    int X = 28 * CompWeeks
            + 7 * (CompWeeks
                   * (CompWeeks - 1) / 2);
   
    int Y = RemDays
                * (RemDays + 1) / 2
            + CompWeeks * RemDays;
   
    int cost = X + Y;
   
    System.out.print(cost);
}
 
 
    // Driver Code
    public static void main(String[] args)
    {
    // Input
    int N = 15;
   
    // Function call to find
    // the total money placed
    totalMoney(N);
    }
}
 
// This code is contributed by souravghosh0416.

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to find total
// money placed in the box
static void totalMoney(int N)
{
     
    // Number of complete weeks
    int CompWeeks = N / 7;
    
    // Remaining days in
    // the last week
    int RemDays = N % 7;
    
    int X = 28 * CompWeeks + 7 *
    (CompWeeks * (CompWeeks - 1) / 2);
    
    int Y = RemDays * (RemDays + 1) / 2 +
          CompWeeks * RemDays;
    
    int cost = X + Y;
    
    Console.WriteLine(cost);
}   
 
// Driver Code
public static void Main()
{
     
    // Input
    int N = 15;
    
    // Function call to find
    // the total money placed
    totalMoney(N);
}
}
 
// This code is contriobuted by sanjoy_62

Javascript


输出:
66

时间复杂度: O(1)
辅助空间: O(1)