除法表达式
![去爱问答提问或回答](http://acm.tzc.edu.cn/acmhome/forum/images/iqa.gif)
Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByte
Total Submit: 279 Accepted: 68
Total Submit: 279 Accepted: 68
Description
给出如下除法表达式E:
X1/X2/X3/..../Xk
其中Xi是正整数并且Xi<=2 000 000 000(1<=i<=k,k<=10 000)。除法表达式应当按照从左到右的顺序求结果,例如
表达式1/2/1/2的值是1/4。现在让你可以在表达E中嵌入括号以改变计算顺序,例如表达式(1/2)/(1/2)的值是1。现在给你
一个除法表达式E,要求告诉是否能够通过加括号(或者不加)得到表达式E' ,E'的值为整数。
Input
输入数据包括多组数据,每组数据占一行,给出的是题目描述的表达式E,E中不含空格。
Output
每个测试数据占一行如果能找到题目中描述的E' 则输出"YES"(不含引号),否则输出"NO" (不含引号)。
Sample Input
1/2/1/2
2/3
Sample Output
YES
NO
Source
Uploader
http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1537
1 // Project name : 除法表达式 2 // File name : main.cpp 3 // Author : Izumu 4 // Date & Time : Thu Jul 12 13:34:50 2012 5 6 7 #include <iostream> 8 #include <stdio.h> 9 #include <string> 10 #include <cmath> 11 #include <algorithm> 12 using namespace std; 13 14 #define MAXN 11000 15 16 typedef unsigned long long int int64; 17 18 int64 gcd(int64 a, int64 b) 19 { 20 int tmp; 21 while (b) 22 { 23 int tmp; 24 tmp = a % b; 25 a = b; 26 b = tmp; 27 } 28 29 return a; 30 } 31 32 void ism() 33 { 34 cout << "--ism" << endl; 35 } 36 int num[MAXN]; 37 38 int main() 39 { 40 string s; 41 while (cin >> s) 42 { 43 // store numbers into array of num[] 44 int top = -1; 45 int tmp = 0; 46 int current = 0; 47 while (s[current] != '\0') 48 { 49 if (s[current] == '/') 50 { 51 top++; 52 num[top] = tmp; 53 tmp = 0; 54 } 55 else 56 { 57 tmp = tmp * 10 + (s[current] - '0'); 58 } 59 current++; 60 } 61 top++; 62 num[top] = tmp; 63 64 // start process 65 num[1] /= gcd(num[0], num[1]); 66 67 for (int i = 2; i <= top; i++) 68 { 69 num[1] /= gcd(num[1], num[i]); 70 } 71 if (top == 0) 72 { 73 cout << "YES" << endl; 74 } 75 else 76 { 77 if (num[1] == 1) 78 { 79 cout << "YES" << endl; 80 } 81 else 82 { 83 cout << "NO" << endl; 84 } 85 } 86 } 87 88 return 0; 89 } 90 91 // end 92 // ism