1. 程式人生 > >UVa512追蹤電子表格中的單元格詳解

UVa512追蹤電子表格中的單元格詳解

stdlib.h else body class HR 刪除列 ont include 變化

解題思路:

因為我們最終要查找的是一開始的那些單元格的最終的位置,所以每次變換後,我們只記錄更新這些單元格的位置

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"); }


技術分享圖片

UVa512追蹤電子表格中的單元格詳解