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


相关推荐

  • MySQL基础知识:存储过程 – Stored Procedure

    MySQL基础知识:存储过程 – Stored ProcedureMySQL存储过程(StoredProcedure)主要的知识点:分隔符(delimiter)变量(variable)参数(parameters)分隔符(DELIMITER)MySQL通过

    2022年7月2日
    22
  • windows cuda安装_虚拟机 cuda

    windows cuda安装_虚拟机 cuda1.cuda的安装到 https://developer.nvidia.com/cuda-downloads(旧:URL )去下载。在安装的时候一定要自定义安装,否则将会安装

    2022年8月6日
    4
  • 软件测试工程师经典面试题[通俗易懂]

    软件测试工程师经典面试题[通俗易懂]  软件测试工程师,和开发工程师相比起来,虽然前期可能不会太深,但是涉及的面还是比较广的。前期面试实习生或者一年左右的岗位,问的也主要是一些基础性的问题比较多。涉及的知识主要有MySQL数据库的使用、Linux操作系统的使用、软件测试框架性的问题,测试环境搭建问题、当然还有一些自动化测试和性能测试的问题。测试工程师的面试题,基本上都是大同小异的,面试的核心主要在于框架模块(一到两年工作经验)。今…

    2022年7月15日
    19
  • 程序员法则xiazai_程序员手册

    程序员法则xiazai_程序员手册CSDN上很火的一帖子,全中国所有程序员都在集体YY(花了好半天时间才知道YY==意淫),http://community.csdn.net/Expert/topic/3881/3881210.xml?temp=.9396173CSDN上的帖子,可以看看人气http://www.javadict.com/profz.htm小说版,没有烦人的跟贴 …

    2022年10月6日
    2
  • 安装试用国产系统 ——中标麒麟V7.0

    安装试用国产系统 ——中标麒麟V7.0     安装试用国产系统——中标麒麟V7.0首先自然是下载个系统的安装镜像了。下载完镜像,创建一个新的虚拟机 配置好镜像文件,开始安装了 这个倒是和一般的Linux系统没什么区别,反正中标麒麟也是基于Linux的。 加载十几秒,下面开始正式安装:  使用默认的分区就好了。  安装完成,重启一下。  然后是对系统进行简单的配置,结果忘截图了。。。。登陆进去。 中标麒麟系统的默认桌面:是不…

    2022年10月20日
    2
  • 3极管npn和pnp_npn开关电路工作原理

    3极管npn和pnp_npn开关电路工作原理===================================================================三极管,全称应为半导体三极管,也称双极型晶体管、晶体三极管,是一种电流控制电流的半导体器件·其作用是把微弱信号放大成幅度值较大的电信号,也用作无触点开关。晶体三极管,是半导体基本元器件之一,具有电流放大作用,是电子电路的核心元件。三极管是在一块半导体基片上制作…

    2022年9月20日
    3

发表回复

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

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