n阶数字正方形/n阶数字三角形/n阶递减三角形/乘法表

 很经典的习题,只是提供一种思路,需要多联系循环嵌套,感谢女青年与西西(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;	
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值