PAT A 1001 A+B Format

来源:互联网 发布:陈奕迅专辑 知乎 编辑:程序博客网 时间:2024/06/06 23:24

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

Description

输入A,B
格式化输出A+B
从后往前,三个打一个逗号

Algorithm

算出A+B=C
然后sprintf到s
对s从后往前加逗号
然后输出

Code

#define LOG(x) cout << #x << " = " << (x) << endl#define PRINTLN(x) cout << (x) << endl#define MEM(x, y) memset((x), (y), sizeof((x)))#include <bits/stdc++.h>using namespace std;const double PI = 2*acos(0);typedef long long ll;typedef complex<double> Complex;int nextInt(){  int x;  scanf("%d", &x);  return x;}ll nextLL(){  ll x;  scanf("%lld", &x);  return x;}//TEMPLATE//MAINint main(){  //freopen("in.txt", "r", stdin);  int a = nextInt(), b = nextInt();  int c = a + b;  char s[1000];  sprintf(s, "%d", c);  vector<char> v;  int j = 0;  for (int i = strlen(s) - 1; i >= 0; i--) {    v.push_back(s[i]);    j++;    if (j == 3 && s[i - 1] != '-' && i > 0) {      v.push_back(',');      j = 0;    }  }  while (!v.empty()) {    cout << v.back();    v.pop_back();  }  cout << endl;}
0 0
原创粉丝点击