📌  相关文章
📜  通过将范围[a,b]和[b,c]中的任何两个数字相加而获得范围[1,b + c]中的每个数字的方法数

📅  最后修改于: 2021-05-06 17:46:27             🧑  作者: Mango

给定三个整数abc 。您需要从[a,b]范围中选择一个整数,并从[b,c]范围中选择一个整数并将它们相加。计算范围为[1,b + c]内所有数字之和的方法。

例子:

简单方法:

  • 一个简单的暴力解决方案是使用嵌套循环,其中外部循环从i = a遍历到i = b,内部循环从j = b遍历到j = c。
  • 我们将用零初始化大小为b + c + 1的数组a。现在循环执行,我们将在i + j处增加索引,即(a [i + j] ++)
  • 我们将只在最后打印数组。

下面是上述方法的实现。

C++
// C++ program to calculate
// the number of ways
 
#include 
using namespace std;
 
void CountWays(int a, int b, int c)
{
    int x = b + c + 1;
    int arr[x] = { 0 };
 
    // Initialising the array with zeros.
    // You can do using memset too.
    for (int i = a; i <= b; i++) {
        for (int j = b; j <= c; j++) {
            arr[i + j]++;
        }
    }
    // Printing the array
    for (int i = 1; i < x; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}
// Driver code
int main()
{
    int a = 1;
    int b = 2;
    int c = 2;
 
    CountWays(a, b, c);
 
    return 0;
}


Java
// Java program to calculate
// the number of ways
class GFG{
     
public static void CountWays(int a, int b,
                                    int c)
{
    int x = b + c + 1;
    int[] arr = new int[x];
     
    // Initialising the array with zeros.
    // You can do using memset too.
    for(int i = a; i <= b; i++)
    {
       for(int j = b; j <= c; j++)
       {
          arr[i + j]++;
       }
    }
     
    // Printing the array
    for(int i = 1; i < x; i++)
    {
       System.out.print(arr[i] + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int a = 1;
    int b = 2;
    int c = 2;
     
    CountWays(a, b, c);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to calculate
# the number of ways
def CountWays(a, b, c):
     
    x = b + c + 1;
    arr = [0] * x;
 
    # Initialising the array with zeros.
    # You can do using memset too.
    for i in range(a, b + 1):
        for j in range(b, c + 1):
            arr[i + j] += 1;
 
    # Printing the array
    for i in range(1, x):
        print(arr[i], end = " ");
         
# Driver code
if __name__ == '__main__':
     
    a = 1;
    b = 2;
    c = 2;
 
    CountWays(a, b, c);
     
# This code is contributed by Rajput-Ji


C#
// C# program to calculate
// the number of ways
using System;
class GFG{
     
public static void CountWays(int a, int b,
                                    int c)
{
    int x = b + c + 1;
    int[] arr = new int[x];
     
    // Initialising the array with zeros.
    // You can do using memset too.
    for(int i = a; i <= b; i++)
    {
        for(int j = b; j <= c; j++)
        {
            arr[i + j]++;
        }
    }
     
    // Printing the array
    for(int i = 1; i < x; i++)
    {
        Console.Write(arr[i] + " ");
    }
}
 
// Driver code
public static void Main()
{
    int a = 1;
    int b = 2;
    int c = 2;
     
    CountWays(a, b, c);
}
}
 
// This code is contributed by rutvik_56


Javascript


C++
// C++ program to calculate
// the number of ways
 
#include 
using namespace std;
 
void CountWays(int a, int b, int c)
{
    // 2 is added because sometimes
    // we will decrease the
    // value out of bounds.
    int x = b + c + 2;
 
    // Initialising the array with zeros.
    // You can do using memset too.
    int arr[x] = { 0 };
 
    for (int i = 1; i <= b; i++) {
        arr[i + b]++;
        arr[i + c + 1]--;
    }
 
    // Printing the array
    for (int i = 1; i < x - 1; i++) {
        arr[i] += arr[i - 1];
        cout << arr[i] << " ";
    }
    cout << endl;
}
 
// Driver code
int main()
{
    int a = 1;
    int b = 2;
    int c = 2;
 
    CountWays(a, b, c);
 
    return 0;
}


Java
// Java program to calculate
// the number of ways
import java.util.*;
 
class GFG{
 
static void CountWays(int a, int b, int c)
{
     
    // 2 is added because sometimes
    // we will decrease the
    // value out of bounds.
    int x = b + c + 2;
 
    // Initialising the array with zeros.
    int arr[] = new int[x];
 
    for(int i = 1; i <= b; i++)
    {
       arr[i + b]++;
       arr[i + c + 1]--;
    }
 
    // Printing the array
    for(int i = 1; i < x - 1; i++)
    {
       arr[i] += arr[i - 1];
       System.out.print(arr[i] + " ");
    }
    System.out.println();
}
 
// Driver code
public static void main(String[] args)
{
    int a = 1;
    int b = 2;
    int c = 2;
 
    CountWays(a, b, c);
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 program to calculate
# the number of ways
def CountWays(a, b, c):
      
    # 2 is added because sometimes
    # we will decrease the
    # value out of bounds.
    x = b + c + 2;
  
    # Initialising the array with zeros.
    arr = [0] * x;
  
    for i in range(1, b+1):
       arr[i + b] = arr[i + b] + 1;
       arr[i + c + 1] = arr[i + c + 1] -1;
     
  
    # Printing the array
    for i in range(1, x-1):
     
       arr[i] += arr[i - 1];
       print(arr[i], end = " ");
 
  
# Driver code
if __name__ == '__main__':
      
    a = 1;
    b = 2;
    c = 2;
  
    CountWays(a, b, c);
      
# This code is contributed by rock_cool


C#
// C# program to calculate
// the number of ways
using System;
class GFG{
 
static void CountWays(int a, int b, int c)
{
     
    // 2 is added because sometimes
    // we will decrease the
    // value out of bounds.
    int x = b + c + 2;
 
    // Initialising the array with zeros.
    int []arr = new int[x];
 
    for(int i = 1; i <= b; i++)
    {
        arr[i + b]++;
        arr[i + c + 1]--;
    }
 
    // Printing the array
    for(int i = 1; i < x - 1; i++)
    {
        arr[i] += arr[i - 1];
        Console.Write(arr[i] + " ");
    }
    Console.WriteLine();
}
 
// Driver code
public static void Main()
{
    int a = 1;
    int b = 2;
    int c = 2;
 
    CountWays(a, b, c);
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
0 0 1 1

时间复杂度: O((ba)*(cb)),在最坏的情况下为O(c 2 )

高效方法:想法是使用Prefix Sum逻辑来解决此问题。

  1. 我们将从[a,b]遍历i,对于每个i,我们将简单地将开始间隔arr [i + b]的值增加1,并将结束间隔arr [i + c + 1]的值减少1。
  2. 现在我们要做的就是计算数组的前缀和(arr [i] + = arr [i-1])并打印该数组。

让我们在一个示例的帮助下看一下该方法。
为什么这样做?

下面是上述方法的实现。

C++

// C++ program to calculate
// the number of ways
 
#include 
using namespace std;
 
void CountWays(int a, int b, int c)
{
    // 2 is added because sometimes
    // we will decrease the
    // value out of bounds.
    int x = b + c + 2;
 
    // Initialising the array with zeros.
    // You can do using memset too.
    int arr[x] = { 0 };
 
    for (int i = 1; i <= b; i++) {
        arr[i + b]++;
        arr[i + c + 1]--;
    }
 
    // Printing the array
    for (int i = 1; i < x - 1; i++) {
        arr[i] += arr[i - 1];
        cout << arr[i] << " ";
    }
    cout << endl;
}
 
// Driver code
int main()
{
    int a = 1;
    int b = 2;
    int c = 2;
 
    CountWays(a, b, c);
 
    return 0;
}

Java

// Java program to calculate
// the number of ways
import java.util.*;
 
class GFG{
 
static void CountWays(int a, int b, int c)
{
     
    // 2 is added because sometimes
    // we will decrease the
    // value out of bounds.
    int x = b + c + 2;
 
    // Initialising the array with zeros.
    int arr[] = new int[x];
 
    for(int i = 1; i <= b; i++)
    {
       arr[i + b]++;
       arr[i + c + 1]--;
    }
 
    // Printing the array
    for(int i = 1; i < x - 1; i++)
    {
       arr[i] += arr[i - 1];
       System.out.print(arr[i] + " ");
    }
    System.out.println();
}
 
// Driver code
public static void main(String[] args)
{
    int a = 1;
    int b = 2;
    int c = 2;
 
    CountWays(a, b, c);
}
}
 
// This code is contributed by Rohit_ranjan

Python3

# Python3 program to calculate
# the number of ways
def CountWays(a, b, c):
      
    # 2 is added because sometimes
    # we will decrease the
    # value out of bounds.
    x = b + c + 2;
  
    # Initialising the array with zeros.
    arr = [0] * x;
  
    for i in range(1, b+1):
       arr[i + b] = arr[i + b] + 1;
       arr[i + c + 1] = arr[i + c + 1] -1;
     
  
    # Printing the array
    for i in range(1, x-1):
     
       arr[i] += arr[i - 1];
       print(arr[i], end = " ");
 
  
# Driver code
if __name__ == '__main__':
      
    a = 1;
    b = 2;
    c = 2;
  
    CountWays(a, b, c);
      
# This code is contributed by rock_cool

C#

// C# program to calculate
// the number of ways
using System;
class GFG{
 
static void CountWays(int a, int b, int c)
{
     
    // 2 is added because sometimes
    // we will decrease the
    // value out of bounds.
    int x = b + c + 2;
 
    // Initialising the array with zeros.
    int []arr = new int[x];
 
    for(int i = 1; i <= b; i++)
    {
        arr[i + b]++;
        arr[i + c + 1]--;
    }
 
    // Printing the array
    for(int i = 1; i < x - 1; i++)
    {
        arr[i] += arr[i - 1];
        Console.Write(arr[i] + " ");
    }
    Console.WriteLine();
}
 
// Driver code
public static void Main()
{
    int a = 1;
    int b = 2;
    int c = 2;
 
    CountWays(a, b, c);
}
}
 
// This code is contributed by Code_Mech

Java脚本


输出:
0 0 1 1

时间复杂度: O(C)