Spreadsheet Tracking 例题4-5

题目链接

大意为经过几次操作以后原来的位置现在的位置在哪。

操作有两种

EX r1 c1 r2 c2交换单元格(r1,c1),(r2,c2)。
<command> A x 1 x 2 … x A 插入或删除A行或列(DC-删除列,DR-删除行,IC-插入
列,IR-插入行,1≤A≤10)。

题意不难理解 题目看上去有点难

紫书上的使用函数处理确实挺好的

第二种解法链接

第一种解法:

最直接的思路就是首先模拟操作,算出最后的电子表格,然后在每次查询时直接在电子
表格中找到所求的单元格。

 

首先用一个数组 d[][]存放点 为了防止重复将 i*1000 然后加上j 这样就对应了当前的位置

然后根据操作指令将需要操作的 行/列 号存起来

 

删除操作:

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])iscopy(type,++cnt2,i);
    if(type ==  'R')
        r = cnt2;
    else
        c = cnt2;
}

这种写法很好,如果有删除的就直接覆盖,如果不变的话就是 赋值原来的值

然后刷新 r/c 的值这样就达到了删除的目的

 

插入操作

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])iscopy(type,++cnt2,0);
       iscopy(type,++cnt2,i);
    }
    if(type =='R')
        r = cnt2;
    else
        c = cnt2;
}

插入和删除差不多 不过插入往后移 发现是这一行是要插入的话那么就将这一行值为0最后更新 r/c 的值

最后的时候遍历经过删除或添加的数组

        mem(ans,0);
        for(int i = 1;i <= r;i++)
            for(int j = 1;j <= c;j++)
                ans[d[i][j]/BIG][d[i][j]%BIG] = i*BIG+j;

 这样就可以得到原来位置值现在变成了什么。

完整代码如下:

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>

#define mem(a,b) memset(a,b,sizeof(a))
const int maxn = 100;
const int BIG = 1000;
using namespace std;

int r,c,n,d[maxn][maxn],d2[maxn][maxn],ans[maxn][maxn],cols[maxn];

void iscopy(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])iscopy(type,++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])iscopy(type,++cnt2,0);
       iscopy(type,++cnt2,i);
    }
    if(type =='R')
        r = cnt2;
    else
        c = cnt2;
}
int main(void)
{
    int r1,c1,r2,c2,q,kase = 0;
    mem(d,0);
    char cmd[10];
    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;
        while(n--)
        {
            scanf("%s",cmd);
            if(cmd[0] == 'E')
            {
                scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
                swap(d[r1][c1],d[r2][c2]);
            }
            else
            {
                int x, a; scanf("%d",&a);
                mem(cols,0);
                for(int i = 0;i<a;i++){ scanf("%d",&x); cols[x] = 1;}
                if(cmd[0] == 'D')
                    del(cmd[1]);
                else
                    ins(cmd[1]);
            }
        }
        mem(ans,0);
        for(int i = 1;i <= r;i++)
            for(int j = 1;j <= c;j++)
                ans[d[i][j]/BIG][d[i][j]%BIG] = i*BIG+j;
        if(kase > 0)printf("\n");
        printf("Spreadsheet #%d\n",++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\n");
            else printf("moved to (%d,%d)\n",ans[r1][c1]/BIG,ans[r1][c1]%BIG);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值