1. 程式人生 > >實驗2-4-1 統計各位數字之和是5的數 (20分)

實驗2-4-1 統計各位數字之和是5的數 (20分)

http://pta.patest.cn/pta/test/13/exam/3/question/411

#include <stdio.h>
int is( int number );
void count_sum( int a, int b );
int main() {
int a, b;
scanf("%d %d", &a, &b);
if (is(a)) printf("%d is counted.\n", a);
if (is(b)) printf("%d is counted.\n", b);
count_sum(a, b);
return 0;
}
int is(int number) {
//1526
int c,d;
int sum=0;
while(number!=0) {
c=number%10;
sum=sum+c;
number=number/10;
}
if(sum==5) {
return 1;
} else {
return 0;
}
}//104   999
void count_sum( int a, int b ) {
int i;
int sum1=0;
int count=0;
for(i=a; i<=b; i++) {
if(is(i)) {
count++;
sum1=sum1+i;
}
}
printf("count = %d, sum = %d",count,sum1);
}