1. 程式人生 > >1001 A+B Format - 字串處理

1001 A+B Format - 字串處理

思路:用stringstream把int轉換為string

程式碼如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<cmath>
#include<set>
#include<sstream>
#define ll long long
using namespace std;
const int N=505,INF=0x3f3f3f3f;

int main(){
    int a,b;
    scanf("%d%d",&a,&b);
    int c=a+b;
    string ans;
    stringstream ss;
    ss<<abs(c);
    ss>>ans;
    int len=ans.length();
    if(c<0)printf("-");
    int yu=len%3;
    for(int i=0;i<len;i++){
        if(yu==0){
            if(i>=3&&i%3==0)printf(",%c",ans[i]);
            else printf("%c",ans[i]);
        }
        else{
            if(i<yu)printf("%c",ans[i]);
            else if(i==yu)printf(",%c",ans[i]);
            else if((i-yu)>=3&&(i-yu)%3==0)printf(",%c",ans[i]);
            else printf("%c",ans[i]);
        }
    }
    printf("\n");
}