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


相关推荐

  • java测试案例编写方法_java实现自动化测试实例

    java测试案例编写方法_java实现自动化测试实例1.定义一个测试类(测试用例)1.1测试类名:被测试类的名字+Test比如UserServiceImplTest1.2测试类的包名:最后以.test结尾比如xxx.xx.test2.测试类中的测试方法2.1test+方法名比如testAdd2.2返回值建议void因为独立运行没有调用返回值没有意义2.3同上没有调用自然也不会有人传参参数建议…

    2022年10月10日
    5
  • 我的KT库之—–对象池

    我的KT库之—–对象池

    2021年8月13日
    56
  • Max Script|修改器篇

    Max Script|修改器篇创建一个立方体盒子 并选中并修改其高度宽度长度 box 创建立方体盒子 select box 选中以 box 命名开口的 objecta 将选中的物体赋予给集合 a 以后可以直接操作集合 aa height 60 a width 20 a length 20 高度 60 长宽各 20a heightsegs 10

    2025年9月28日
    2
  • SQL注入基本原理_sql到底怎么注入

    SQL注入基本原理_sql到底怎么注入SQL注入攻击通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的一些组合,通过执行SQL语句进而执行攻击者所要的操作,它目前是黑客对数据库进行攻击的最常用手段之一。

    2025年7月17日
    5
  • java.lang.ClassNotFoundException: org.springframework.boot.Bootstrapper

    java.lang.ClassNotFoundException: org.springframework.boot.Bootstrapper错误13:20:03.686[main]ERRORorg.springframework.boot.SpringApplication-Applicationrunfailedjava.lang.NoClassDefFoundError:org/springframework/boot/Bootstrapper atjava.lang.ClassLoader.defineClass1(NativeMethod) atjava.lang.ClassLoader.defineCla

    2022年7月20日
    20
  • 函数依赖关系的例子_部分函数依赖

    函数依赖关系的例子_部分函数依赖这里写自定义目录标题完全函数依赖、部分函数依赖和传递函数依赖举例1.完全依赖:2.部分函数依赖:传递函数依赖:完全函数依赖、部分函数依赖和传递函数依赖举例1.完全依赖:通过{学生学号,选修课程名}可以得到{该生本门选修课程的成绩},而通过单独的{学生学号}或者单独的{选修课程名}都无法得到该成绩,则说明{该生本门选修课程的成绩}完全依赖于{学生学号,选修课程名}2.部分函数依赖:通过{学生学号,课程号}可以得到{该生姓名},而通过单独的{学生学号}已经能够得到{该生姓名},则说明{该生姓

    2025年5月25日
    3

发表回复

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

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