Ural1313(输出格式)

题目链接:点击打开链接

解题思路:

本题考查输出格式,不用看描述,太长····直接看样例即可。PTQ告诉我一种方法,十分神奇。

首先对于左上角(包括对角线)那些元素开一个数组存储,发现对于每个斜线上的元素,都以每行第一个元素为首,依次变换为(i – 1 , j + 1);同理。对于右下角哪些元素,从最右侧的每个元素为首,依次变换为(i + 1 , j – 1)。不同之处在于第二个数组倒着输出。

本题第二个样例(隐藏的)为特殊数据,,当n == 1时,我们直接输出g[0][0]即可,作为特殊处理。

完整代码:

#include <algorithm>#include <iostream>#include <cstring>#include <climits>#include <cstdio>#include <string>#include <cmath>#include <map>#include <queue>using namespace std;typedef long long LL;const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const double EPS = 1e-9;const double PI = acos(-1.0); //M_PI;const int maxn = 101;int g[maxn][maxn];int res[10001] , res2[10001];int main(){#ifdef DoubleQfreopen("in.txt","r",stdin);#endifstd::ios::sync_with_stdio(false);std::cin.tie(0);int n;while(cin >> n){for(int i = 0 ; i < n ; i ++)for(int j = 0 ; j < n ; j ++)cin >> g[i][j];if(n == 1){cout << g[0][0] << endl;continue;}int index = 0;for(int k = 0 ; k < n ; k ++){int j = 0;res[index++] = g[k][j];int i = k;while(i – 1 >= 0 && j + 1 < n){i –;j ++;res[index++] = g[i][j];}}int index2 = 0;for(int k = n – 1 ; k >= 1 ; k –){int j = n – 1;res2[index2++] = g[k][j];int i = k;while(i + 1 < n && j – 1 >= 0){i ++;j –;res2[index2++] = g[i][j];}}for(int i = 0 ; i < index ; i ++)cout << res[i] << " ";for(int i = index2 – 1 ; i >= 1 ; i –)cout << res2[i] << " ";cout << res2[0] << endl;}}

更多精彩请访问:点击打开链接

少一点预设的期待,那份对人的关怀会更自在

Ural1313(输出格式)

相关文章:

你感兴趣的文章:

标签云: