当前位置: 代码迷 >> 综合 >> PAT 1005 Spell It Right
  详细解决方案

PAT 1005 Spell It Right

热度:2   发布时间:2024-01-29 22:50:12.0

1005 Spell It Right

题目的意思是:输入n个数,然后把这n个数加起来 然后用英文输出

注意点:考虑数字的时候要考虑0,输入的时候也要考虑全部为0 的情况

#include<stdio.h>
#include<string.h>
int main()
{char a[100];int i=0;while(scanf("%s",&a[i])!=EOF)//输入n个数{i++;}int len = strlen(a);for (int i = 0;i < len;i++)//求和{if (a[i] >= 0 || a[i] <= 9)a[i] = a[i] - '0';}int sum = 0;for (int i = 0;i < len;i++){sum = sum + a[i];}char c[10];int j = 0;if(sum==0)                 //特殊情况 输入的全部为0!c[0]='0';else                       //普通情况{while (sum != 0){c[j] = (sum%10)+ '0';sum = sum / 10;j++;}}for (int i =10;i >= 0;i--){if (c[i] == '0') printf("zero");if (c[i] == '1') printf("one");if (c[i] == '2') printf("two");if (c[i] == '3') printf("three");if (c[i] == '4') printf("four");if (c[i] == '5') printf("five");if (c[i] == '6') printf("six");if (c[i] == '7') printf("seven");if (c[i] == '8') printf("eight");if (c[i] == '9') printf("nine");if ((c[i]>='0'&&c[i]<='9')&&(i!=0)) printf(" ");}return 0;
}
  相关解决方案