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)
上一篇 2026年4月15日 下午6:19
下一篇 2026年4月15日 下午6:25


相关推荐

  • macbookpro安装homebrew_虚拟机安装mac流畅吗

    macbookpro安装homebrew_虚拟机安装mac流畅吗Homebrew简称brew,Homebrew是一款MacOS平台下的软件包管理工具,很方便帮助我们实现安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,Homebrew官网中清楚介绍了安装和基本使用安装与卸载安装1.打开mac终端将以下命令粘贴至终端。/usr/bin/ruby-e”$(curl-fsSLhttps://…

    2025年8月6日
    7
  • 三角形余弦定理

    三角形余弦定理

    2026年3月20日
    2
  • windows退出vim

    windows退出vim在普通模式下,用ZZ来保存并退出,用ZQ不保存退出。在插入模式下,先按来回到普通模式,再按ZZ或者ZQ。(注意:ZZ或者ZQ直接按,要大写。)

    2022年5月1日
    51
  • SpringBoot注解验证参数

    SpringBoot注解验证参数注解 作用类型 解释 NotNull 任何类型 属性不能为 null NotEmpty 集合 集合不能为 null 且 size 大于 0 NotBlanck 字符串 字符 字符类不能为 null 且去掉空格之后长度大于 0 AssertTrue Boolean boolean 布尔属性必须是 true Min 数字类型 原子和包装 限定数字的最小值 整型 Max 同 Min 限定数字的最大值 整型

    2026年3月16日
    2
  • MySQL增删改查常用语句命令「建议收藏」

    MySQL增删改查常用语句命令「建议收藏」 2017/11/01 |  未分类 |songjian|  1条评论 |  1818viewsMySQL关系型数据库RDS中的老大哥,增删改查是MySQL入门的基础,数据库吧来说说MySQL数据库增删改查常用语句。增删改查语句增删改查的语句命令为增:insert删:delete改:update查:SELECT或者show库操作创建数据库:createdatabaseshujukuba;…

    2025年7月31日
    6
  • 谷歌地球Google Earth打不开的解决办法[通俗易懂]

    谷歌地球Google Earth打不开的解决办法[通俗易懂]从2020年11月20号左右,谷歌地球中国服务器全部关停,所有原来可以使用的hosts,全部不能使用了,导致原来可以在电脑上打开谷歌地球的,现在全部提示无网络,如下图:这个是谷歌地球的最新版,一样打不开:解决办法,尝试了,国内所有的有关谷歌地图的软件。唯一现在可以使用的:BIGEMAP如下图分下下载地址,大家可以安装来试一试,免费可用:http://download.bigemap.com/bmsetup.rar欢迎留言,提供更多谷歌地球的信息…

    2026年1月27日
    5

发表回复

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

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