Water problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 84 Accepted Submission(s): 64
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.
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
Author
BUPT
Source
2016 Multi-University Training Contest 10
题目大意:
给你一个正整数n,让你求从1到n的所有数字的英文表达式的字母个数和。
思路:
①处理一下1-20的每个数字对应英文的个数处理到一个数组中
②处理一下30-90中%10==0的数字对应英文的个数到一个数组中
③对应将百和and两个单词处理到一个定义变量中
④然后分类讨论即可,具体参考代码;
AC代码:
#include<string.h>
#include<stdio.h>
using namespace std;
int gewei[1000]={0,3,3,5,4,4,3,5,5,4,3,6,6,8,8,7,7,9,8,8,6};//1-20
int shiwei[1000]={0,0,6,6,5,5,5,7,6,6};
#define bai 7
#define andd 3
int cal(int x)
{
if(x==1000)return 11;
if(x%100==0)
{
return bai+gewei[x/100];
}
if(x<=20)return gewei[x];
if(x>20&&x<100)
{
return shiwei[x/10]+gewei[x%10];
}
if(x>100)
{
int tmpp=gewei[x/100]+bai+andd;
x%=100;
if(x<=20)tmpp+=gewei[x];
if(x>20&&x<=100)tmpp+=shiwei[x/10]+gewei[x%10];
return tmpp;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
int output=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
output+=cal(i);
}
printf("%d\n",output);
}
}

本文探讨了如何计算从1到n(n不超过1000)所有数字的英文名称中字母的总数,通过预处理常见数字的英文长度并采用分类讨论的方法实现。
3792

被折叠的 条评论
为什么被折叠?



