资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

菜鸟记录PAT甲级1001--Format(20)

前几天刚开始对PAT甲级的刷题,首次看到英语的题目,让原本就菜的我更是头秃,但第一题叫了n遍以后满分通过的时候还是蛮爽的,在此仅记录一下该题的个人解题心路,菜鸟记录,技术极低。

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、网站设计、浦北网络推广、成都微信小程序、浦北网络营销、浦北企业策划、浦北品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供浦北建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

Calculatea+band output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input Specification:

Each input file contains one test case. Each case contains a pair of integersaandbwhere106a,b106. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum ofaandbin one line. The sum must be written in the standard format.

Sample Input:

- 9

Sample Output:

-999,991

题目分析:

    加法计算,但是输出格式需分成三个一组,输入和输出的数据范围控制在10e-6~10e6。

个人想法:

    首先,加法的计算和数据的范围无需多说,即重点在于输出的分组,开始我以为作为第一题,会是一个近乎没有结构或算法的题目,于是在开始之前以为会是有关键字或某个库中的函数,迟迟不敢下笔而是苦思冥想有没有学过,脑海搜索失败后开始编写这道题。

    由于10e6三个一组并没有太多组,所以仅在该题中可以用if语句写,虽然很笨,但有用。

第一次提交:

 1 #include
 2 
 3 int main ()
 4 {
 5     int a, b;
 6         int sum;
 7         int s; //记录sum值千位以上的数值,方便输出
 8         scanf("%d%d", &a, &b);
 9         sum = a + b;
10         s = sum / 1000;
11         while (sum / 1000 != 0)
12         {
13             s = sum / 1000;
14             if (s >= 1000)
15             {
16                 a=s/ 1000;
17                 printf("%d,", a);
18                 s %= 1000;
19             }
20             if (s / 100 != 0) {
21                 printf("%d\n", s);
22             }
23             else if (s / 10 != 0) {
24                 printf("0%d\n", s);
25             }
26             else
27             {
28                 printf("00%d,", s);
29 
30             }
31             sum = sum % 1000;
32         }
33         if (sum / 100 != 0) {
34             printf("%d\n", sum);
35         }
36         else if(sum/10!=0) {
37             printf("0%d\n", sum);
38         }
39         else
40         {
41             printf("00%d\n", sum);
42 
43         }
44         return 0;
45 }

分享名称:菜鸟记录PAT甲级1001--Format(20)
标题链接:http://cdkjz.cn/article/dsoiejj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220