PAT 甲級 1001 A+B Format

開兩個數組,一個存數字,一個存逗號,一開始用遞歸快點寫了,發現逗號不好處理改用2個數組做了
#include <iostream>
#include "cstring"
#include <stdio.h>
#include "iomanip"
#include "vector"
#include "cmath"
#include "stack"
#include "algorithm"
#include <math.h>
#include "map"
using namespace std;
int main()
{
 
 int a,b,t=0,tt=0;
 cin>>a>>b;
 a+=b;
 if(a<0)
    cout<<"-";
    a=abs(a);
    int *q=new int[10000001];//存逗號
    int *qq=new int[10000001];//存數字
    if(!a)
    cout<<a;
    else{
while(a)
{
    if(t==3)
    {
        q[tt]=1;
        t=0;
    }
    t++;
    qq[tt++]=a%10;
    a/=10;
}
for(int h=tt-1;h>=0;h--)
{


    cout<<qq[h];
    if(q[h]==1)
        cout<<",";
}
}
return  0;


}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章