Gym - 100803B Miscalculation 解题

                                              Problem B

                          Miscalculation

                                              Input: Standard Input

                                              Time Limit: 1 second
Bob is an elementary schoolboy, not so good at mathematics. He found Father’s calculator and tried cheating on his homework using it. His homework was calculating given expressions containing multiplications and additions. Multiplications should be done prior to additions, of course, but the calculator evaluates the expression from left to right, neglecting the operator precedence. So his answers may be the result of either of the following two calculation rules.
? Doing multiplication before addition ? Doing calculation from left to right neglecting the operator precedence
Write a program that tells which of the rules is applied from an expression and his answer.
An expression consists of integers and operators. All the integers have only one digit, from 0 to 9. There are two kinds of operators + and *, which represent addition and multiplication, respectively.
The following is an example expression.
1+2*3+4
Calculating this expression with the multiplication-?rst rule, the answer is 11, as in Sample Input 1. With the left-to-right rule, however, the answer will be 13 as shown in Sample Input 2.
There may be cases in which both rules lead to the same result and you cannot tell which of the rules is applied. Moreover, Bob sometimes commits miscalculations. When neither rules would result in Bob’s answer, it is clear that he actually did.
Input
The input consists of a single test case speci?ed with two lines. The ?rst line contains the expression to be calculated. The number of characters of the expression is always odd and less than or equal to 17. Each of the odd-numbered characters in the expression is a digit from ‘0’ to ‘9’. Each of the even-numbered characters is an operator ‘+’ or ‘*’. The second line contains an integer which ranges from 0 to 999999999, inclusive. This integer represents Bob’s answer for the expression given in the ?rst line.
Output
Output one of the following four characters:
M When only the multiplication-?rst rule results Bob’s answer.
L When only the left-to-right rule results Bob’s answer.
U When both of the rules result Bob’s answer.
I When neither of the rules results Bob’s answer.
Sample Input 1 Sample Output 1
1+2*3+4 11
M
Sample Input 2 Sample Output 2
1+2*3+4 13
L
Sample Input 3 Sample Output 3
3 3
U
Sample Input 4 Sample Output 4
1+2*3+4 9
I

这道题有俩种解法,第一个可以用数组,第二种可以用栈,其实思路都差不多的

第一种  数组

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char a[100];
 6     int b[100],c[100];
 7     int n,m,x,y=0,i;
 8     scanf("%s",a);
 9     scanf("%d",&n);
10     for(i=0;i<strlen(a);i+=2)
11     {
12         b[i]=a[i]-‘0‘;
13         c[i]=a[i]-‘0‘;
14         //printf("%d\n",b[i]);
15     }
16     for(i=1;i<strlen(a);i+=2)
17     {
18         if(a[i]==‘+‘)
19         {
20             b[i+1]=b[i-1]+b[i+1];
21         }
22         if(a[i]==‘*‘)
23         {
24             b[i+1]=b[i-1]*b[i+1];
25         }
26         //printf("%d\n",b[i+1]);
27
28     }
29     //printf("%d\n",i);
30     x=b[i-1];
31     //printf("%d\n",x);
32     for(i=1;i<strlen(a);i+=2)
33     {
34         if(a[i]==‘*‘)
35         {
36             c[i+1]=c[i-1]*c[i+1];
37             //printf("%d\n",c[i+1]);
38             c[i-1]=0;
39         }
40     }
41     for(i=0;i<strlen(a);i+=2)
42     {
43             y+=c[i];
44             //printf("%d\n",y);
45     }
46     if(n==x&&n==y)
47     {
48         printf("U\n");
49         return 0;
50     }
51     if(n==x)
52     {
53         printf("L\n");
54         return 0;
55     }
56     if(n==y)
57     {
58         printf("M\n");
59         return 0;
60     }
61     if(n!=x&&n!=y)
62     {
63         printf("I\n");
64         return 0;
65     }
66  } 

第二种 栈

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<map>
 9 #include<stack>
