题目
十进制数包含0,1,2,3,4,5,6,7,8,9共10个计数符号,其中仅0,6,8,9四个计数符号中包含封闭的圆圈。0,
6和9分别包含1个圆圈,8包含2个圆圈。请编程计算low至high之间的所有整数所包含的圆圈总数。
输入
两个整数low和high(可以是负整数、0或正整数,但满足low<=high)
输出
[low, high]中数字包含的圆圈总数
代码
#include<stdio.h>
#include<math.h>
int main(){
int sum(int a,int b);
int low,high;
scanf("%d %d",&low,&high);
printf("%d\n",sum(low,high));
return 0;
}
//一个数有几个圈
int digit(int x){
int count=0;
int t;
if(x<0)
x=fabs(x);
//注意当数为负数时的取余求模运算,我是在实验时发现不对,于是取绝对值
if(x!=0)
{
while(x!=0)
{
t=x%10;
if(t==0||t==6||t==9)
count+=1;
if(t==8)
count+=2;
x/=10;
}
}
else
count+=1;
return count;
}
//遍历由a到b的数
int sum(int a,int b){
int digit(int x);
int sumary=0;
for(int i=a;i<=b;i++){
sumary+=digit(i);
}
return sumary;
}