【发布时间】:2018-10-09 11:22:09
【问题描述】:
我需要打印这张表
2
0
4 2
3 3
6 4 2
6 6 6
8 6 4 2
9 9 9 9
我为以下结果编写了这段代码
#include <iostream>
using namespace std;
int main(){
const int N = 9;
for(int i = 0; i <= N; i += 3){
for (int j = 0; j <= i; j +=3) {
cout << i << " ";
}
cout << endl;
}
cout << "\n";
for(int i = 2; i <= N; i += 2){
for (int j = i; j > 0; j -= 2) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
我的结果:
0
3 3
6 6 6
9 9 9 9
2
4 2
6 4 2
8 6 4 2
所需结果:
2
0
4 2
3 3
6 4 2
6 6 6
8 6 4 2
9 9 9 9
【问题讨论】:
-
1) 改变你的逻辑来创建数字,这样你的外循环索引对应于行号 2) 将两者合二为一
-
我问我如何组合它们,你的回答是,组合它们!哇
-
再仔细阅读评论......
-
我建议创建子函数:1 创建 3 的倍数,on 用于减少偶数。然后编写两个循环以使 i = 0 -> 4。然后合并循环。
-
#include <iostream> using namespace std; int main(){ const int N = 9; for(int i = 2; i <= N; i += 2){ for (int j = i; j > 0; j -= 2) { cout << j << " "; } cout << endl; for(int j = 0; j <= i; j +=3) { cout << j << " "; } cout << endl; } return 0; }
标签: c++ c++11 sequence tabular