UVA 10940 Throwing cards away II

题意略;

先暴力打表发现规律

N=1 ans=1
N=2 ans=2
N=3 ans=2
N=4 ans=4
N=5 ans=2
N=6 ans=4
N=7 ans=6
N=8 ans=8
N=9 ans=2
N=10 ans=4
N=11 ans=6
N=12 ans=8
N=13 ans=10
N=14 ans=12
N=15 ans=14
N=16 ans=16
N=17 ans=2
N=18 ans=4
N=19 ans=6
N=20 ans=8
N=21 ans=10
N=22 ans=12
N=23 ans=14
N=24 ans=16
N=25 ans=18
N=26 ans=20
N=27 ans=22
N=28 ans=24
N=29 ans=26
N=30 ans=28
N=31 ans=30
N=32 ans=32

这规律很明显了就略了。。直接上代码了

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
#define MAXN 500003
vector<int>tab[20];
int num[20];
int N;
void init()
{
    for (int i=0;i<20;i++) tab[i].clear();
    tab[0].push_back(1);
    for (int i=1;i<20;i++)
    {
        int limit=1<<i;
        int cas=2;
        while (cas<=limit)
        {
            tab[i].push_back(cas);
            cas+=2;
        }
    }
    num[0]=1;
    for (int i=1;i<20;i++) num[i]=num[i-1]+tab[i].size();
}
int main()
{
    init();
    while (scanf("%d",&N)!=EOF)
    {
        if (N==0) break;
        if (N==1) {puts("1");continue;}
        int cas=0,i;
        for ( i=0;i<20;i++)
        {
            cas+=tab[i].size();
            if (cas>=N) break;
        }
        int tmp=N-num[i-1];
        printf("%d\n",tab[i][tmp-1]);
    }
    return 0;
}
时间: 2024-08-10 02:00:15

UVA 10940 Throwing cards away II的相关文章

UVA10940 Throwing cards away II【数学规律+约瑟夫环】

Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move the card that is now on th

UVa 10935 - Throwing cards away I

模拟队列操作.  注意当n == 1时第一行输出末尾没有空格.PE一次~~~ 代码  : import java.util.*; public class Main10935 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Queue<Integer> q = new LinkedList<Integer>(); while(true) { int n = scan.

UVa 10935 Throwing cards away I【队列】

题意:给出 n张牌,从上往下编号依次为1到n,当牌的数目至少还剩下2张时,把第一张牌扔掉,然后把新的一张牌放在牌堆的最底部,问最后剩下的那一张牌是哪一张牌. 模拟队列的操作------- 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 usin

UVA10940 - Throwing cards away II(找规律)

题目链接 题目大意:桌上有n张牌,按照1-n的顺序从上到下,每次进行将第一张牌丢掉,然后把第二张放到这叠牌的最后.反复进行这样的操作,直到只剩下一张牌. 解题思路:只能先暴力,将前面小点的n打印出来,看看有什么规律. 规律:f[2^k + mod] = 2*mod;(mod > 0); n = 1需要特判. 代码: #include <cstdio> #include <cstring> const int maxn = 5e5 + 5; int f[maxn]; void

Throwing cards away I

Throwing cards away I   Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck:  Throw away the top card and move t

紫书第五章训练3 D - Throwing cards away I

D - Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move

UVA, 686 Goldbach&#39;s Conjecture (II)

题意:给你一个数,认为它能拆成两个素数之和的形式,有几组素数则输出几 例:10= 3+7 思路:打印素数表,判断x-prime[a]是否为素数即可 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 #define MAXN 1000001 7 #define ll 8 int cnt,counts; 9 long long num;

Throwing cards away I uva1594

 Throwing cards away I Given is an ordered deck of  n  cards numbered 1 to  n  with card 1 at the top and card  n  at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and

UVa---------10935(Throwing cards away I)

题目: Problem B: Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top car