NYOJ 1013 除法表达式

来源:互联网 发布:java小软件源代码 编辑:程序博客网 时间:2024/05/22 00:43

经典的除法表达式题目
参见《算法竞赛入门经典(第二版)》P310~311

#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <sstream>using namespace std;const int MAXK = 10000;void solve(){  int a[MAXK];  string s;  getline(cin, s);  int k = 0;  for (;;) {    size_t found = s.find("/");    if (found == string::npos) {      stringstream ss(s);      ss >> a[k++];      break;    }    string substring = s.substr(0, found);    //cout << substring << endl;    stringstream ss(substring);    ss >> a[k++];    s.erase(0, found + 1);  }  a[1] /= __gcd(a[1], a[0]);  for (int i = 2; i < k; i++) {    a[1] /= __gcd(a[1], a[i]);  }  if (a[1] == 1) puts("YES"); else puts("NO");}int main(){  //freopen("in.txt", "r", stdin);  int t;  scanf("%d\n", &t);  while (t--) {    solve();  }}
0 0