菜鸟记录:c语言实现PAT甲级1005--Spell It Right

这篇具有很好参考价值的文章主要介绍了菜鸟记录:c语言实现PAT甲级1005--Spell It Right。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

 非常简单的一题了,但还是交了两三次,原因:对数组的理解不足;对数字和字符之间的转换不够敏感。这将在下文中细说。

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345
 

Sample Output:

one five

题目分析

  输入一串数字,然后用英文表达所有数字相加和的每一位数字。

    输入:10100以内的数字

    输出:和的每位数字英文表达(eg:sum=256,output=two five six)

首先很明显代码是不会翻译你的结果的,所有用一个数组来存储表示每位数字的英文,其次,10100的数字不需要精准表示,因为这里也就100个数字,不是10100个数字,开始我在这卡了半天,想着数字太大要精准存储,特地翻了之前写的c语言-大数阶乘 - 1001010 - 博客园 (cnblogs.com)一文,后面发现不大行的通,审视一遍后发现了问题,最后输出,要怎么拆开数字和也是个问题。

个人想法

  首先,还是从变量入手

1 #define  MAX  100
2 int N,m;//输入N,用于统计N的位数m
3 char lan[10][10] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
4 int sum,a[MAX];//计算和sum,数组a用来记录拆开的sum
5 char n[MAX];

  这里开始是想N用来输入数据,后面发现循环过程中因为不知道它的位数,所以往往遍历要很多【补:而且N作为int变量放不了100位,double型的时候可以,但sum作为int不能让N强转,都作为double时就会很麻烦】,所以在错了一次后采取了  char n[MAX];  的写法,这样有很多的妙处:

  1. 可以利用字符串来进行自动分割,即在输入时不赋值在地址上(n的每位地址),scanf("%s", &n[i]); ❌  scanf("%s", &n);❌  scanf("%s", n);✔  c语言因为只接受字符,不支持string,写成这样就会自动把一串数字字符串分成单独的数字字符并赋值在每个空间单位上。
  2. 循环遍历时,不需要知道有几位,因为每个字符是被附在每个单位空间上的,所以这个数组遍历到空,即'\0'就可以停止。
  3. 由于得到的是字符的地址(数字自生的ACII值),所以在计算sum时,不用强转,而是减去0的ACII值,这个差不仅是地址差也非常融洽的是数字之间的差。

  到这里其实已经没有什么疑惑的点了,下面直接上完整代码。

 1 #include<stdio.h>
 2 
 3 #define  MAX  150
 4 int N,m;
 5 char n[MAX];
 6 char lan[10][10] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
 7 int sum,a[MAX];
 8 int main() {
 9     scanf("%s", n);
10     for (int i = 0; n[i] != '\0'; i++) {
11         sum += n[i] - '0';
12     }
13     if (sum == 0){printf("zero");return 0;}//如果和等于0 ,那么直接输出“zero”就行。注意,这里必须return终止,因为数组a[-1]也是0
14     for (int i = 0; sum; i++) {
15         a[i] = sum % 10;
16         sum /= 10;
17         m += 1;//m记录sum的位数,但注意数组在使用时要减1,因为超范围了
18     }//sum最后等于0时即完成所有位数的记录,终止循环(非常巧妙) 
19     
20         printf("%s", lan[a[m - 1]]);
21         for(int i=m-2;i>=0;i--)
22             printf(" %s", lan[a[i]]);//老一套输出格式,每一位的数字对于英语组记录的每一位上的英文
23     
24     return 0;
25 }

菜鸟记录:c语言实现PAT甲级1005--Spell It Right

 文章来源地址https://www.toymoban.com/news/detail-427438.html

总结

  • 数组的利用,例如本文对英语的存储采用了二维数组,由于是char型,每单位只存放一个字符,所以lan[10][10]有10行10列,每列存放一个字符,一行存放10个字符。如果列数不足或是刚好,就会因为没有读到 '\0' 而出现连续输出,如下。每列只有5个单位,故原本“seven”就该停止却因为没有 '\0' 而读取到下一行“eight”一直到“nine”才停下。同样还有上文提到的数组地址。 菜鸟记录:c语言实现PAT甲级1005--Spell It Right
  •  字符与数字的转换。
  • 停止条件的巧妙使用,如这里for循环采用sum=0停止的条件,是我个人之前没看过的。

  

                                                                                                         

 

到了这里,关于菜鸟记录:c语言实现PAT甲级1005--Spell It Right的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 菜鸟记录:c语言实现PAT甲级1004--Counting Leaves

    好消息:与上题的Emergency是同样的方法。坏消息:又错了c++真的比c方便太多太多。 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Each input file contains one test case. Each case starts with a line containing  0 N 100, the number of nodes in a tree, and  M 

    2023年04月17日
    浏览(53)
  • 菜鸟记录PAT甲级1003--Emergency

    久违的PAT,由于考研408数据结构中有一定需要,同时也是对先前所遗留的竞赛遗憾进行一定弥补 ,再次继续PAT甲级1003.。 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the l

    2023年04月13日
    浏览(45)
  • PAT 甲级【1010 Radix】

    本题范围long型(35)^10 枚举radix范围上限pow(n/a0,1/m)上,考虑上限加1.范围较大。使用二分查找枚举 代码如下 本页面将简要介绍二分查找,由二分法衍生的三分法以及二分答案。 二分查找(英语:binary search),也称折半搜索(英语:half-interval search)、对数搜索(英语:logar

    2024年02月08日
    浏览(39)
  • PAT 甲级考试【1003 Emergency】

    题目: As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead

    2024年02月08日
    浏览(49)
  • 1021 Deepest Root (PAT甲级)

    1021. Deepest Root (25)-PAT甲级真题(图的遍历,dfs,连通分量的个数)_柳婼的博客-CSDN博客 柳婼的解法在这里,两次dfs,还是挺好玩的。 我的解法比较暴力,就是先用并查集算连通分量(这个其实还是dfs来算会更方便),如果只有一个连通分量,那deepest root一定在仅有一条arc的

    2024年02月15日
    浏览(34)
  • 1114 Family Property (PAT甲级)

    This time, you are supposed to help us collect the data for family-owned property. Given each person\\\'s family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate. Input Specification: Each input file contains one test case. For each case, the fir

    2024年02月06日
    浏览(51)
  • 1028 List Sorting (PAT甲级)

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, eac

    2024年02月15日
    浏览(41)
  • 1072 Gas Station (PAT甲级)

    A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range. Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendatio

    2024年02月09日
    浏览(42)
  • 1111 Online Map (PAT甲级)

    这道题我读题不仔细导致踩了个大坑,一个测试点过不了卡了好几个小时:第二个dijkstra算法中,题目要求是“In case the fastest path is not unique, output the one that passes through the fewest intersections”,我却想当然地认为在fastest path is not unique的时候,判断标准是最短距离…… Input our

    2024年02月07日
    浏览(42)
  • pat甲级 1022 Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID\\\'s. Input Specification: Each inp

    2024年04月15日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包