给定一个数字N,任务是打印给定数字的和与乘之间的最大值,直到该数字减少到一个位数。 注: 数字的求和和和乘法,直到数字减少到一位数。
null
举个例子,N=19,
19分为1+9=10,然后10分为1+0=1。1是个位数的总和。 此外,19分为1*9=9。9是一位数的乘法。 因此,输出为9,即最大值为9和1。
Input: N = 631Output: 8Input: 110Output: 2
方法:
- 如果一个数字小于10,那么总和和乘积将是相同的。那么,把那个号码还给我。
- 其他的
- 重复使用查找数字之和 方法2 属于 求一个数的位数之和,直到和变成一位数 .
- 并且,找到重复使用的数字的乘积 方法1 属于 求一个数的位数之和,直到和变成一位数 .
- 返回两者的最大值。
以下是上述方法的实施情况:
C++
// CPP implementation of above approach #include<bits/stdc++.h> using namespace std; // Function to sum the digits until it // becomes a single digit long repeatedSum( long n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } // Function to product the digits until it // becomes a single digit long repeatedProduct( long n) { long prod = 1; // Loop to do sum while // sum is not less than // or equal to 9 while (n > 0 || prod > 9) { if (n == 0) { n = prod; prod = 1; } prod *= n % 10; n /= 10; } return prod; } // Function to find the maximum among // repeated sum and repeated product long maxSumProduct( long N) { if (N < 10) return N; return max(repeatedSum(N), repeatedProduct(N)); } // Driver code int main() { long n = 631; cout << maxSumProduct(n)<<endl; return 0; } // This code is contributed by mits |
JAVA
// Java implementation of above approach import java.util.*; import java.lang.*; import java.io.*; class GFG { // Function to sum the digits until it // becomes a single digit public static long repeatedSum( long n) { if (n == 0 ) return 0 ; return (n % 9 == 0 ) ? 9 : (n % 9 ); } // Function to product the digits until it // becomes a single digit public static long repeatedProduct( long n) { long prod = 1 ; // Loop to do sum while // sum is not less than // or equal to 9 while (n > 0 || prod > 9 ) { if (n == 0 ) { n = prod; prod = 1 ; } prod *= n % 10 ; n /= 10 ; } return prod; } // Function to find the maximum among // repeated sum and repeated product public static long maxSumProduct( long N) { if (N < 10 ) return N; return Math.max(repeatedSum(N), repeatedProduct(N)); } // Driver code public static void main(String[] args) { long n = 631 ; System.out.println(maxSumProduct(n)); } } |
Python3
# Python 3 implementation of above approach # Function to sum the digits until # it becomes a single digit def repeatedSum(n): if (n = = 0 ): return 0 return 9 if (n % 9 = = 0 ) else (n % 9 ) # Function to product the digits # until it becomes a single digit def repeatedProduct(n): prod = 1 # Loop to do sum while # sum is not less than # or equal to 9 while (n > 0 or prod > 9 ) : if (n = = 0 ) : n = prod prod = 1 prod * = n % 10 n / / = 10 return prod # Function to find the maximum among # repeated sum and repeated product def maxSumProduct(N): if (N < 10 ): return N return max (repeatedSum(N), repeatedProduct(N)) # Driver code if __name__ = = "__main__" : n = 631 print (maxSumProduct(n)) # This code is contributed # by ChitraNayal |
C#
// C# implementation of // above approach using System; class GFG { // Function to sum the digits // until it becomes a single digit public static long repeatedSum( long n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } // Function to product the digits // until it becomes a single digit public static long repeatedProduct( long n) { long prod = 1; // Loop to do sum while // sum is not less than // or equal to 9 while (n > 0 || prod > 9) { if (n == 0) { n = prod; prod = 1; } prod *= n % 10; n /= 10; } return prod; } // Function to find the maximum among // repeated sum and repeated product public static long maxSumProduct( long N) { if (N < 10) return N; return Math.Max(repeatedSum(N), repeatedProduct(N)); } // Driver code public static void Main() { long n = 631; Console.WriteLine(maxSumProduct(n)); } } // This code is contributed // by inder_verma |
Javascript
<script> // javascript implementation of above approach // Function to sum the digits until it // becomes a single digit function repeatedSum(n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } // Function to product the digits until it // becomes a single digit function repeatedProduct(n) { var prod = 1; // Loop to do sum while // sum is not less than // or equal to 9 while (n > 0 || prod > 9) { if (n == 0) { n = prod; prod = 1; } prod *= n % 10; n = parseInt(n/10); } return prod; } // Function to find the maximum among // repeated sum and repeated product function maxSumProduct(N) { if (N < 10) return N; return Math.max(repeatedSum(N), repeatedProduct(N)); } // Driver code var n = 631; document.write(maxSumProduct(n)); // This code contributed by aashish1995 </script> |
输出:
8
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END