程序显示不需要的输出。

huangapple go评论60阅读模式
英文:

program showing unwanted output

问题

我明白你的要求,以下是你提供的文本的翻译:

问题陈述如下:我正在研究的问题是这样的
一天,爱丽丝正在尝试使用数字来创建新的算法。他引入了一个新术语Rsum。任何数字的Rsum定义为对给定数字的数字进行迭代求和,直到得到单个数字为止。
例如:

365(3 + 6 + 5)= 14
14 1 + 4 = 5
Rsum(365)= 5

鲍勃在爱丽丝不在的时候进入了爱丽丝的房间,并将爱丽丝正在尝试的所有数字更改为它们对应的阶乘。例如,鲍勃将3更改为6(3!= 321)。考虑0!= 0。
当爱丽丝开始实验时,他不知道数字已经改变了。他得到了一些模糊的结果,无法找到算法的某些模式。因此,他需要你的帮助。你被给定一个实际数字范围[A,B],你的任务是找到新改变的数字范围[A,B]中所有Rsum值的总和。
因此,输入将包括范围
输出应为Rsum

例如:
如果我们输入1,3,则输出应为9,即1!+2!+3!
这是1 + 2 + 6 = 9
或者当输入为3,4时,输出应为12,因为6 + (2 + 4) = 12
这是我的代码

#include <stdio.h>
int main(){
    int num,range1,range2;
    printf("输入数字范围:");
    int rsum=0,total=0;
    scanf("%d %d",&range1,&range2);

    for(int j=range1;j<=range2;j++)
    {  
        int sum=0;
        int factorial=1;

        for(int k=1;k<=j;k++)
        {
            factorial=factorial*k;
        }
        if (factorial > 10 )
        {
            while(factorial>0)    
            {    
                int m=factorial%10;    
                sum=sum+m;    
                factorial=factorial/10;    
            } 
            rsum=rsum+sum;
        }
        else
        {
            rsum=rsum+factorial;
        }
    }

    printf("%d\n",rsum);
}

这段代码对于小范围的数字工作得很好,但是对于大范围,如41到49,输出显示为0。有人可以帮助我吗?

英文:

the problem statement on which i am working is as follows
One day Alice was experimenting with the numbers to make new algorithms. He introduce a new term Rsum. Rsum of any number is defined as number obtained by iterative summing of digits of the given number until single digit number is obtained.
For example:

    365 (3+6+5) = 14
     14 1+4 = 5
    Rsum(365)=5

Bob came in the room of Alice in his absence and change all the numbers on which Alice was experimenting by their corresponding factorials. For example Bob will change 3 with 6 (3!=321). Consider 0!=0.
When Alice start experimenting without knowing that the numbers are now changed. He got some ambiguous results and unable to find some pattern for his algorithm. So he wants your help. You are given a range [A,B] of actual numbers and your task is to find sum of all Rsum values in range [A,B] of new changed numbers.
So the input will consist of the range
and output should be Rsum

       for example:
       if we input 1,3 the output should be 9 that is 1!+2!+3! 
       which is 1+2+6=9
       or when the input is 3,4 the output should be 12 since, 6+(2+4)=12

this is my code

        #include &lt;stdio.h&gt;
        int main(){
   int num,range1,range2;
        printf(&quot;enter the range of the number:&quot;);
    int rsum=0,total=0;
	scanf(&quot;%d %d&quot;,&amp;range1,&amp;range2);

	for(int j=range1;j&lt;=range2;j++)

	{  
	    int sum=0;
		int factorial=1;
		
		for(int k=1;k&lt;=j;k++)
		{
			factorial=factorial*k;
		   
		}
		if (factorial &gt;10 )
		{
			while(factorial&gt;0)    
             {    
              int m=factorial%10;    
             sum=sum+m;    
              factorial=factorial/10;    
                } 
                rsum=rsum+sum;
		}
		else
		{
			rsum=rsum+factorial;
		 
		}
	}

		printf(&quot;%d\n&quot;,rsum);
         }    

the code is working fine for small numbers however in case of large range such as 41,49 the output is shown to be 0 can anyone help me with it?

答案1

得分: 2

你可以同时计算阶乘并求和其各位数字。重复将数字相加,直到只剩一个数字,等同于取模9。 取模M,如果在计算结束时应用或在每一步应用都会得到相同的结果。在每一步之后应用它可以使结果舒适地适应int

所以:

int facsum(int n) {
    if (n == 0) return 0; /* 根据指示 */
    int res = 1;
    for (int i = 2; i<=n; i++)
        res  = (res * n) % 9;
    if (res == 0) /* 如果和为9,%9将得到0,我们想要得到9 */
        res = 9;
    return res;
}
英文:

You can compute the factorial and sum its digits at the same time. Repeated summing of digits until one is left is equivalent to mod 9.. Modulo M gives the same result if applied at the end of the calculation or if applied at every step. Applying it after every step makes the result fit into int comfortably.

So:

int facsum(int n) {
    if (n == 0) return 0; /* as per instructions */
    int res = 1;
    for (int i = 2; i&lt;=n; i++)
        res  = (res * n) % 9;
    if (res == 0) /* if the sum was 9, %9 would give 0, we want 9 */
        res = 9;
    return res;
}

huangapple
  • 本文由 发表于 2023年3月7日 18:48:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定