KazaQ's Socks

描述

KazaQ wearssocks everyday. 

At the beginning, he has n pairs of socks numbered from 1 to nn in his closets. 

Every morning, he puts on a pair of socks which has the smallest number in theclosets. 

Every evening, he puts this pair of socks in the basket. If there are n−1 pairs of socks in the basket now,lazy KazaQ has to wash them. These socks will be put in theclosets again in tomorrow evening. 

KazaQ would like to know which pair of socks he should wear on the k-th day.

输入

The input consists of multiple test cases. (about 2000) 

For each case, there is a line contains two numbers n,k (2≤n≤109,1≤k≤1018)

输出

For each test case, output " Case #x:  y" in one line (without quotes), where xxindicates the case number starting from 1 and y denotes theanswer of corresponding case.

样例输入

3 7

3 6 

4 9

样例输出

Case #1: 3

Case #2: 1 

Case #3: 2


题意:

  有n双袜子,从1到n标记,依次放在柜子里,每天早上起床穿袜子,选标号最小的一双。然后晚上回来将穿过的扔一个地方。当那个地方里的袜子数量达到n-1双的时候,就把这些袜子洗一下,等到现在的袜子洗的时候就可以穿了。问在第K天穿的是哪一个标号的袜子。

思路:

     模拟一下,就可以了,找到一个规律。

模拟如下:

 n = 5,k = 6;

                                                             

第一天: 1 

,第二天: 2

,第三天: 3 

,第四天: 4        达到n-1双(1 2 3)

,第五天: 1 

,第六天: 2

,第七天: 3        达到n-1双(4 1 2)

,第八天: 1 

,第九天: 2 

,第十天: 4      达到n-1双(1 2 3)

..........

然后:(1 2 3 4) (1 2 3) (1 2 4) (1 2 3)(1 2 4)

规律就是这样,具体见代码。


代码如下:

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int  t=1;
    long long int m,n;
    while(scanf("%lld%lld",&n,&m)!=EOF)
    {
        ///前面的单个
        if(m<=n) printf("Case #%d: %lld\n",t++,m);
        else
        {
            ///减去1到n双袜子
            m = m-n;
            ///循环长度为 n-1
            long long int k = m%(n-1);
            if(k!=0)
            {
                ///代表着是前面1到n-2之间的数字,直接输出
                printf("Case #%d: %lld\n",t++,k);
            }
            else
            {
                ///判断是n-1,还是n结尾的循环节
                ///刚开始时n-1结尾,
                if(m/(n-1)%2==0)
                    printf("Case #%d: %lld\n",t++,n);
                else
                    printf("Case #%d: %lld\n",t++,n-1);
            }
        }
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值