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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 编译树莓派Linux内核[通俗易懂]

    编译树莓派Linux内核[通俗易懂]  RaspberryPi内核Linux代码存储在GitHub中,可以在github.com/raspberrypi/linux上查看。一、下载linux内核源码gitclone–depth=1https://github.com/raspberrypi/linux  上面的命令将下载当前的活动分支。省略–depth=1将下载整个存储库,包括所有分支的完整历史记录,但占用更多的存储空间。要下载不同的分支,可以使用以下–branch选项:gitclone–depth=1–b

    2022年7月23日
  • scope 前缀开头的方法

    scope 前缀开头的方法

    2021年11月7日
  • java jar包与war包_jar包和war包的区别,java jar包和war包部署的区别介绍「建议收藏」

    java jar包与war包_jar包和war包的区别,java jar包和war包部署的区别介绍「建议收藏」很多人都在想jar和war部署究竟哪个好呢?jar包和war包这两者之间的区别又是怎样的?有着什么不同?带着这些问题下面就一起来了解一下吧。1、jar包和war包概念介绍最首先的话,我们还是要来对jar包以及war包的概念进行一个简单的了解,首先是jar包。什么是jar包?jar包是类的归档文件,jar文件格式以流行的ZIP文件格式作为基础,和ZIP文件不一样的是,JAR文件不仅仅用来进行压缩和发…

    2022年5月24日
  • Mybatisplus 相关操作之 IService BaseMapper ServiceImpl 简单应用

    本篇主要介绍mybatisplus的简单应用这里对mybatisplus中配置遇到的问题及extent、implement所导入的包也为大家方便引入依赖1引入相关依赖依赖只是参考,每人的仓库不同,代码块内容仅作参考<dependencies><dependency><groupId>org.springframework.boot</groupId><artifac

    2022年4月8日
  • PHP实现图片的等比缩放和Logo水印功能示例

    PHP实现图片的等比缩放和Logo水印功能示例

    2021年10月25日
  • 源码免杀教程 源码免杀思路详解

    绝对不一样的源码免杀教程!绝对不一样的免杀实战体验!清晰的思路!细致全面的思路详解!让你感到免杀原来可以这么简单!教你如何在源代码中找出被杀代码,修改代码从而达到免杀效果!免杀之-网络攻防入门书籍推荐《精通黑客免杀》一部关于黑客免杀技术的书籍,学习各种免杀,躲避杀毒软件的追杀,为自己的马放下一道赦免令。目前国内关于黑客免杀技术的书籍应该很少的,大致可以说是没有。在本书中所介绍的启发式扫描免杀…

    2022年4月4日

发表回复

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

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