J - Water problem
HDU - 5867
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3+3+5+4+4=19 letters used in total.If all the numbers from 1 to n (up to one thousand) inclusive were written out in words, how many letters would be used?
Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases.
For each test case: There is one positive integer not greater one thousand.
Output
For each case, print the number of letters would be used.
Sample Input
3
1
2
3
Sample Output
3
6
11
题意:求一个不超过n的所有正整数的英文写法的字母数之和
解法:对于1-20以及30、40、50......100单独人为计算,其余的数转换为这些数相加。
代码:
#include<iostream>
using namespace std;
int a[1005]={-1};
int b[1005];
int main()
{
int handred=7;
a[0]=0;
a[1]=a[2]=a[6]=a[10]=3;
a[4]=a[5]=a[9]=4;
a[3]=a[7]=a[8]=a[40]=a[50]=a[60]=5;
a[11]=a[12]=a[20]=a[30]=a[80]=a[90]=6;
a[15]=a[16]=a[70]=7;
a[13]=a[14]=a[18]=a[19]=8;
a[17]=9;
a[1000]=11;
for(int i=21;i<=99;i++)
a[i]=a[i/10*10]+a[i%10];
for(int i=1;i<=9;i++)
a[i*100]=handred+a[i];
for(int i=101;i<=999;i++){
if(i%100==0)
continue;
a[i]=a[i/100*100]+3+a[i%100];
}
b[1]=a[1];
for(int i=2;i<=1000;i++)
b[i]=b[i-1]+a[i];
int n,k;
cin>>n;
while(n--){
cin>>k;
cout<<b[k]<<endl;
}
return 0;
}
网友评论