UVa512 追蹤電子表格中的單元格 例題4-5(P85)

題目地址

注釋后的代碼

//uva512 追蹤電子表格中的單元格 
// 2017.1.25
#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;         //新的行數(shù)或者列數(shù) 
}

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列移動到增加的位置,所以增加的空位置內(nèi)填入的都是0 
  }
  if(type == 'R') r = cnt2; else c = cnt2;           //新的行數(shù)或者列數(shù)  
}

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++) {
        ans[d[i][j]/BIG][d[i][j]%BIG] = i*BIG+j;              //ans存儲經(jīng)過增減調(diào)換后的數(shù)組,ans的內(nèi)容為變化后的數(shù)組d的坐標 
      }
    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);            //查找該數(shù)在變化后的數(shù)組中的位置,輸出坐標 
    }
  }
  return 0;
}
uva512-a.png
  • 感想
    1.表格中的內(nèi)容為”行000列“這樣的五位數(shù)方便統(tǒng)計坐標。
    橫標:數(shù)/10000
    縱標:數(shù)%10000
    2.新數(shù)組
    ans[d[i][j]/BIG][d[i][j]%BIG]=i*BIG+j
    保存的是變化后的數(shù)組
    3.插入時 cnt2多加一次 i正常遞增
    刪除時 cnt2少加一次 i正常遞增
    cnt2被i覆蓋
    4.輸出坐標時from(c1,r1) move to (ans[c1][r1]/BIG,ans[c1][r1]%BIG)
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容