Magic Trick(水题)

本文介绍了一个基于数学魔术的编程挑战,任务是通过一系列算术操作判断哪些初始整数会导致过程出现分数或负数,最终统计数量。代码使用C++实现,逐个检查1到100之间的整数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值