A-B Game

Think:
这题 也是很醉啊!!! 不知为何 用C的scanf就是WA 改成用C++的 Cin 就很神奇的AC了~~~~~~~~~ WA了14发2333333 然后 改着改着就神奇的对了~~~~~~~
Problem Description

Fat brother and Maze are playing a kind of special (hentai) game by two integers A and B. First Fat brother write an integer A on a white paper and then Maze start to change this integer. Every time Maze can select an integer x between 1 and A-1 then change A into A-(A%x). The game ends when this integer is less than or equals to B. Here is the problem, at least how many times Maze needs to perform to end this special (hentai) game.
Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers A and B described above.

1 <= T <=100, 2 <= B < A < 100861008610086
Output

For each case, output the case number first, and then output an integer describes the number of times Maze needs to perform. See the sample input and output for more details.
Sample Input
2
5 3
10086 110
Sample Output
Case 1: 1
Case 2: 7

以下是 WA 代码 很神奇的 WA 不懂错在哪儿~~~~~~

#include<stdio.h>
int main()
{
    long long int a, b;
    int cnt,kase;
    int T;
    scanf("%d", &T);
        kase  =  0;
        while(T --)
        {
            scanf("%lld %lld", &a, &b);
            cnt = 0;
            kase = kase + 1;
            while(a > b)
            {
                a = a / 2 + 1;
                cnt ++;
            }
            printf("Case %d: %d\n", kase, cnt);
        }
}

以下是 AC 代码 很神奇的改着改着 就AC了TAT

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    long long a, b;
    int kase = 0, cnt;
    int T;
    cin >> T;
    kase = 0;
    while(T --)
    {
        kase ++;
        cnt = 0;
        cin >> a >> b;
        while(a > b)
        {
            a = a / 2 + 1;
            cnt ++;
        }
        printf("Case %d: %d\n", kase, cnt);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<stdio.h> #include<stdlib.h> #include<sys/ipc.h> #include<sys/shm.h> #include<sys/sem.h> #include<string.h> typedef struct _test{ int a_val; int b_val; int a_flag; int b_flag; int game_no; int stage; }test; int pk[3][3] = {0,-1,1,1,0,-1,-1,1,0}; void sem_p(); void sem_v(); void set_sem(); void del_sem(); int sem_id; union semun{ int val; struct semid_ds *buf; unsigned short *arry; }; int main(){ int shmid; test* shm; shmid = shmget((key_t)1236,sizeof(test),0666|IPC_CREAT); if(shmid == -1){ printf("shmget failed\n"); exit(EXIT_FAILURE); } printf("%d",shmid); shm = shmat(shmid,0,0); if (shm == (void*)-1){ printf("shmat failed\n"); exit(EXIT_FAILURE); } printf("\nMemory attached at %X\n",(int)shm); sem_id = semget((key_t)3000,1,0666|IPC_CREAT); set_sem(); int no=0,debug=0,a,b; shm->a_flag=0; shm->a_val = -2; shm->b_flag=0; shm->b_val = -2; shm->game_no=1; shm->stage=0; while(1){ sem_p(); //printf("a:%d b:%d\n",shm->a_val,shm->b_val); sleep(1); if(shm->game_no==-1){ sem_v(); break; } if (shm->stage==0){ if(no!=shm->game_no){ no = shm->game_no; printf("-------------------\n"); printf("game_no:%d\n",no); } if(shm->a_flag==1 && shm->b_flag==1) shm->stage=1; } else if(shm->stage==1){ printf("a:%d\n",shm->a_val); printf("b:%d\n",shm->b_val); a = pk[shm->a_val][shm->b_val]; b = pk[shm->b_val][shm->a_val]; shm->a_val=a; shm->b_val=b; shm->a_flag=0; shm->b_flag=0; shm->stage=2; } else if(shm->stage==2){ if(shm->a_flag==1 && shm->b_flag==1){ shm->stage=0; shm->game_no++; shm->a_flag=0; shm->b_flag=0; printf("-------------------\n"); if(shm->game_no > 100) shm->game_no=-1; } } sem_v(); } shmdt(shm); int ret=0; ret = shmctl(shmid,IPC_RMID,NULL); if(ret<0){ printf("shmctl error!\n"); } del_sem(); printf("finish"); } void set_sem(){ union semun sem_union; sem_union.val=1; semctl(sem_id,0,SETVAL,sem_union); } void del_sem(){ union semun sem_union; semctl(sem_id,0,IPC_RMID,sem_union); } void sem_p(){ struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = -1; sem_b.sem_flg = SEM_UNDO; semop(sem_id,&sem_b,1); } void sem_v(){ struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = 1; sem_b.sem_flg = SEM_UNDO; semop(sem_id,&sem_b,1); }
最新发布
06-08

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值