菜鸟记录:c语言实现PAT甲级1001--Format(20)

前几天刚开始对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 106a,b106. 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\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 }

  提交结果:  

  

 

20分只有2分的屑。。。经过多次测试,发现问题很多,例如:当sum值不超过100时,答案会是025或005这样的格式,0并没有消除,与预期所想不符,大刀阔斧改一下。

 

第二次提交:

#include<stdio.h>
int main()
{

        int a, b;
        int sum;
        int s;
        scanf("%d%d", &a, &b);
        sum = a + b;
        s = sum / 1000;
        while (sum / 1000 != 0||s>0)
        {
            if (s / 1000 !=0)
            {
                a=s/ 1000;
                printf("%d,", a);
                s %= 1000;
            }
            if (s / 100 != 0) {
                printf("%d,", s);
                s = 0;
            }
            else if (s / 10 != 0) {
                printf("%02d,", s);
                s = 0;
            }
            else
            {
                printf("%03d,", s);
                s = 0;
            }
            sum = sum % 1000;
        }
        if (sum / 100 != 0) {
            printf("%d\n", abs(sum));
        }
        else if(sum/10!=0) {
            printf("%02d\n", abs(sum));
        }
        else
        {
            printf("%03d\n", abs(sum));

        }
        return 0;

}
View Code

提交结果:

 

 

 

第二次测试对s进行了改进,在while的判定条件里加入了||s>0,并在几个if语句中对s进行清零,意图通过对高位的判别进行改进,然而还是会有小错误,其中还要特殊情况,sum=0时出现的000。于是在第三次进行改进时,干脆将几个,分开的组用不同变量进行记录。

<--------------------------------------------------------------------------------------------------------------------------------------------------------------->

第三次提交:

 

 1 //10e6===1,000,000
 2 
 3 #include<stdio.h>
 4 #include<math.h>
 5 
 6 int main()
 7 {
 8         int a,b;
 9     int sum;
10     scanf("%d%d",&a,&b);
11     sum=a+b;
12 
13     int t=0,s,x;
14     if(sum<0) t=1;
15     sum=abs(sum);
16     s=sum/1000;
17     x=s/1000;
18     sum%=1000;
19     if(t)
20     {
21         if(x)
22         {
23             s%=1000;
24             printf("-%d,%03d,%03d\n",x,s,sum);
25         }
26         else if(s)
27             printf("-%d,%03d\n",s,sum);
28         else if(sum)
29             printf("-%d\n",sum);
30     }
31     else
32     {
33         if(x)
34         {
35             s%=1000;
36             printf("%d,%03d,%03d\n",x,s,sum);
37         }
38         else if(s)
39             printf("%d,%03d\n",s,sum);
40         else if(sum)
41             printf("%d\n",sum);
42         else
43             printf("0\n");
44     }
45     return 0;
46 
47 }

最终结果:

 

 满分通过。

 

补充:

 

网上看来的,比起增加变量,这样的if语句更加简洁。先判断sum值是否为负数,为负数时就先输出 - 号,在判断sum的大小,根据范围依次输出。

  

总结:

    首先,仅基于对题目的算法进行编写,如果脱离题目,那么范围就可能太小了,需要多个变量和if语句。其次,题目本身并无难问题,调试过程中直觉得是逻辑问题,无新的地方。最后,对于PAT甲级,英语题目,除了数据结构和算法,英语也要提升。

 

 

 

 

posted @ 2022-09-26 20:35  000100110111  阅读(54)  评论(0编辑  收藏  举报