题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5867
Water problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 258 Accepted Submission(s): 170
Problem Description
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.
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.
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
Author
BUPT
Source
解题思路:
对特殊的情况进行预处理即可.
1~19的英文单词是特殊情况. 20 30 ... 整十是特殊情况. 1000也是特殊情况.
其他的就是由上述单词组合得到. 注意整百时是没有"and"的.
注意细节处理即可.
详见代码。
#include <bits/stdc++.h>
using namespace std;
#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)<(y)?(x):(y))
#define ABS(x) ((x)>0?(x):(-(x)))
#define ll long long
#define N 250
const int mod=1e9+7;
const int inf=99999999;
const double esp=1e-9;
int main()
{
int num[]={0,3,3,5,4,4,3,5,5,4,3};
int num_2[]={3,6,6,8,8,7,7,9,8,8,6};
int num_3[]={0,0,6,6,5,5,5,7,6,6};
int hun=7;
int thou=8;
int s=0;
int anss[1010];
int ans[1010];
ans[0]=0;
for(int i=1;i<=1000;i++)
{
s=0;
int a,b,c;
a=i/1000;
if(a>0)
s+=num[a],s+=thou;
//if(i==108)
// cout<<a<<" "<<s<<endl;
b=i/100%10;
if(b>0)
{
if(a>0)
s+=3;
s+=num[b];
s+=hun;
}
//if(i==108)
// cout<<b<<" "<<s<<endl;
c=i%100;
if(c>0&&(a>0||b>0))
s+=3;
if(c>=20)
{
s+=num_3[c/10];
if(c%10>0)
s+=num[c%10];
}
else if(c>=10)
{
s+=num_2[c-10];
}
else s+=num[c];
//if(i==108)
// cout<<c<<" "<<s<<endl;
anss[i]=s;
ans[i]=ans[i-1]+s;
}
int t,a;
scanf("%d",&t);
while(t--)
{
scanf("%d",&a);
//printf("%d\n",anss[a]);
printf("%d\n",ans[a]);
}
return 0;
}