思路是先计算个位为8的和,共10个数
再计算十位为8的和,共10个数,88计算两次
main()
{
int sum, i;
sum = 0;
** calculate sum of 8, 18, ...98
for(i=8;i<100;i+=10)
sum += i;
**calculcate sum of 80,81,...89
for( i = 80; i < 90;i++)
sum +=i;
**88 is added twice
sum -= 88;
printf("sum = %d\n",sum);
}
#include
void main()
{
int i;
int sum; sum=0;
for(i=8;i<100;)
{
sum=sum+i;
i+=10;
}
printf("%d\n",sum);
}
转换成字符串处理非常方便:
#include
#include
#include
int i,sum = 0;
for(i = 8;i < 100;i = i + 10) {
sum = sum + i;
}
printf("100以内,个位含有8的整数之和是:%d\n\n",sum);
return 0;
}