列表

详情


OR167. 计数器

描述

小爱有一个奇怪的计数器。在第一个时刻计数器显示数字3,在接下来的每一个时刻,屏幕上的数字都会减1,直到减到1为止。

接下来,计数器会重置为上一个计数周期初始值的两倍,然后再每一个时刻减1。具体过程如下图所示:

找出规律,并打印出t时刻计数器的值。

输入描述

输入为时刻t,一个整形数字。0<t<1e12

输出描述

计数器显示的值。

示例1

输入:

4

输出:

6

原站题解

C 解法, 执行用时: 1ms, 内存消耗: 360KB, 提交时间: 2020-10-31

#include<stdio.h>


const unsigned long long K[] = {0,3,6,12,24,48,96,192,384,768,1536,3072,6144,12288,24576,49152,98304,196608,393216,
						786432,1572864,3145728,6291456,12582912,25165824,50331648,100663296,201326592,402653184,
						805306368,1610612736,3221225472,6442450944,12884901888,25769803776,51539607552,103079215104,
						206158430208,412316860416,824633720832,1649267441664,3298534883328,6597069766656,13194139533312,
						26388279066624,52776558133248,105553116266496,211106232532992,422212465065984,844424930131968,
						1688849860263936,3377699720527872,6755399441055744,13510798882111488,27021597764222976,
						54043195528445952,108086391056891904,216172782113783808,432345564227567616,864691128455135232,
						1729382256910270464,3458764513820540928,6917529027641081856 };
const unsigned long long sum[] = { 0,3,9,21,45,93,189,381,765,1533,3069,6141,12285,24573,49149,98301,196605,393213,786429,
                  1572861,3145725,6291453,12582909,25165821,50331645,100663293,201326589,402653181,805306365,1610612733,
	               3221225469,6442450941,12884901885,25769803773,51539607549,103079215101,206158430205,412316860413,
	               824633720829,1649267441661,3298534883325,6597069766653,13194139533309,26388279066621,52776558133245,
	              105553116266493,211106232532989,422212465065981,844424930131965,1688849860263933,3377699720527869,
	              6755399441055741,13510798882111485,27021597764222973,54043195528445949,108086391056891901,216172782113783805,
	              432345564227567613,864691128455135229,1729382256910270461,3458764513820540925,6917529027641081853 };

int main()
{
	unsigned int n = 0;
	unsigned long long num;
	unsigned int i = 0, j = 62;
	unsigned long long start = 0;
	unsigned long long index = 0;
	scanf("%lld", &num);

	for (n = 62; n >= 0; n--)
	{
		if (sum[n - 1] + 1 <= num )
		{
			break;
		}
	}
	start = sum[n - 1] + 1;
	index = num - start + 1;//当前所在的位置
	num = K[n] - index + 1;
	printf("%lld", num);
    return 0;
}

C 解法, 执行用时: 1ms, 内存消耗: 368KB, 提交时间: 2020-05-18

#include<stdio.h>
 
int main() {
    long int a, b, h;
     
    a = 1;
    b = 3;
 
    scanf("%ld", &h);
 
    while (a < h) {
        a += b;
        b *= 2;
    }
    if (a == h) {
        printf("%ld", b);
    }
    else {
        b = 0;
        while (a!=h) {
            a--;
            b++;
        }
        printf("%ld", b);
    }
    return 0;
}

上一题