uva1599_cumulative iteration number

uva1599_cumulative iteration numberProblemC:Self­describingSequence SolomonGolomb’s self­describingsequence  istheonlynon­decreasingsequenceofpositiveintegerswiththepropertythatitcontainsexactly f(k)

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

  Problem C: Self­describing Sequence 

Solomon Golomb’s 
self­describing sequence
 

$\langle f(1), f(2), f(3), \dots \rangle$
 is the only non­decreasing sequence of positive integers with the property that it contains exactly 
f
(
k
) occurrences of 
k
 for each 
k
. A few moments thought reveals that the sequence must begin as follows:

\begin{displaymath}\begin{array}{c\vert cccccccccccc}\mbox{\boldmath $n$} & 1 &......)$} & 1 & 2 & 2 & 3 & 3 & 4 & 4 & 4 & 5 & 5 & 5 & 6\end{array}\end{displaymath}


In this problem you are expected to write a program that calculates the value of 
f
(
n
) given the value of 
n
.

Input 

The input may contain multiple test cases. Each test case occupies a separate line and contains an integer n ( $1 \le n \le 2,000,000,000$). The input terminates with a test case containing a value0 for n and this case must not be processed.

Output 

For each test case in the input output the value of f(n) on a separate line.

Sample Input 

100
9999
123456
1000000000
0

Sample Output 

21
356
1684
438744


Miguel Revilla 
2000-12-26

#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>

/*
 * n    1  2  3  4  5  6  7  8  9 10 11 12 ...
 * f(n) 1  2  2  3  3  4  4  4  5  5  5  6 ...
 */

const int NMAX = 1000000;

void solve()
{
    int n;
    if (scanf ("%d", &n) == EOF || 0 == n) {
        exit (0);
    }

    static int g[NMAX];
    g[0] = 0;
    g[1] = 1;

    int i = 1;
    int fi = 1;
    int k = 1;

    while (g[i - 1] < n) {
        g[i] = g[i - 1] + fi;
#ifndef ONLINE_JUDGE
        printf ("g[%d] = %d\n", i, g[i]);
#endif
        if (++i > g[k]) {
            ++fi;
            ++k;
        }
        assert (i < NMAX);
    }

    printf ("%d\n", i - 1);
}

int main (int argc, char **argv)
{
    while (true) {
        solve();
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/188752.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pip如何卸载库_查看python版本的命令是

    pip如何卸载库_查看python版本的命令是pipfreeze>allpackages.txtpipuninstall-rallpackages.txt-y

    2022年10月10日
    3
  • 终端命令查看TensorFlow版本号及路径[通俗易懂]

    终端命令查看TensorFlow版本号及路径[通俗易懂]在Mac终端查询安装的TensorFlow版本号及路径

    2022年6月25日
    61
  • 国外最流行的Bootstrap后台管理模板

    国外最流行的Bootstrap后台管理模板工欲善其事,必先利其器对于从事软件开发的您也一样,有一套熟悉的bootstrap后台ui框架让您的开发速度大幅度提升这是本人经常使用到的一些bootstrap后台框架推荐给大家第一名inspiniabootstrap演示地址http://cn.inspinia.cn效果图http://cn.inspinia.cnhttp://cn.inspinia.cn第二名…

    2022年4月25日
    38
  • Android 绑定服务 bindService[通俗易懂]

    Android 绑定服务 bindService[通俗易懂]绑定服务是客户端–服务器接口中的服务器。组件(如activity)和服务进行绑定后,可以发送请求、接收响应、执行进程间通信(IPC)。不会无限期在后台运行。要提供服务绑定,必须实现onBind()回调方法,该方法返回的IBinder对象定义了客户端用来与服务进行交互的编程接口。客户端可以通过调用bindService()绑定到服务。调用时,必须提供ServiceConnection的实现,后者会…

    2022年6月10日
    36
  • java中使用uuid函数_uuid主键

    java中使用uuid函数_uuid主键UUID介绍:UUID(UniversallyUniqueIdentifier)全局唯一标识符,是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字。由以下几部分的组合:当前日期和时间(UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则…

    2022年9月22日
    4
  • 穷举暴力激活成功教程密码(真香版)

    穷举暴力激活成功教程密码(真香版)最近没事干,本来是准备激活成功教程邻居家WiFi密码的,后来发现太慢了,要加载好久,所以自己做了个测试机。下面是激活成功教程器源码:#include<windows.h>#include<iostream>#include<cstdio>#include<conio.h>usingnamespacestd;voiddown(chara){ keybd_event(a,(BYTE)0,0,0); keybd_event(a,(BYTE)0,

    2022年8月22日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号