九度OJ-题目1171:C翻转-10计院上机C

来源:互联网 发布:java安装失败1335 编辑:程序博客网 时间:2024/05/22 04:35

题目描述:
首先输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作数据为以x y为左上角的那几个数据。
操作类型有四种:
1 2 表示:90度,顺时针,翻转4个数
1 3 表示:90度,顺时针,翻转9个数
2 2 表示:90度,逆时针,翻转4个数
2 3 表示:90度,逆时针,翻转9个数
输入:
输入有多组数据。
每组输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作数据为以x y为左上角的那几个数据。
输出:
输出翻转后的数组。
样例输入:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
1 3 1 1
样例输出:
11 6 1 4 5
12 7 2 9 10
13 8 3 14 15
16 17 18 19 20
21 22 23 24 25

#include <iostream>#include <cstdio> using namespace std;int buf[5][5];void swap(int &a,int &b){    int temp=a;    a=b;    b=temp;} void clockWise2(int x,int y){    int temp;    temp=buf[x][y];    swap(temp,buf[x][y+1]);    swap(temp,buf[x+1][y+1]);    swap(temp,buf[x+1][y]);    swap(temp,buf[x][y]);}void clockWise3(int x,int y){    int temp;    for (int i=0;i<2;i++){        temp=buf[x][y];        swap(temp,buf[x][y+1]);        swap(temp,buf[x][y+2]);        swap(temp,buf[x+1][y+2]);        swap(temp,buf[x+2][y+2]);        swap(temp,buf[x+2][y+1]);        swap(temp,buf[x+2][y]);        swap(temp,buf[x+1][y]);        swap(temp,buf[x][y]);    }}void antiClock2(int x,int y){    for (int i=0;i<3;i++){        clockWise2(x,y);    }}void antiClock3(int x,int y){    for (int i=0;i<3;i++){        clockWise3(x,y);    }}int main(){    int operation[4];    bool firstCase;    while (scanf("%d",&buf[0][0])!=EOF){        //input        for (int i=0;i<5;i++){            for (int j=0;j<5;j++){                if (i==0&&j==0)                    continue;                scanf("%d",&buf[i][j]);            }        }        for (int i=0;i<4;i++){            scanf("%d",&operation[i]);/*bug*/        }        operation[2]--;/*bug*/        operation[3]--;        //initiate        //operate        switch (operation[0]){            case 1:                if (operation[1]==2)                    clockWise2(operation[2],operation[3]);                else if (operation[1]=3)                    clockWise3(operation[2],operation[3]);                break;            case 2:                if (operation[1]==2)                    antiClock2(operation[2],operation[3]);                else if (operation[1]=3)                    antiClock3(operation[2],operation[3]);                break;        }        //output        for (int i=0;i<5;i++){            firstCase=true;            for (int j=0;j<5;j++){                if (firstCase)                    firstCase=false;                else                    printf(" ");                printf("%d",buf[i][j]);            }            printf("\n");        }    }    return true;}
0 0
原创粉丝点击