HDU 6043 KazaQ's Socks 思维题

  题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6043

  题目大意: 有n个袜子从1到n编号, 每天都要穿干净的袜子中的标号最小的那个, 脏的袜子放进框里, 框里面有n-1个袜子时拿去清洗, 第二天又变成干净的袜子可以重新穿。问第K天穿的袜子的标号是多少?

  解题思路: 一道思维题, 顺次写出要穿的袜子的标号就可以找出循环节, 然后编程。

  代码: 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>

using namespace std;

typedef long long ll;
ll n, k;
int cases = 0;
int main() {
    while( cin >> n >> k ) {
        ll ans = -1;
        if( k <= n ) {
            ans = k;
        }
        else if( n == 2 ) {
            ans = k & 1 ? 1 : 2;
        }
        else {
            ll temp = k - n;
            ll temp1 = temp;
            temp %= (n-1);
            ans = temp;
            if( temp == 0 ) {
                if( (temp1 / (n-1)) & 1 ) {
                    ans = n-1;
                }
                else {
                    ans = n;
                }
            }
            
        }
        cout << "Case #" << ++cases << ": " << ans << endl;
    }
    return 0;
}
View Code

  思考: 多思考, 多动脑。

posted on 2017-07-25 20:22  FriskyPuppy  阅读(204)  评论(0编辑  收藏  举报

导航