title: PAT1001
date: 2021-01-22 20:03:07
tags: PAT
1001題解
問題:
用標(biāo)準(zhǔn)顯示法顯示兩數(shù)之和镊屎,例如:99,999
范圍:
-10^6
<= a,b <= 10^6
分析:
- 相加后為
正 負(fù) 零
三種整數(shù) - 從后往前每 3 位一個(gè)逗號(hào)
題解:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std ;
int main(){
int a, b ;
cin >> a >> b ;
int c = a + b ;
char tmp[10] ;
if(c == 0) cout << 0 ;
else {
bool minus ;
if (c < 0) {
minus = true ;
c = -c ;
}
else {
minus = false ;
}
if(minus) cout<<'-' ;
sprintf(tmp, "%d", c) ;
int len = strlen(tmp) ;
for(int i = 0; i <= len; i++){
cout<<tmp[i] ;
if((len - i - 1) % 3 == 0 && (len - i - 1) / 3 != 0){
cout<<',' ;
}
}
}
}
//input
/*
-1000000 9
*/