前几天刚开始对PAT甲级的刷题,首次看到英语的题目,让原本就菜的我更是头秃,但第一题叫了n遍以后满分通过的时候还是蛮爽的,在此仅记录一下该题的个人解题心路,菜鸟记录,技术极低。
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 integersaandbwhere−106≤a,b≤106. 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:
-1000000 9
Sample Output:
-999,991
题目分析:
加法计算,但是输出格式需分成三个一组,输入和输出的数据范围控制在10e-6~10e6。
个人想法:
首先,加法的计算和数据的范围无需多说,即重点在于输出的分组,开始我以为作为第一题,会是一个近乎没有结构或算法的题目,于是在开始之前以为会是有关键字或某个库中的函数,迟迟不敢下笔而是苦思冥想有没有学过,脑海搜索失败后开始编写这道题。
由于10e6三个一组并没有太多组,所以仅在该题中可以用if语句写,虽然很笨,但有用。
第一次提交:
#include 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 = sum / 1000; if (s >= 1000) { a=s/ 1000; printf(“%d,”, a); s %= 1000; } if (s / 100 != 0) { printf(“%d\n”, s); } else if (s / 10 != 0) { printf(“0%d\n”, s); } else { printf(“00%d,”, s); } sum = sum % 1000; } if (sum / 100 != 0) { printf(“%d\n”, sum); } else if(sum/10!=0) { printf(“0%d\n”, sum); } else { printf(“00%d\n”, sum); } return 0; }