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


相关推荐

  • 多线程开发实战:Java实现多线程四种方式及相关方法原理

    多线程开发实战:Java实现多线程四种方式及相关方法原理本文带大家了解 Java 实现多线程的四种方法以及实现多线程的重要注意事项和难点

    2026年3月19日
    2
  • NTP协议详解及C语言实现

    NTP协议详解及C语言实现NTP 协议详解前言 NTP 是一种通过网络在计算机之间进行时钟同步的协议 它工作在 OSI 模型的应用层 通过一系列原理与算法 实现以极小的误差 将所有网络中的计算机与 UTC 同步 由于时钟硬件精度的限制 离线的设备不总是能时刻与 UTC 同步 误差随着时间累积使计算机的本地时钟产生较大的偏差 此外 设备初次启动 启动前时钟仍处于默认状态 也需要与现在的时间同步 因此 通过互联网与可靠的时间源同步是必要的 通过这一协议 设备将寻找合适的同步源 将自身时钟与同步源同步 以保证依赖时间的应用能正常运行

    2025年8月8日
    5
  • pycharm怎么设置默认编码为utf-8

    pycharm怎么设置默认编码为utf-8一在我们的电脑上打开 pycharm 点击 file gt settings 二进去 settings 界面之后 点击 Editor gt FileEncoding 三将 GlobalEncodi 和 projectEncod 的编码设置为 utf 8 点击下拉框可以进行设置四可以看到已经设置项目默认编码为 utf 8 了 点击 OK 就设置完成了另外可以设置属性文件

    2026年3月27日
    2
  • 软件著作权登记申请时的60页源代码格式

    软件著作权登记申请时的60页源代码格式申请软件著作权登记的时候会被要求提交 60 页的源代码 没有经验的开发者朋友第一次申请的时候难免会遇到因代码文档格式不正确 代码里含有其他版权信息等原因被要求补正的问题 从而导致拿证时间延误 为了帮助开发者朋友一次性顺利通过软件著作权登记的审查 下面为大家分享下自己总结的 60 页源代码整理攻略 第一步 请点击下载软件著作权登记源代码模板 第二步 将打算申请软著的软件名称及版本号替换模板里左上角 自助登记安卓版应用软件 V1 0 第三步 打开软件的代码文件 复制代码 第四步 回到本文档 Ctal A

    2026年3月17日
    2
  • IT视频资源分享列表(二)[通俗易懂]

    IT视频资源分享列表(二)

    2022年2月11日
    37
  • “养龙虾”需求激增3倍!美团携手联想百应推出OpenClaw远程部署服务

    “养龙虾”需求激增3倍!美团携手联想百应推出OpenClaw远程部署服务

    2026年3月13日
    2

发表回复

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

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