我得到的5个整数必须不小于10且不大于100。那么,如果我们把它们除以10,我必须找到它们的除法余数。然后,我必须找到我找到的余数之和,最后五个余数中的哪一个是最大值。
在这里,我给你我写的代码,但我不知道如何进一步。也许有“for”,但我不知道具体是怎么回事。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c, d, e, a10,b10,c10,d10,e10, sum, max;
printf("give 5 integers \n");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
a10 = a % 10;
b10 = b % 10;
c10 = c % 10;
d10 = d % 10;
e10 = e % 10;
printf("division remainder is: %d %d %d %d %d\n",
a10,b10,c10,d10,e10);
sum = a10 + b10 + c10 + d10 + e10;
printf("the sum of the remains is: %d\n", sum);
system("pause");
}
最佳答案
下面是使用“for”和int数组的代码。若要使用“for”,必须使用数组。
#include<stdio.h>
#include <stdlib.h>
int main()
{
int i;
int max;
int sum=0;
int aryNum[5];
int aryRem[5];
printf("give 5 integers \n");
for(i=0;i<5;i++)
{
scanf("%d",&aryNum[i]);
}
printf("division remainder is: ");
for(i=0;i<5;i++)
{
aryRem[i]=aryNum[i]%10;
printf("%d ",aryRem[i]);
}
printf("\n");
max=aryRem[0];
sum = max;
for(i=1;i<5;i++)
{
if(max<aryRem[i])
{
max=aryRem[i];
}
sum+=aryRem[i];
}
printf("the sum of the remains is: %d\n", sum);
printf("maximum remains is: %d\n", max);
system("pause");
return 0;
}
如果不想使用“for”和int数组,则可以使用以下代码。但不可取的是:
#include <stdio.h>
#include <stdlib.h>
int greater(int a, int b)
{
return (a>b)? a:b;
}
int main()
{
int a, b, c, d, e, a10,b10,c10,d10,e10, sum, max;
printf("give 5 integers \n");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
a10 = a % 10;
b10 = b % 10;
c10 = c % 10;
d10 = d % 10;
e10 = e % 10;
printf("division remainder is: %d %d %d %d %d\n",
a10,b10,c10,d10,e10);
sum = a10 + b10 + c10 + d10 + e10;
printf("the sum of the remains is: %d\n", sum);
/*New added code*/
max=greater(a10,b10);
max=greater(max,c10);
max=greater(max,d10);
max=greater(max,e10);
printf("maximum remains is: %d\n", max);
system("pause");
return 0;
}