title: 261119教师培训第四天 - 图形打印、while 循环 author: wu_sir date: 2026-11-19 organization: 河源高级中学 header: title————{title} ———— {author}(organization)footer:起始于:{organization}) footer: 起始于:{date} - - - - - - 第 pageNo页/共{pageNo} 页 / 共 {pageCount} 页 - - - - - - 终止于:${today} keywords:


图形打印 - 切割图形

#B1202. 小杨的 H 字矩阵 [GESP202312 二级]

code
	int n;	cin >> n;
	
	// 上半部分
	for(int i = 1; i <= n / 2; i ++) {
		cout << '|';
		for(int j = 1; j <= n - 2; j ++) cout << 'a';
		cout << '|' << endl;
	}
	cout << '|';
	for(int j = 1; j <= n - 2; j ++) cout << '-';
	cout << '|' << endl;
	for(int i = 1; i <= n / 2; i ++) {
		cout << '|';
		for(int j = 1; j <= n - 2; j ++) cout << 'a';
		cout << '|' << endl;
	}

图形打印 - 逐步变换

#B1210. 箭形图案

code
	int n;	cin >> n;
	
	int a = 2 * n, b = 1;
	for(int i = 1; i <= n + 1; i ++) {
		for(int j = 1; j <= a; j ++) cout << ' ';
		for(int j = 1; j <= b; j ++) cout << '*';
		cout << endl;
		a -= 2, b ++;
	}
	a = 2, b = n;
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= a; j ++) cout << ' ';
		for(int j = 1; j <= b; j ++) cout << '*';
		cout << endl;
		a += 2, b --;
	}

图形打印 - 计数变化

#B1209. 数字递增的正三角形

code
	int n;	cin >> n;
	
	int a = n - 1, b = 1;
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= a; j ++) cout << ' ';
		char c = '0';
		for(int j = 1; j <= b; j ++) {
			c ++;
			if(c == '9' + 1) c = 'A';
			cout << c;
		}
		for(int j = 1; j <= b - 1; j ++) {
			c --;
			if(c == 'A' - 1) c = '9';
			cout << c;
		}
		cout << endl;
		a --, b ++;
	}

图形打印 - 矩阵坐标思想

#B1233. 田字矩阵

code
	int n;	cin >> n;
	
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= n; j ++) {
			if(j == 1 || j == n) cout << '|';
			else if(i == 1 || i == n) cout << '-';
			else if(i == n / 2 + 1 && j == n / 2 + 1) cout << 'x';
			else if(i == n / 2 + 1) cout << '-';
			else if(j == n / 2 + 1) cout << '|';
			else cout << 'x';
		}
		cout << endl;
	}

#B1008. 打印六芒星

code
	int n;	cin >> n;
	
	int h = 4 * n - 3, m = 6 * n - 5;
	for(int i = 1; i <= h; i ++) {
		for(int j = 1; j <= m; j ++) {
			if((i == n || i == 3 * n - 2) && j % 2 != 0) cout << '*';
			else if(i - j == n - 1 || i - j == 1 - (3 * n - 2)) cout << '*';
			else if(i + j == 3 * n - 2 + 1 || i + j == n + m) cout << '*';
			else cout << ' ';
		}
		cout << endl;
	}

while 循环

#B1172. 数据统计(死循环读入)

code
	int mx = -1e9, mn = 1e9, sum = 0, cnt = 0;
	int x;
	while(cin >> x) {
		if(x > mx) mx = x;
		if(x < mn) mn = x;
		
		sum += x;
		cnt ++;
	}
	
	cout << fixed << setprecision(3);
	cout << mn << ' ' << mx << ' ' << (double)sum / cnt;

#B1176. 角谷猜想

code
	int n;	cin >> n;
	
	while(n != 1) {
		if(n % 2 == 0) {
			cout << n << "/2=" << n / 2 << endl;
			n /= 2;
		} else {
			cout << n << "*3+1=" << n * 3 + 1 << endl;
			n = n * 3 + 1;
		}
	}
	cout << "End";

#B1179. n-1 位数

code
	// int n;	cin >> n;
	
	// int p = 1;
	// while(p * 10 <= n) p *= 10;
	
	// cout << n % p;
	
	char c;	cin >> c;
	int n;	cin >> n;
	cout << n;

#B1298. 回文时间

code
	char c;
	int h, m;	cin >> h >> c >> m;
	
	while(true) {
		// 先将时间加 1 
		m ++;
		if(m == 60) h ++, m = 0;
		if(h == 24) h = 0;
		
		// 考虑 HH:MM 是否是回文
		int a = h / 10, b = h % 10, c = m / 10, d = m % 10;
		if(a == d && b == c) {
			cout << a << b << ':' << c << d;
			break;
		}
	}

数位分离

#B1181. 分离整数的各个数

#B1183. 含 k 个 3 的数

code
	int n, k;	cin >> n >> k;
	
	int cnt = 0, x = n;
	while(x != 0) {	// 数位分离的副作用:会把对应数变为 0
		int c = x % 10;
		x /= 10;
		
		cnt += c == 3;
	}
	
	if(n % 19 == 0 && cnt == k) cout << "YES";
	else cout << "NO";

#B1187. 数字反转

code
	// int n;	cin >> n;
	
	// if(n < 0) {
	// 	cout << '-';
	// 	n = -n;
	// }
	
	// while(n % 10 == 0 && n != 0) n /= 10;
	// // while(n != 0) cout << n % 10, n /= 10;
	// // do {
	// // 	cout << n % 10;
	// // 	n /= 10;
	// // } while(n != 0);
	
	int n;	cin >> n;
	
	int ans = 0;
	while(n != 0) {
		int c = n % 10;
		n /= 10;
		
		ans = ans * 10 + c;
	}
	
	cout << ans;

0 条评论

目前还没有评论...