当前位置: 代码迷 >> 综合 >> 比赛题目 Give Me an E
  详细解决方案

比赛题目 Give Me an E

热度:16   发布时间:2023-12-19 11:50:18.0

题目描述
Everyone knows that the letter “E” is the most frequent letter in the English language. In fact, there are one hundred sixteen E’s on this very page ... no, make that one hundred twenty one. Indeed, when spelling out integers it is interesting to see which ones do NOT use the letter “E”. For example 6030 (six thousand thirty) doesn’t. Nor does 4002064 (four million two thousand sixty four). 
It turns out that 6030 is the 64th positive integer that does not use an “E” when spelled out and 4002064 is the 838th such number. Your job is to find the n-th such number.
Note: 1,001,001,001,001,001,001,001,001,000 is “one octillion, one septillion, one sextillion, one quintil-lion, one quadrillion, one trillion, one billion, one million, one thousand”. (Whew!)
输入
The input file will consist of multiple test cases. Each input case will consist of one positive integer n (less than 231) on a line. A 0 indicates end-of-input. (There will be no commas in the input.)
输出
For each input n you will print, with appropriate commas, the n-th positive integer whose spelling does not use an “E”. You may assume that all answers are less than 1028.
示例输入
1
10
838
0示例输出
2
44
4,002,064
这道题目的大体意思就是说输入n,然后输出从1到10^28,用英文表示的数字,第n个英文中没有E的数字。然后输出的时候是没三个数字为一组,每两组之间用一个“,”隔开。
对于这个题,从原始的角度找规律是可行的,小于1000的有19个数字,小于1000^2的有19*(19+1)个数字,小于1000^3的有19*(19+19*(19+1)+1),即每多增加一组数字,就总共有19*(前面所有数字的总和+1);
仔细观察就可以发现,这其中是有规律的,对于输入的n,我们可以把n转化为19进制,每一位对应着原数字上的一组,这样,每一位上对应的数字即可以转化为
原数字这一组的三个数字;
下面是我的程序:

#include<stdio.h>
int main()
{
int c[20]={0,2,4,6,30,32,34,36,40,42,44,46,50,52,54,56,60,62,64,66};
int leap,i,n,leapp,b[20];
while(scanf("%I64d",&n)&&n)
{
leap=0;
while(n)
{
b[leap]=n%20;
leap++;
n=n/20;
}
leapp=0;
if(leap>=8)
leapp=1;
printf("%d",c[b[--leap]]);
for(i=leap-1;i>=0;i--)
{
if(i==6&&leapp)
printf(",000,000");
printf(",%.3d",c[b[i]]);
}
printf("\n");
}
return 0;
}