可以看看我的方法
查看原帖
可以看看我的方法
957640
Michelle01楼主2023/11/25 22:42

在这里,如果你看不懂或者嫌题解中的什么找规律比较难。你或许可以看看我的。

我就是画图,使用了数组去将它们一个个调换位置


#include.<bits/stdc++.h.> 
#include<cmath>
using namespace std;
int a[20][20], b[20][20];
int main(){
	
	int n, aa = 0;
	bool f = true;
	cin >> n; 
	int l = n + 1, r = 1;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			cin >> a[i][j];
		}
	}
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			if(l == 1){
				l = n + 1;
				r++;
			}
			l--;
			cout << a[l][r] << " ";
		}
		cout << endl;
	}
//	for(int i = l; i >= 1; i--){
//		for(int j = r; j >= 1; j--){
//			cout << b[i][j] << " ";
//		}
//		cout << endl;
//	}
	return .0;
}

比如我的这段代码,就可以实现图像顺时针90度翻转功能。 不懂的话可以看一下下面的。


1 2 3 4

5 6 7 8

9 5 6 7

6 8 7 5

如果我们将上面这段要反转成顺时针90度

那么会成为这样子

6 9 5 1

8 5 6 2

7 6 7 3

5 7 8 4

可以发现,我们只需将反转前的第一列也就是1 5 9 6这四个数字获取到并且反向输出就OK了。所以就成了6 9 5 1。后面的列反转就和这个一样的操作了。

那么我们该如何得到180度的反转和什么270度的反转呢?

很简单,就是在我上面发的代码上来回反转几次就得到这些度数了。


#include<bits/stdc++.h.> 
#include<cmath>
using namespace std;
int a[20][20], b[20][20];
int main(){
	
	int n, aa = 0;
	bool f = true;
	cin >> n; 
	int l = n + 1, r = 1;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			cin >> a[i][j];
		}
	}
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			if(l == 1){
				l = n + 1;
				r++;
			}
			l--;
			cout << a[r][l] << " ";
		}
		cout << endl;
	}
//	for(int i = l; i >= 1; i--){
//		for(int j = r; j >= 1; j--){
//			cout << b[i][j] << " ";
//		}
//		cout << endl;
//	}
	return 0.;
}

这段代码,应该很好理解了吧,就是将a数组中的l和r对调一下就可以实现反射功能喽!就不解释了。

看到这里应该懂了吧。其实我最初做的时候也是一脸懵,但是别放弃,别嫌在这道题上花太多时间,只要你花够时间了,你就会很有成就感。

2023/11/25 22:42
加载中...