跳到正文

休闲·娱乐

防止你会写A+B problem

Bingxiu2约 1 分钟
原文

这是 2026年2月9日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年2月9日
收录
2026年2月9日
更新
2026年11月17日
篇幅
约 1 分钟

版本

  1. v3当前
  2. v2
  3. v1

输入一行两个正整数 A,BA,B,保证 1≤A,B≤1091 \le A,B \le 10^9。 输出一行一个正整数 A+BA+B。

稍微推导一下:
A+B=(A−(A mod B))+(A mod B)+B=⌊AB⌋×B+(A mod B)+B=(⌊AB⌋+1)×B+(A mod B)\begin{aligned} A+B&=(A-(A\ \mathrm{mod}\ B))+(A\ \mathrm{mod}\ B)+B\\ &=\left\lfloor\frac{A}{B}\right\rfloor \times B+(A\ \mathrm{mod}\ B)+B\\ &=\left(\left\lfloor\frac{A}{B}\right\rfloor+1\right) \times B+(A\ \mathrm{mod}\ B) \end{aligned}

于是我们就可以写出会报错的代码:

Rust
//A+B problem
//A+B=(floor(A/B)+1)*B+A mod B
#include<bits/stdc++.h>
using namespace std;
int *a=new int,*b=new int;
int main(){
    cin>>*a>>*b;
    cout<<(*a/*b+1)*(*b)+(*a)%(*b);
    return 0;
}