10
11 using namespace std;
12
13 int main() {
14     char a[20];
15     while (~scanf("%s",a)){
16         getchar();
17         long long  n;
18         scanf("%lld",&n);
19         getchar();
20
21         if(strlen(a)==1){
22             if(a[0]==n+48)
23                 cout<<"U"<<endl;
24             else
25                 cout<<"I"<<endl;
26             continue;
27         }
28
29         long long l = 0;
30
31         l+=a[0]-48;
32         for(int i = 1;i<strlen(a);i++){
33             if(a[i] == ‘+‘){
34                 i++;
35                 l+= a[i]-48;
36             }
37             else if(a[i] == ‘*‘){
38                 i++;
39                 l *= a[i]-48;
40             }
41         }
42
43
44         long long m = 0;
45         stack<long long>s;
46         stack<char>f;
47         f.push(‘+‘);
48         for(int i = 0;i<strlen(a);i++){
49             if(a[i]==‘+‘ || a[i]==‘*‘){
50                 f.push(a[i]);
51             }
52             else{
53                 s.push((long long)a[i]-48);
54             }
55         }
56
57         while (!f.empty()){
58             if(f.top() == ‘+‘){
59                 m+=s.top();
60                 s.pop();
61                 f.pop();
62             }
63             else{
64                 long long x1 = s.top();
65                 s.pop();
66                 long long x2 = s.top();
67                 s.pop();
68                 s.push((x1)*(x2));
69                 f.pop();
70             }
71         }
72
73
74         if(m==n && l==n){
75             cout<<"U"<<endl;
76         }
77         else if(m==n){
78             cout<<"M"<<endl;
79         }
80         else if(l==n){
81             cout<<"L"<<endl;
82         }
83         else{
84             cout<<"I"<<endl;
85         }
86     }
87     return 0;
88 }
时间: 2024-10-12 13:56:33

Gym - 100803B Miscalculation 解题的相关文章

ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Practice Description standard input/output At the ruins of Wat Phra Si Sanphet (??????????????), one can find famous inscri

2016&quot;百度之星&quot; - 初赛(Astar Round2A)解题报告

此文章可以使用目录功能哟↑(点击上方[+]) 有点智商捉急,第一题卡了好久,看来不服老,不服笨是不行的了...以下是本人目前的题解,有什么疑问欢迎提出 链接→2016"百度之星" - 初赛(Astar Round2A)  Problem 1001 All X Accept: 0    Submit: 0 Time Limit: 2000/1000 mSec(Java/Others)    Memory Limit : 65536 KB  Problem Description F(x,

HDU 5695 ——Gym Class——————【贪心思想,拓扑排序】

Gym Class Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 768    Accepted Submission(s): 309 Problem Description 众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有

Gym - 101982B Coprime Integers (莫比乌斯反演)

题目链接:http://codeforces.com/gym/101982/attachments 题目大意:有区间[a,b]和区间[c,d],求gcd(x,y)=1,其中x属于[a,b],y属于[c,d],求这样的x,y有多少对. 解题思路: 第一种反演思路: 把下界变换一下 代码: #include<iostream> #include<cstdio> using namespace std; typedef long long ll; const int maxn=1e7+7

Gym - 101194H Great Cells

题目链接:https://codeforces.com/gym/101194/attachments Problem H. Great Cells Input file: Standard Input Output file: Standard Ouptut Time limit: 2 seconds 题目大意: 在N×M的网格里填[1,K]的整数,如果满足这个格子中的数是本行和本列中严格的最大值,定义这个格子是great的.定义A-g为网格中恰好有g个great格子的填法数,求Σ(g+1)A-

6081: Gym Class(拓扑排序+优先队列)

6081: Gym Class 时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte 总提交: 40            测试通过:10 描述 众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有一个唯一的ID,从1到N,在排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为自己评价这堂课的分数.麻烦的是,有一些同学不希望某个(

CodeForces Gym 100935D Enormous Carpet 快速幂取模

Enormous Carpet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935D Description standard input/outputStatements Ameer is an upcoming and pretty talented problem solver who loves to solve problems using computers.

解题报告 之 POJ3057 Evacuation

解题报告 之 POJ3057 Evacuation Description Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time

LeetCode Plus One Java版解题报告

https://oj.leetcode.com/problems/plus-one/ 题意:一个整数按位存储于一个int数组中,排列顺序为:最高位在array[0] ,最低位在[n-1],例如:98,存储为:array[0]=9; array[1]=8; 解题思路,从数组的最后一位开始加1,需要考虑进位,如果到[0]位之后仍然有进位存在,需要新开一个长度为(n.length + 1)的数组,拷贝原来的数组. public class Solution { public int[] plusOne