题目
题解
看题解注释吧, 就是控制输出格式。
注意:10 * 10会出现的特殊情况。这可能是你数据过了80的原因。
代码
#include<bits/stdc++.h>
using namespace std;
int a, b;
int main()
{
// cout<<" 99"<<endl;
// cout<<"×99"<<endl;
// cout<<"━━━"<<endl;
// cout<<" 891"<<endl;
// cout<<"891"<<endl;
// cout<<"9801"<<endl;
cin>>a>>b;
cout<<(a/10?" ":" ")<<a<<endl;
cout<<"×"<<(b/10?"":" ")<<b<<endl;
cout<<"━━━"<<endl;
if(b/10) { // 第二个乘数是两位数,那么就会存在两个横线
int t = (b%10)*a;
// if用于处理 10 ×10 的情况 第一行应该为两个0
if(t) cout<<(t/100?" ":t/10?" ":" ")<<t<<endl; // b的低位乘以a不是0,则按格式输出结果即可
else cout<<(a/10?" 00":" 0")<<endl; // b的低位乘以a是0,则a有多少位输出多少个0,同时要求输出的0也要对齐
int k = (b/10)*a;
cout<<(k/100?"":k/10?" ":" ")<<k<<endl; // 如果是三位数,不输出空格,两位数输出一个空格,一位数输出三个空格
cout<<"━━━"<<endl;
int c = a*b;
cout<<(c/1000?"":c/100?" ":c/10?" ":" ")<<c<<endl; // 四位数不输出空格,三位数一个空格,两位数两个空格,不存在一位数情况,输出什么都无所谓
} else {
int c = a*b;
cout<<(c/10?" ":" ")<<c<<endl; // 控制好格式,输出即可
}
return 0;
}