PAT Grade A Zhenti 1001. A+B Format

Topic link: https://www.patest.cn/contests/pat-a-practise/1001


The meaning of the question, give us two numbers from -1000000 ~ 1000000, find the sum of the two numbers, and output the result in the form of one "," for every three digits.

The meaning of the question is very simple, and the range of these two numbers and the range of the sum will obviously not exceed the range of int, so we first directly input the two numbers in the form of int and calculate the sum of these two numbers, then we take out each One bit, when the number of digits modulo 3 is equal to 1 and the number of digits is not 1, we add "," to solve this problem.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 7;
int num[7];
int main() {
	int a, b, sum, len, tmp;
	while(scanf("%d%d", &a, &b) !=EOF) {
		sum = a + b;
		len = 0;
		tmp = sum<0? 1 : 0;
		sum = sum<0? -am
		if(sum == 0) {
			printf("0\n");
			continue;
		}
		while(sum) {
			num [len ++] = sum% 10;
			sum /= 10;
		}
		if(tmp) {
			printf("-");
		}
		for(int i=len-1; i>=0; i--) {
			printf("%d", num[i]);
			if((i+1) % 3 == 1 && i != 0) {
				printf(",");
			}
		}
		printf("\n");
	}
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326733654&siteId=291194637