Magic Trick(水题)

xiaoxiao2021-02-28  20

Description

Your friend has come up with a math trick that supposedly will blow your mind. Intrigued, youask your friend to explain the trick.

First, you generate a random positive integer k between 1 and 100. Then, your friend will giveyou n operations to execute. An operation consists of one of the four arithmetic operations ADD,SUBTRACT, MULTIPLY, or DIVIDE, along with an integer-valued operand x. You are supposed toperform the requested operations in order.

You don’t like dealing with fractions or negative numbers though, so if during the process, theoperations generate a fraction or a negative number, you will tell your friend that he messed up.

Now, you know the n operations your friend will give. How many of the first 100 positiveintegers will cause your friend to mess up?

Input

The first line of input contains a single positive integer n (1 ≤ n ≤ 10). Each of the next n linesconsists of an operation, followed by an operand. The operation is one of the strings ADD, SUBTRACT,MULTIPLY, or DIVIDE. Operands are positive integes not exceeding 5.

Output

Print, on a single line, a single integer indicating how many of the first 100 positive integers willresult in you telling your friend that he messed up.

Sample Input

1

SUBTRACT 5

Sample Output

4

Sample Input

1

DIVIDE 2

Sample Output

50

Sample Input

2

ADD 5

DIVIDE 5

Sample Output

80

题解:当时做的时候没理解题意,一直w,后来一看,真简单......,就是每一步执行都把小数和负数排除,最后求和。

代码如下:

#include <iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<stack> #include<queue> #include<deque> using namespace std; int main() { int t,b,count=0,c[200]; char a[20]; cin>>t; for(int i=1; i<=100; i++) c[i]=i; while(t--) { cin>>a>>b; if(a[0]=='A') for(int i=1; i<=100; i++) if(c[i]!=-0x3f3f3f3f) c[i]+=b; if(a[0]=='S') for(int i=1; i<=100; i++) if(c[i]!=-0x3f3f3f3f) c[i]-=b; if(a[0]=='M') for(int i=1; i<=100; i++) if(c[i]!=-0x3f3f3f3f) c[i]*=b; if(a[0]=='D') for(int i=1; i<=100; i++) if(c[i]!=-0x3f3f3f3f) { if(c[i]%b==0) c[i]/=b; else c[i]=-99999; } for(int i=1; i<=100; i++) if(c[i]<0&&c[i]!=-0x3f3f3f3f) { c[i]=-0x3f3f3f3f; count++; } } printf("%d\n",count); return 0; }

转载请注明原文地址: https://www.6miu.com/read-2450364.html

最新回复(0)