[UVA][字串] 10875 - Big Math
Problem F
Big Math
Input File: Standard Input
Output: Standard Output
The people at Awesome Color Monitors (ACM), Inc., makers of the GinormatronÔ -- those big scoreboard monitors used at most sports stadiums, are looking for new uses for their products. One of the areas they are most interested in supporting is education in children. To this end, they would like to use their scoreboards to teach simple mathematics to large groups of youngsters.
Once the stadium is filled with children, the scoreboard will display a simple mathematical equation, and then, after a brief pause, print the answer underneath. Your job is to read in such equations, perform the calculations, and print the result in "scoreboard" format.
The standard precedence and associatively of mathematical operators apply, so '*' and '/' have precedence over '+' and '-', and equations are performed left to right. Each number or operator is displayed in a 3 x 5 array of pixels. Each number or operator is separated by one space. You can assume that:
§ all equations are well formed
§ there are no divide-by-zero errors
§ all numbers are positive integers
§ operators appear as binary operators only
§ division means integer division
§ the intermediate and final results will fit in a 32-bit signed integer.
The numbers and operators appear in the input as follows (without the labels underneath):
000 .0. 000 000 0.0 000 0.. 000 000 000 .0. ... 0.0 .0.
0.0 .0. ..0 ..0 0.0 0.. 0.. ..0 0.0 0.0 .0. ... 0.0 ...
0.0 .0. 000 000 000 000 000 ..0 000 000 000 000 .0. 000
0.0 .0. 0.. ..0 ..0 ..0 0.0 ..0 0.0 ..0 .0. ... 0.0 ...
000 .0. 000 000 ..0 000 000 ..0 000 ..0 .0. ... 0.0 .0.
0 1 2 3 4 5 6 7 8 9 + - * /
As an example, the equation "128 + 256" in scoreboard notation would be:
.0. 000 000 .0. 000 000 0..
.0. ..0 0.0 .0. ..0 0.. 0..
.0. 000 000 000 000 000 000
.0. 0.. 0.0 .0. 0.. ..0 0.0
.0. 000 000 .0. 000 000 000
The resulting answer, 384, in scoreboard form would be:
000 000 0.0
..0 0.0 0.0
000 000 000
..0 0.0 ..0
000 000 ..0
Input
The input consists of a sequence of equations in scoreboard notation. Each equation is contained on one scoreboard-notation line, followed by a blank line. The maximum number of scoreboard notation characters in one equation is 20. The end of input is denoted by a zero in scoreboard notation on a line by itself.
Output
For each equation, compute the resulting answer, and print your output in scoreboard notation, followed by a blank line.
Sample Input Output for Sample Input
.0. 000 .0. 0.0 .0. 000 .0. .0. 0.0 .0. 0.0 .0. 0.0 .0. .0. 0.0 .0. .0. .0. 0.0 .0. .0. 0.0 .0. 0.0 .0. 0.0 .0. .0. 000 .0. 0.0 .0. 000 .0.
000 000 0.0 0.0 .0. 0.0 000 ..0 ..0 0.0 0.0 .0. 0.0 ..0 000 000 .0. 000 .0. .0. 000 0.. ..0 0.0 ..0 .0. 0.0 ..0 000 000 0.0 ..0 .0. 0.0 000
000 0.0 0.0 0.0 000
|
.0. 000 000 000 .0. .0. 0.0 ..0 0.0 .0. .0. 0.0 000 0.0 .0. .0. 0.0 0.. 0.0 .0. .0. 000 000 000 .0.
000 000 000 000 ..0 0.0 ..0 0.0 000 000 000 000 0.. 0.0 0.. ..0 000 000 000 ..0
|
Problem setter: Robert W. Lindeman
Moderator: Monirul Hasan
Special Thanks: Shahriar Manzoor
將輸入轉換成一般 char array, 只有一般的 +-*/, 不會存在單一負數。
但是答案有沒有負數不確定,因此特別處理一下。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char g[14][5][4] = {
{"000",
"0.0",
"0.0",
"0.0",
"000"},
{".0.",
".0.",
".0.",
".0.",
".0."},
{"000",
"..0",
"000",
"0..",
"000"},
{"000",
"..0",
"000",
"..0",
"000"},
{"0.0",
"0.0",
"000",
"..0",
"..0"},
{"000",
"0..",
"000",
"..0",
"000"},
{"0..",
"0..",
"000",
"0.0",
"000"},
{"000",
"..0",
"..0",
"..0",
"..0"},
{"000",
"0.0",
"000",
"0.0",
"000"},
{"000",
"0.0",
"000",
"..0",
"..0"},
{".0.",
".0.",
"000",
".0.",
".0."},
{"...",
"...",
"000",
"...",
"..."},
{"0.0",
"0.0",
".0.",
"0.0",
"0.0"},
{".0.",
"...",
"000",
"...",
".0."}
};
char gval[] = "0123456789+-*/";
int main() {
int i, j, k, p, q, r;
char input[6][1024];
while(true) {
gets(input[0]), gets(input[1]);
gets(input[2]), gets(input[3]);
gets(input[4]), gets(input[5]);
int len = strlen(input[0]);
char s[1024];
int n = 0;
for(i = 0; i < len; i += 4) {
for(j = 0; j < 14; j++) {
int ok = 1;
for(p = 0; p < 5; p++) {
for(q = 0; q < 3; q++) {
if(input[p][i+q] != g[j][p][q])
ok = 0, p = 5, q = 3;
}
}
if(ok == 1) {
s[n++] = gval[j];
break;
}
}
}
s[n] = '\0';
if(!strcmp(s, "0"))
break;
char prevop = '+';//+-*/
long long buf[1005] = {}, bidx = 1;
for(i = 0; i < n; i++) {
if(s[i] >= '0' && s[i] <= '9') {
long long val = 0;
while(s[i] >= '0' && s[i] <= '9') {
val = val*10 + s[i]-'0';
i++;
}
switch(prevop) {
case '+':buf[bidx++] = val;break;
case '-':buf[bidx++] = -val;break;
case '*':buf[bidx-1] *= val;break;
case '/':buf[bidx-1] /= val;break;
}
prevop = s[i];
} else
prevop = s[i], bidx++;
}
long long sum = 0;
for(i = 0; i < bidx; i++)
sum += buf[i];
sprintf(s, "%lld", sum);
for(i = 0; i < 5; i++) {
for(j = 0; s[j]; j++) {
if(j) putchar(' ');
if(s[j] >= '0' && s[j] <= '9')
printf("%s", g[s[j]-'0'][i]);
else if(s[j] == '-')
printf("%s", g[11][i]);
}
puts("");
}
puts("");
}
return 0;
}
/*
.0. 000 .0. 0.0 .0. 000 .0.
.0. 0.0 .0. 0.0 .0. 0.0 .0.
.0. 0.0 .0. .0. .0. 0.0 .0.
.0. 0.0 .0. 0.0 .0. 0.0 .0.
.0. 000 .0. 0.0 .0. 000 .0.
000 000 0.0 0.0 .0. 0.0 000
..0 ..0 0.0 0.0 .0. 0.0 ..0
000 000 .0. 000 .0. .0. 000
0.. ..0 0.0 ..0 .0. 0.0 ..0
000 000 0.0 ..0 .0. 0.0 000
000
0.0
0.0
0.0
000
*/