7-1 币值转换 (20 分)
输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式。如23108元,转换后变成“贰万叁仟壹百零捌”元。为了简化输出,用小写英文字母a-j顺序代表大写数字0-9,用S、B、Q、W、Y分别代表拾、百、仟、万、亿。于是23108元应被转换输出为“cWdQbBai”元。
输入格式:
输入在一行中给出一个不超过9位的非负整数。
输出格式:
在一行中输出转换后的结果。注意“零”的用法必须符合中文习惯
输入样例1:
813227345
输出样例1:
iYbQdBcScWhQdBeSf
输入样例2:
6900
输出样例2:
gQjB
思路
实验代码一:
···
include <stdio.h>
int main (void)
{
long int n,o,p,q,r,s,t,u,v,w,y;
scanf ("%d",&n);
o=n/100000000;
p=(n-o100000000)/10000000;
q=(n-o100000000-p10000000)/1000000;
r=(n-o100000000-p10000000-q1000000)/100000;
s=(n-o100000000-p10000000-q1000000-r100000)/10000;
t=(n-o100000000-p10000000-q1000000-r100000-s10000)/1000;
u=(n-o100000000-p10000000-q1000000-r100000-s10000-t1000)/100;
v=(n-o100000000-p10000000-q1000000-r100000-s10000-t1000-u100)/10;
w=(n-o100000000-p10000000-q1000000-r100000-s10000-t1000-u100-v10);
switch(o){
scanf ("%d",&o);
case 1: printf("bY");break;
case 2: printf("cY");break;
case 3: printf("dY");break;
case 4: printf("eY");break;
case 5: printf("fY");break;
case 6: printf("gY");break;
case 7: printf("hY");break;
case 8: printf("iY");break;
case 9: printf("jY");break;
default:break;
}
switch(p){
scanf ("%d",&p);
case 1: printf("bQ");break;
case 2: printf("cQ");break;
case 3: printf("dQ");break;
case 4: printf("eQ");break;
case 5: printf("fQ");break;
case 6: printf("gQ");break;
case 7: printf("hQ");break;
case 8: printf("iQ");break;
case 9: printf("jQ");break;
default:break;
}
switch(q){
scanf ("%d",&q);
case 1: printf("bB");break;
case 2: printf("cB");break;
case 3: printf("dB");break;
case 4: printf("eB");break;
case 5: printf("fB");break;
case 6: printf("gB");break;
case 7: printf("hB");break;
case 8: printf("iB");break;
case 9: printf("jB");break;
default:break;
}
switch(r){
scanf ("%d",&r);
case 1: printf("bS");break;
case 2: printf("cS");break;
case 3: printf("dS");break;
case 4: printf("eS");break;
case 5: printf("fS");break;
case 6: printf("gS");break;
case 7: printf("hS");break;
case 8: printf("iS");break;
case 9: printf("jS");break;
default:break;
}
if(n>10000)
switch(s){
scanf ("%d",&s);
case 1: printf("bW");break;
case 2: printf("cW");break;
case 3: printf("dW");break;
case 4: printf("eW");break;
case 5: printf("fW");break;
case 6: printf("gW");break;
case 7: printf("hW");break;
case 8: printf("iW");break;
case 9: printf("jw");break;
default:printf("W"); break;
}
switch(t){
scanf ("%d",&t);
case 1: printf("bQ");break;
case 2: printf("cQ");break;
case 3: printf("dQ");break;
case 4: printf("eQ");break;
case 5: printf("fQ");break;
case 6: printf("gQ");break;
case 7: printf("hQ");break;
case 8: printf("iQ");break;
case 9: printf("jQ");break;
default:break;
}
switch(u){
scanf ("%d",&u);
case 1: printf("bB");break;
case 2: printf("cB");break;
case 3: printf("dB");break;
case 4: printf("eB");break;
case 5: printf("fB");break;
case 6: printf("gB");break;
case 7: printf("hB");break;
case 8: printf("iB");break;
case 9: printf("jB");break;
default:break;
}
switch(v){
scanf ("%d",&v);
case 1: printf("bS");break;
case 2: printf("cS");break;
case 3: printf("dS");break;
case 4: printf("eS");break;
case 5: printf("fS");break;
case 6: printf("gS");break;
case 7: printf("hS");break;
case 8: printf("iS");break;
case 9: printf("jS");break;
default:break;
}
switch(w){
scanf ("%d",&w);
case 1: printf("b");break;
case 2: printf("c");break;
case 3: printf("d");break;
case 4: printf("e");break;
case 5: printf("f");break;
case 6: printf("g");break;
case 7: printf("h");break;
case 8: printf("i");break;
case 9: printf("j");break;
default:break;
}
return 0;
}
···
运行结果只有一项错误,但这项错误一种无法解决
解决
方案
在请教助教后,学习了新的方法
思路
实验代码二:
···
include<stdio.h>
int main ()
{
char NUM[10]={'a','b','c','d','e','f','g','h','i','j'};
char UNIT[10]={0,0,'S','B','Q','W','S','B','Q','Y'};
char RESULT[17];
int n,k=0,bitnum=0,cur,pre;
scanf("%d", &n);
if(n==0)
{
printf("a");
return 0;
}
while(n!=0)
{
cur = n % 10;
n /= 10;
bitnum++;
if(cur)
{
if(bitnum>1)
RESULT[k++] = UNIT[bitnum];
RESULT[k++] = NUM[cur];
}
else
{
if(bitnum==5)
RESULT[k++] = UNIT[bitnum];
if(pre!=0&&bitnum!=4&&bitnum!=1)
RESULT[k++]=NUM[cur];
}
pre=cur;
}
for(int i=k-1;i>=0;i--)
printf("%c",RESULT[i]);
return 0;
}
···
注意事项
本题在101这个数字的输出值尤其要考究,对于10000的输出,以及再未达到万位的数字输出也是考量的目标,且需要加入对数位的判断