1001: 九九乘法表
题目描述
九九乘法表
输出
输出形式为正三角形,格式如下:
1*1=1,
2*1=2,2*2=4,
3*1=3,3*2=6,3*3=9,
.....
(注意:逗号)
分析:
我们在这用一个循环表格用于分析:
i | j | 效果 |
1 | 1 | 1*1=1 |
2 | 1 | 2*1=1 |
2 | 2 | 2*2=4 |
3 | 1 | |
3 | 2 | |
3 | 3 | |
4 | 1 | |
4 | 2 | |
4 | 3 | |
4 | 4 | |
5 | 1 | |
5 | 2 | |
5 | 3 | |
5 | 4 | |
5 | 5 | |
6 | 1 | |
6 | 2 | |
…… | …… |
代码:
#include<bits/stdc++.h>//个人习惯用万能头
using namespace std;
int main(){
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
cout<<i<<'*'<<j<<'='<<i*j<<',';
}
cout<<endl;
}
return 0;
}
错误代码展示:
#include<bits/stdc++.h>//个人习惯用万能头
using namespace std;
int main(){
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
cout<<i<<'*'<<j<<'='<<i*j;
}
}
return 0;
}
错误原因:未换行。