给定一个表示百分比变化的数组Arr。任务是确定这些百分比更改后的百分比增加。
例子:
Input: arr[] = {10, 20, 30, 10}
Output: Percentage change is = 88.76 %
Input: arr[] = {20, 15, 9, 7}
Output: Percentage change is = 60.94 %
无需连续更改的解决方案:-
let us take a number N = 120.
and percentage changes are given as, arr[] = {10, 20, 30, 10}
Now, if we first increase 120 by 10% i.e 120 * 1.1 We get N = 132
again if we increase it by 20% i.e 132 * 1.2 We get N = 158.4
again if we increase it by 30% i.e 158.4 * 1.3 We get N = 205.92
and lastly if we further increase it by 10% i.e 205.92 * 1.1 We get N = 226.51
Now, Percentage change = (226.51 – 120) / 120 = 0.8876
percentage change = 0.8876 * 100 = 88.76 %
这个公式如何运作?
令x为初始值。 A%更改后,x的值变为(x + x * A / 100)
在连续的B%变化之后,x的值变为(x + x * A / 100)+(x + x * A / 100)* B / 100。因此,x的值的增量为x *(A + B + A * B / 100)/ 100。就百分比而言,我们可以说该值增加了(A + B + A * B / 100)%
方法:-
- 只需在arr [0]和arr [1]之间应用连续更改公式,然后将结果存储在结果变量中
- 现在,使用上述公式计算result和arr [2]之间的连续变化,并将结果存储在result变量中,依此类推。
下面是上述方法的实现:
C++
// C++ implementation of above approach #include
using namespace std; float successiveChange(int arr[], int N) { float var1, var2, result = 0; var1 = arr[0]; var2 = arr[1]; // Calculate successive change of 1st 2 change result = var1 + var2 + (float(var1 * var2) / 100); // Calculate successive change // for rest of the value for (int i = 2; i < N; i++) result = result + arr[i] + (float(result * arr[i]) / 100); return result; } // Driver code int main() { int arr[] = {10, 20, 30, 10}; int N = sizeof(arr) / sizeof(arr[0]); // Calling function float result = successiveChange(arr, N); cout << "Percentage change is = " << result << " %"; return 0; }
Java
// Java implementation of above approach import java.io.*; class GFG { static float successiveChange(int arr[], int N) { float var1, var2, result = 0; var1 = arr[0]; var2 = arr[1]; // Calculate successive change of 1st 2 change result = var1 + var2 + ((var1 * var2) / 100); // Calculate successive change // for rest of the value for (int i = 2; i < N; i++) result = result + arr[i] + ((result * arr[i]) / 100); return result; } // Driver code public static void main (String[] args) { int []arr = {10, 20, 30, 10}; int N = arr.length; // Calling function float result = successiveChange(arr, N); System.out.println("Percentage change is = " + result + " %"); } } // This code is contributed by shs..
Python 3
# Python implementation of above approach def successiveChange(arr, N): result = 0; var1 = arr[0]; var2 = arr[1]; # Calculate successive change of 1st 2 change result = float(var1 + var2 + (float(var1 * var2) / 100)); # Calculate successive change # for rest of the value for i in range(2, N): result = (result + arr[i] + (float(result * arr[i]) / 100)); return result; # Driver code arr = [10, 20, 30, 10]; N = len(arr) ; # Calling function result = successiveChange(arr, N); print ("Percentage change is = %.2f" % (result), "%"); # This code is contributed # by Shivi_Aggarwal
C#
// C# implementation of above approach using System; class GFG { static float successiveChange(int []arr, int N) { float var1, var2, result = 0; var1 = arr[0]; var2 = arr[1]; // Calculate successive change of 1st 2 change result = var1 + var2 + ((var1 * var2) / 100); // Calculate successive change // for rest of the value for (int i = 2; i < N; i++) result = result + arr[i] + ((result * arr[i]) / 100); return result; } // Driver code public static void Main () { int []arr = {10, 20, 30, 10}; int N = arr.Length; // Calling function float result = successiveChange(arr, N); Console.WriteLine("Percentage change is = " + result + " %"); } } // This code is contributed by shs..
PHP
输出:Percentage change is = 88.76 %