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


相关推荐

  • 用new创建数组

    用new创建数组用new创建数组用new创建数组的优势由于new创建的对象是在运行时确立的,所以有着具体情况具体分析的优点,那么什么叫做具体情况具体分析呢?我觉得c++primerplus的一个例子十分贴切——比如你在度假,已经做好每天的参观计划,可突然有一天天气不好或你心情不好,此时你就不想参观了,如果此时是在编译状态,系统是不允许的,你必须按照计划去参观,但运行时状态,系统是允许的,此时你就可以呆在酒店尽情的玩耍了。用new创建数组也有此优点,即数组长度可以根据情况而定。比如说创建10个元素的数组,可以如下代

    2022年5月15日
    36
  • pytest parametrize fixture_reno参数

    pytest parametrize fixture_reno参数前言当某个接口中的一个字段,里面规定的范围为1-5,你5个数字都要单独写一条测试用例,就太麻烦了,这个时候可以使用pytest.mark.parametrize装饰器可以实现测试用例参数化。官方示

    2022年7月30日
    2
  • yum下载rpm包

    yum下载rpm包

    2021年6月3日
    92
  • 最大矩形 —— 单调栈「建议收藏」

    最大矩形 —— 单调栈「建议收藏」https://cn.vjudge.net/contest/245662#problemAhistogramisapolygoncomposedofasequenceofrectanglesalignedatacommonbaseline.Therectangleshaveequalwidthsbutmayhavedifferentheigh…

    2022年9月22日
    0
  • 根据bak还原数据库,备份集中的数据库与现有数据库“XXX”数据库不同

    根据bak还原数据库,备份集中的数据库与现有数据库“XXX”数据库不同在做数据库相关的日常工作中,还原与备份数据库会经常遇到,有时候同样的sql2008备份的数据库,从别人那边备份的数据库文件,在自己的电脑上还原会出现:的错误。解决方法有两种:第一种:右键数据库点击还原数据库,填上需要还原的数据库名,就可以直接还原了。第二种:在新建的数据库上还原数据库时,选好备份文件后,勾选上覆盖现有数据库即可。原文地址:https://blog.csdn.net/sushena/…

    2022年5月10日
    39
  • 使用matlab绘制分段函数的三种方法

    使用matlab绘制分段函数的三种方法找到了三种绘制分段函数的方法,绘制如下函数第一种方法:%第一种分段函数t1=0:0.1:10;v1=t1;t2=10:0.1:20;v2=0*t2+10;t3=20:0.1:30;v3=30-t3;t=[t1t2t3];v=[v1v2v3];plot(t,v);axis([032012]);第二种方法:%第二种分段函数表示方法t=0:0.01:30;v=zeros(size(t));fori=1:length(t)ift(i…

    2022年6月12日
    93

发表回复

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

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