• UVa512 追踪电子表格中的单元格



    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=453
    法一:算出变化后的表格

    #include<stdio.h> #include<string.h> #define maxd 100 #define BIG 10000 //注意这里取10000 d中的万位为行坐标,个位为纵坐标 int r, c, n, d[maxd][maxd], d2[maxd][maxd], ans[maxd][maxd], cols[maxd]; //此处cols储存操作行或者列的位置,需要操作的为1,其余为0 void copy(char type, int p, int q) { if(type == 'R') { for(int i = 1; i <= c; i++) d[p][i] = d2[q][i]; //复制行到行 } else { for(int i = 1; i <= r; i++) //复制列到列 d[i][p] = d2[i][q]; } } void del(char type) { memcpy(d2, d, sizeof(d)); int cnt = type == 'R' ? r : c, cnt2 = 0; for(int i = 1; i <= cnt; i++) { if(!cols[i]) copy(type, ++cnt2, i); //遇到1时,cnt2少加一次,i正常递增 } if(type == 'R') r = cnt2; else c = cnt2; //新的行数或者列数 } void ins(char type) { memcpy(d2, d, sizeof(d)); int cnt = type == 'R' ? r : c, cnt2 = 0; for(int i = 1; i <= cnt; i++) { if(cols[i]) copy(type, ++cnt2, 0); //遇到1时,cnt2 多加一次, copy(type, ++cnt2, i); //i正常递增 由于将0行或是0列移动到增加的位置,所以增加的空位置内填入的都是0 } if(type == 'R') r = cnt2; else c = cnt2; //新的行数或者列数 } int main() { int r1, c1, r2, c2, q, kase = 0; char cmd[10]; memset(d, 0, sizeof(d)); while(scanf("%d%d%d", &r, &c, &n) == 3 && r) { int r0 = r, c0 = c; for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) d[i][j] = i*BIG + j; //为d赋值,第一行10001,10002……第二行20001,20002…… while(n--) { //接下来输入n个操作语句 scanf("%s", cmd); if(cmd[0] == 'E') { scanf("%d%d%d%d", &r1, &c1, &r2, &c2); int t = d[r1][c1]; d[r1][c1] = d[r2][c2]; d[r2][c2] = t; } else { int a, x; scanf("%d", &a); memset(cols, 0, sizeof(cols)); for(int i = 0; i < a; i++) { scanf("%d", &x); cols[x] = 1; } if(cmd[0] == 'D') del(cmd[1]); else ins(cmd[1]); //cmd存储操作行为的字符DR、DC、IR、IC。通过cmd[0]和cmd[1]共同组成判断条件 } } memset(ans, 0, sizeof(ans)); for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) {//原来是30001,上移1行,赋值20001,即从(3,1)变为(2,1)
    //d[i][j]值 /10000 %10000 存储的是原来的行数和列数 ans[d[i][j]
    /BIG][d[i][j]%BIG] = i*BIG+j; //ans存储经过增减调换后的数组,ans的内容为变化后的数组d的坐标 } if(kase > 0) printf(" "); printf("Spreadsheet #%d ", ++kase); scanf("%d", &q); while(q--) { scanf("%d%d", &r1, &c1); printf("Cell data in (%d,%d) ", r1, c1); if(ans[r1][c1] == 0) printf("GONE "); else printf("moved to (%d,%d) ", ans[r1][c1]/BIG, ans[r1][c1]%BIG); //查找该数在变化后的数组中的位置,输出坐标 } } return 0; }

    法二:追踪电子表格中单元格的变化

    #include<stdio.h>
    #include<string.h>
    #define maxd 1000
    struct Command{
        char c[5];
        int r1,c1,r2,c2;
        int a,x[20];
    }cmd[maxd];
    int r,c,n;
    int simulate(int *r0,int *c0)
    {
        for(int i=0;i<n;i++)
        {
            if(cmd[i].c[0]=='E')
            {
                if(cmd[i].r1==*r0&&cmd[i].c1==*c0)
            {
                *r0=cmd[i].r2;*c0=cmd[i].c2;
            }    
            else if(cmd[i].r2==*r0&&cmd[i].c2==*r0)
            {
                *r0==cmd[i].r1;*c0=cmd[i].c1;
            }
            }
            else{
                int dr,dc=0;
                for(int j=0;j<cmd[i].a;j++)
                {
                    int x=cmd[i].x[j];
                    if(cmd[i].c[0]=='I')//IR 2 3
                    {
                        if(cmd[i].c[1]=='R'&&x<=*r0) dr++;
                        if(cmd[i].c[1]=='C'&&x<=*c0) dc++;
                    }
                    else{
                        if(cmd[i].c[1]=='R'&&x==*r0) return 0;
                        if(cmd[i].c[1]=='C'&&x==*c0) return 0;
                        if(cmd[i].c[1]=='R'&&x<*r0) dr--;
                        if(cmd[i].c[1]=='C'&&x<*c0) dc--;
                    }
                }
                *r0+=dr;
                *c0+=dc;
            }
        }
        return 1;
    }
    int main()
    {
        int r0,c0,q,kase=0;
        while(scanf("%d%d%d",&r,&c,&n)==3&&r)
        {
            for(int i=0;i<n;i++)
            {
                scanf("%s",cmd[i].c);//R 1 2 3 4
                if(cmd[i].c[0]=='E'){
                    scanf("%d%d%d%d",&cmd[i].r1,&cmd[i].c1,&cmd[i].r2,&cmd[i].c2);}
                    else{ //IR 2 1 2 DR 3 1 2 4
                        scanf("%d",&cmd[i].a);
                        for(int j=0;j<cmd[i].a;j++)
                            scanf("%d",&cmd[i].x[j]);
                        
                    }
                }
                if(kase>0) printf("
    ");
                printf("Spreadsheet #%d
    ",++kase);
                scanf("%d",&q);
                while(q--)
                {
                    scanf("%d%d",&r0,&c0);
                    printf("Cell data in (%d,%d)",r0,c0);
                    if(!simulate(&r0,&c0)) printf("GONE");
                    else printf("moved to (%d,%d)",r0,c0);
                }
            }
            return 0;
        }
    • 感想
      1.表格中的内容为”行000列“这样的五位数方便统计坐标。
      横标:数/10000
      纵标:数%10000
      2.新数组
      ans[d[i][j]/BIG][d[i][j]%BIG]=i*BIG+j
      保存的是变化后的数组
      3.插入时 cnt2多加一次 i正常递增
      删除时 cnt2少加一次 i正常递增
      cnt2被i覆盖
      4.输出坐标时from(c1,r1) move to (ans[c1][r1]/BIG,ans[c1][r1]%BIG)


  • 相关阅读:
    接口测试如何在json中引用mock变量
    接口测试--接口文档规范
    接口测试和性能测试的区别
    接口测试和功能测试的区别
    接口请求(get、post、head等)详解
    软件测试流程
    软件测试系统学习流程和常见面试题
    接口测试之json中的key获取
    正则表达式解析
    Jmeter使用HTTPS协议
  • 原文地址:https://www.cnblogs.com/is-Tina/p/7274754.html
Copyright © 2020-2023  润新知