Kattis <Trik>

此文章可以使用目录功能哟↑(点击上方[+])

 Kattis <Trik>

Accept: 0    Submit: 0
Time Limit: 1 second    Memory Limit : 1024 MB

 Problem Description

Jealous of Mirko’s position as head of the village, Borko stormed into his tent and tried to demonstrate Mirko’s incompetence for leadership with a trick.

Borko puts three opaque cups onto the table next to each other (opening facing down) and a small ball under the leftmost cup. He then swaps two cups in one of three possible ways a number of times. Mirko has to tell which cup the ball ends up under.


Wise Mirko grins with his arms crossed while Borko struggles to move the cups faster and faster. What Borko does not know is that programmers in the back are recording all his moves and will use a simple program to determine where the ball is. Write that program.

 Input

The first and only line contains a non-empty string of at most 50 characters, Borko’s moves. Each of the characters is ‘A’, ‘B’ or ‘C’ (without quote marks).

 Output

Output the index of the cup under which the ball is: 1 if it is under the left cup, 2 if it is under the middle cup or 3 if it is under the right cup.

 Sample Input

AB
CBABCACCC

 Sample Output

3
1

 Problem Idea

解题思路:

【题意】

桌上倒扣着三个不透明的杯子

最初,最左边的杯子内有一个小球

问经过一系列的位置交换,最终小球在哪个杯子内

交换过程用字符串表示,'A'表示左边的杯子与中间的杯子交换;'B'表示中间的杯子与右边的杯子交换;'C'表示左边的杯子与右边的杯子交换

【类型】
模拟

【分析】

由于字符串长度最大为50,意味着交换次数最多50次

那我们可以直接进行模拟

以样例1为例:




所以,小球最终在右边的杯子内,故输出"3"

【时间复杂度&&优化】
O(strlen(s))

题目链接→Kattis <Trik>

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 55;
const int M = 100005;
const int inf = 1000000007;
const int mod = 10007;
char s[N];
int main()
{
    int a=1,b=0,c=0,i;
    scanf("%s",s);
    for(i=0;s[i]!='\0';i++)
        if(s[i]=='A')
            swap(a,b);
        else if(s[i]=='B')
            swap(b,c);
        else
            swap(a,c);
    if(a)
        puts("1");
    else if(b)
        puts("2");
    else
        puts("3");
    return 0;
}
菜鸟成长记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值