菜鸟记录PAT甲级1001-

菜鸟记录PAT甲级1001-

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

 

Calculate a+b and 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 integers a and b where −106≤a,b≤106. The numbers are separated by a space.

Output Specification:

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

Sample Input:

-1000000 9

Sample Output:

-999,991

 题目分析:

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

个人想法: 

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

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

第一次提交:

 1 #include<stdio.h>
 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
", s);
22             }
23             else if (s / 10 != 0) {
24                 printf("0%d
", 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
", sum);
35         }
36         else if(sum/10!=0) {
37             printf("0%d
", sum);
38         }
39         else
40         {
41             printf("00%d
", sum);
42 
43         }
44         return 0;
45 }
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 菜鸟记录PAT甲级1001-