题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5867
就是分类讨论一下然后打个表就好了。
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int t,n,a[1010]={0,3,3,5,4,4,3,5,5,4,3,6,6,8,8,7,7,9,8,8};
int main()
{
a[20]=6,a[30]=6,a[40]=5,a[50]=5,a[60]=5,a[70]=7,a[80]=6,a[90]=6,a[1000]=11;
for(int i=21;i<1000;i++)
{
if(i<100)
a[i] = a[(i/10)*10] + a[i%10];
else
{
a[i] = a[i/100] + 7 + a[i%100];
if(a[i%100])
a[i] += 3;
}
}
for(int i=1;i<=1000;i++)
a[i] += a[i-1];
cin>>t;
while(t--)
{
cin>>n;
cout<<a[n]<<endl;
}
return 0;
}