解题思路:
因为我们最终要查找的是一开始的那些单元格的最终的位置,所以每次变换后,我们只记录更新这些单元格的位置
1、注意的地方:如:实施删除操作时,如果删除多行,指的是在同一个表格上同时进行删除这几行
2、比如说删除行操作,为了减少存储,输入一个删除第几行时,对每个单元格应当变化的数量进行更改并将要删除的该行的单元格的参数进行调整
,输入最后一个删除第几行时,将所有单元格对应的位置根据其变化数进行更改
该题起笔比较草率,所以很多细节没有考虑到,以至于后面debug很多次
#include<iostream> #include<String> #include<stdlib.h> using namespace std; struct { int dx;//记录每次变换后的坐标信息 int dy; int n;//记录每种操作过后坐标改变的数量值 }cell[51][51]; int M, N; void init(int m, int n) {//初始化结构体数组 for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { cell[i][j].dx = i; cell[i][j].dy = j; cell[i][j].n = 0; } } } void IR(int m, int n) {//插入行操作,记录下每个元素行数的变化值,在最后一次遍历时做加减法 int num; cin >> num;//记录插入的行数 while (num--) { M++; int R; cin >> R;//在第R行前插入一行 for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (cell[i][j].dx >= R) cell[i][j].n++; if (num == 0) { cell[i][j].dx += cell[i][j].n; cell[i][j].n = 0; } } } } } void DR(int m, int n) {//删除行操作 int num; cin >> num;//记录总共删除的行数 while (num--) { M--; int R; cin >> R;//删除第R行 for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (cell[i][j].dx > R) cell[i][j].n++; if (cell[i][j].dx == R) {//如果该位置被删除把dx,dy,n置0, cell[i][j].dx = cell[i][j].dy = 0; cell[i][j].n = 0; } if (num == 0) {//最后一次输入删除的行数时,计算出每个位置的最终值,并把n置零 cell[i][j].dx -= cell[i][j].n; cell[i][j].n = 0; } } } } } void IC(int m, int n) {//插入列操作 int num; cin >> num; while (num--) { N++; int C; cin >> C; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (cell[i][j].dy >= C) cell[i][j].n++; if (num == 0) { cell[i][j].dy += cell[i][j].n; cell[i][j].n = 0; } } } } } void DC(int m, int n) {//删除列操作 int num; cin >> num; while (num--) { N--; int C; cin >> C; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (cell[i][j].dy > C) cell[i][j].n++; if (cell[i][j].dy == C) { cell[i][j].dx = cell[i][j].dy = 0; cell[i][j].n = 0; } if (num == 0) { cell[i][j].dy -= cell[i][j].n; cell[i][j].n = 0; } } } } } void EX(int m, int n) {//交换操作 int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;//要交换的两个位置的坐标值 if (x1 <= M && x2 <= M && y1 <= N && y2 <= N)//确保坐标值合法 { for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) {//遍历找出该位置并改变其位置值 if (cell[i][j].dy == y1 && cell[i][j].dx == x1) { cell[i][j].dy = y2; cell[i][j].dx = x2; continue; } if (cell[i][j].dy == y2 && cell[i][j].dx == x2) { cell[i][j].dy = y1; cell[i][j].dx = x1; } } } } } int main() { int m, n;//m,n分别记录表格的初始行数和列数 int num = 1;//记录第几个表格 while (cin >> m >> n && m != 0 && n != 0) { M = m; N = n; init(m, n); int s1;//记录操作的种类数 cin >> s1; while (s1--) { string a; char b[4]; cin >> b; a = b; if (a == "DR") DR(m, n); else if (a == "DC") DC(m, n); else if (a == "IC") IC(m, n); else if (a == "IR") IR(m, n); else EX(m, n); } int s2; cin >> s2;//记录对该表查询d次数 cout << "Spreadsheet #" << num << endl;//表示第几个表 num++; while (s2--) {//进行查询 int x, y; cin >> x >> y; if (cell[x][y].dx == 0 && cell[x][y].dy == 0) { printf("Cell data in (%d, %d) GONE\n", x, y); } else { printf("Cell data in (%d, %d) moved to (%d, %d)\n", x, y, cell[x][y].dx, cell[x][y].dy); } } } system("pause"); }
原文地址:https://www.cnblogs.com/denghui666/p/8641682.html
时间: 2024-10-19 14:49:06