很经典的习题,只是提供一种思路,需要多联系循环嵌套,感谢女青年与西西(sinat_38214562)http://blog.csdn.net/sinat_38214562/article/details/72845201提供的思路。
1. n阶数字正方形
#include <iostream>
#include <stdio.h>
using std::cin;
using std::cout;
using std::endl;
int main() {
int n;
int i, j; // i value and number of row and column, j value in row
int c;
cin>>n;
for (i = 1; i <= n; i++) {
cout<<i;
for (j = i, c = 1; c < n; c++) {
if (c < n) {
cout<<" ";
}
cout<<j;
}
if (i < n ) {
cout<<" " << endl;
}
}
system("pause");
return 0;
}
2. 阶数字三角形
#include <iostream>
#include <stdio.h>
using std::cin;
using std::cout;
using std::endl;
int main() {
int n;
int i; // value and number of row
int j; // value of row
int c; // number of column
cin>>n;
for (i = 1; i <= n; i++) {
cout<<i;
for (j = i, c = n - i; c > 0; c--) {
if (i < n) {
cout<<" ";
}
cout<<j;
}
if (i < n) {
cout<<" "<<endl;
}
}
system("pause");
return 0;
}
3. n阶递减三角形
#include <iostream>
#include <stdio.h>
using std::cin;
using std::cout;
using std::endl;
int main() {
int n;
int i;
int j;
cin >> n;
for (i = n; i > 0; i--) {
cout<< i;
if (i > 1) {
cout<<" ";
}
for (j = i - 1; j > 0; j--) {
cout<<j;
if (j > 1) {
cout<<" ";
}
}
if (j = 1){
cout<<" "<<endl;
}
}
system("pause");
return 0;
}
4. 乘法表
#include <iostream>
#include <stdio.h>
using std::cin;
using std::cout;
using std::endl;
int main() {
int j, i, k, n;
cin >> n;
for (j = 1; j <= n; j++) {
for (i = j; i <= n; i++) {
printf("%d*%d = %d ", j, i, j*i);
}
cout<<endl;
}
system("pause");
return 0;
}