PAT刷題


1001. A+B Format(20)


#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

char str[100];

int main(){
	// freopen("I-in.txt", "r", stdin);
	int a, b;
	scanf("%d%d", &a, &b);
	int c = a + b;
	int flag = 0;
	if(c < 0) flag = 1;
	int pos = 1;
	int tmp = abs(c);
	while(tmp){
		if(pos%4 == 0)str[pos++] = ',';
		str[pos++] = '0' + tmp % 10;
		tmp /= 10;
	}
	if(flag) printf("-");
	for(int i = pos - 1; i >= 1; i--)
		printf("%c", str[i]);
	if(!c) printf("0");
	printf("\n");
	return 0;
}


發佈了274 篇原創文章 · 獲贊 19 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章