n^x 中数字的递归和,其中 n 和 x 在 C++ 中非常大

我们给出了正整数变量作为“num”和“x”。任务是递归计算 num ^ x 然后将结果数字的数字相加,直到没有达到个位数,并且结果个位数将作为输出。

让我们看看这个的各种输入输出场景 -

输入 - int num = 2345,int x = 3

输出 - n^x 中数字的递归和,其中 n 和 x 非常大:8

说明 - 我们给定正整数值作为 num 和 x,值为 2345,幂为 3。首先,计算 2345 ^ 3,即 12,895,213,625。现在,我们将这些数字相加,即 1 + 2 + 8 + 9 + 5 + 2 + 1 + 3 + 6 + 2 + 5 即 44。现在我们将添加 4 + 4 即 8。由于我们已经实现了个位数,因此,输出为 8。

输入 - int num = 3,int x = 3

输出 - n^x 中数字的递归总和,其中 n 和 x 非常大: 9

解释 - 我们给定正整数值作为 num 和 x,值为 3,幂为 3。首先,计算 3 ^ 3 即 9。由于我们已经实现了个位数,因此输出为 9,不需要进一步计算。

下面程序中使用的方法如下

  • 输入一个整数变量作为 num 和 x 并将数据传递给函数Recursive_Digit(num, x)进行进一步处理。

  • 函数内部 Recursive_Digit(num, x)

    • 将变量“total”声明为 long 并将其设置为调用函数total_digits(num),该函数将返回作为参数传递的数字的数字总和。

    • 将变量声明为 long 类型的 temp 并将其设置为 power % 6

    • 检查 IF total = 3 OR total = 6 AND power > 1 然后返回 9。

    • ELSE IF,power = 1 然后返回总数。

    • 否则,如果,power = 0,则返回 1。

    • ELSE IF, temp - 0 然后返回调用 total_digits((long)pow(total, 6))

    • 否则,返回 total_digits((long) pow(total, temp))。

  • 函数内部长 total_digits(long num)

    • 检查 IF num = 0 然后返回 0。检查 IF, num % 9 = 0 然后返回 9。

    • 否则,返回 num % 9

示例

#include <bits/stdc++.h>

using namespace std;

long total_digits(long num){

   if(num == 0){

      return 0;

   }

   if(num % 9 == 0){

      return 9;

   }

   else{

      return num % 9;

   }

}

long Recursive_Digit(long num, long power){

   long total = total_digits(num);

   long temp = power % 6;

   if((total == 3 || total == 6) & power > 1){

      return 9;

   }

   else if (power == 1){

      return total;

   }

   else if (power == 0){

      return 1;

   }

   else if (temp == 0){

      return total_digits((long)pow(total, 6));

   }

   else{

      return total_digits((long)pow(total, temp));

   }

}

int main(){

   int num = 2345;

   int x = 98754;

   cout<<"Recursive sum of digit in n^x, where n and x are very large are: "<<Recursive_Digit(num, x);

   return 0;

}

输出结果

如果我们运行上面的代码,它将生成以下输出

Recursive sum of digit in n^x, where n and x are very large are: 1

以上是 n^x 中数字的递归和,其中 n 和 x 在 C++ 中非常大 的全部内容, 来源链接: utcz.com/z/322610.html

回到顶部