Hello, I'm a C++ beginner trying to solve this problem.
You have a spreadsheet and some cells are inserted with formula. A cell without formula has initial value.However, some cells have buggy formula. The formula of several cells might depend on each other and results in reference error. For example, cell A1 has formula B1 + 2, cell B1 has formula C1 + 2, cell C1 has formula A1 + 2, then the spreadsheet reports reference error.If a cell has formula depending a cell with reference error, itself encounters reference error as well. For example, cell C1 has reference error, cell C4 has formula C1 + 2, then C4 has reference error.
Given a spreadsheet, please output results of all cells.
Input Format
The first line contains two integer : N,M the number of cells in the spreadsheet and the number of cells with formula.
The cells are labeled with 1,2,...N (instead of the usual way we see in spreadsheet applications).
The following M lines describes the formula, for each line is either:
A x1 y - Cell x1 has value y
B x1 x2 y - Cell x1 has formula: cell x2 + value y
...
Output Format
For each cell, please output "#REF!" if it has reference error; otherwise, output its value.
Sample Input
10 9
A 1 10
B 2 1 20
B 3 2 30
B 4 6 2
B 5 4 2
B 6 5 2
C 7 1 2 2
C 8 7 6 3
D 9 1 2 3 8
Sample Output
10
30
60
#REF!
#REF!
#REF!
42
#REF!
108
0
Explanation 0
The formula and result of each cell is listed below:
cell id formula result
1 10 10
2 cell 1 + 20 30
3 cell 2 + 30 60
4 cell 6 + 2 reference error
5 cell 4 + 2 reference error
6 cell 5 + 2 reference error
7 cell 1 + cell 2 + 2 42
8 cell 7 + cell 6 + 3 reference error
9 cell 1 + cell 2 + cell 3 + 8 108
10 (empty cell) 0
Sorry the problem is quite long.
I'd like to solve it step by step, first, how to implement the cin that got three items in a line, and, is there any hint on solving this problem efficiently? Appreciate anyone's help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N,M;
cin>>N>>M;
while(M--)
return 0;
}
|