代码改变世界

1780. Faulty Odometer Again

2012-01-26 23:16  Min·zc  阅读(199)  评论(0编辑  收藏  举报

分别计算个位十位百位...中包含2,5,8的数字的个数,注意的一点是会出现重复计算的情况,需要减去。比如22,在计算个位的时候被计算了一次,在计算十位的时候还会被计算一次。

因为不知道怎么计算重复的数字个数,所以我是先将重复数字的个数用枚举的方式计算出来,然后保存在表中。

#include<iostream>
using namespace std;
int cal(int n)
{
if(n<2)
return 0;
else if(n<5)
return 1;
else if(n<8)
return 2;
else
return 3;
}
int main()
{
int n;
cin>>n;
int sub[10]={0,3,51,657,7599,83193,882351,9176457,94235199,959646393};
while(n--)
{
int t;
cin>>t;
int ans=0;
int k=1;
int m=0;
int tem=t;
while(t)
{
int tem=0;
tem+=cal(t%10);
t/=10;
tem+=t*3;
tem*=(k-sub[m]);
ans+=tem;
k*=10;
m++;
}
cout<<tem<<": "<<tem-ans<<endl;
}
}