传智杯校赛题解 | #加法模拟器#
加法模拟器
https://ac.nowcoder.com/acm/problem/22007
显然赛时需要摸索一下%xd的使用方式
Solution
我们可以发现,竖式中的长横是由7条-组成的,因此对于每一个数字的最低位而言,需要对齐自左向右数的第7个位置。
不同的是,对于竖式的第一行,可以直接使用 %7d 进行右对齐;而对于第二行,因为有 + 符号存在,需要使用 %6d。
赛时代码
#include<iostream>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<a<<"+"<<b<<"="<<a+b<<endl;
printf("%7d\n",a);
printf("+");
printf("%6d\n",b);
printf("-------\n");
printf("%7d",a+b);
return 0;
}