【数论 - 除法表达式】

除法表达式 分享至QQ空间 去爱问答提问或回答

Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte
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

TOJ

Uploader

crq

 

 

 

 

 

 

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 

 

转载于:https://www.cnblogs.com/ismdeep/archive/2012/07/12/2588446.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值