[求助]怎么错了?
计算 100 到 1000 间有多少个数其各位数字之和是 5?#include<stdio.h>
main()
{
int a,b,c=0,sum=0;
for(a=100;a<1000;a++)
{
while (a)
{ b=a%10;
a=a/10;
sum=sum+b;
}
if(sum==5)
c=c+1;
}
printf("%d\n",c);
}
我也做了一个, 不过运行效率可能更低.
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i, j = 0;
int count = 0;
int sum = 0;
char *s = (char*)malloc(3 * sizeof(char));
for(i = 100; i < 1000; i++)
{
itoa(i, s, 10);
for(j = 0; j < 3; j++)
sum += (s[j] - '0');
if(sum == 5)
count++;
sum = 0;
}
printf("\n %d \n", count);
}