PAT A A+B Format

博客围绕PAT A 1001题目展开,题目要求输入A和B,格式化输出A+B,从后往前每三位打一个逗号。算法是先算出A+B的结果C,用sprintf将结果存入字符串s,再从后往前给s加逗号后输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PAT A 1001

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

//MAIN
int 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值