UVA 10940 Throwing cards away II

本文通过暴力打表的方法发现了特定数学序列的规律,并提供了一段C++代码实现。该序列从N=1开始,其答案呈现出周期性的增长趋势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意略;

先暴力打表发现规律

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;
}

 

转载于:https://www.cnblogs.com/Commence/p/3980249.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值