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


相关推荐

  • 查询数据库用户所有表名_sql语句收回用户权限

    查询数据库用户所有表名_sql语句收回用户权限在企业系统中经常会使用到给用户分配权限的情况,往往在用户信息表和权限表之间还维护了一张角色表,即通过给用户添加角色,角色添加权限的这样一种方式来给用户间接的添加权限。如图示例那么,查询用户权限的多表查询sql语句长什么样呢?select*frompe_role_userruinnerjoinpe_role_modulermonru.role_id=rm.`role_id…

    2022年9月1日
    2
  • vuecli关闭eslint_如何关闭eslint

    vuecli关闭eslint_如何关闭eslintVue前端

    2022年10月8日
    2
  • 博客日记

    博客日记博客日记 一直想搭建好自己的网站,可是都没有坚持下去。可能对于自己时没有压力的创作,或者是说在自己觉得不好创作的时候直接一把删除是很放松的,这也导致了我从2019年开始弄到现在一个网站也没有坚持下来,就是因为自己的感受到的*删库*的成本太低了,哈哈没有办法,现在重新开始买了服务器买了域名,去备案,结果确实自己的备案被驳回来了,原因就是我的备案信息不正确。。。腾讯的客服回电话说的是比对没有任何问题,但是就是信息比对不正确,我也很迷。现在要重新备案,惨淡的八九天又要来了,不知道粤局

    2022年4月28日
    34
  • python自学基础1week

    python自学基础1week一、python老师介绍二、为什么要学习python?三、学习python有前途吗?疗程1:语言基础疗程2:网络编程疗程3:web基础开发疗程4:算法&设计模式疗程5:pytho

    2022年7月6日
    22
  • 虚拟机ifconfig或ip addr不显示ip地址「建议收藏」

    虚拟机ifconfig或ip addr不显示ip地址「建议收藏」虚拟机ifconfig或ipaddr不显示ip地址报错图片:一直查不到ip地址,有重新启动很多次解决方法(1)命令查看配置文件:vi/etc/sysconfig/network-scripts/ifcfg-ens33ens33注意看这个修改的文件后缀把ONBOOT的状态no改为yes然后重启,应该就没问题了。(2):还有一种可能是因为虚拟网卡没有正常连接,解决方法是开启虚拟网卡的服务:打开任务管理器,选择服务标签,为了保险,开启所有的和vmware有关的服务检

    2022年7月27日
    8
  • hadoop生态圈各个组件简介

    hadoop生态圈各个组件简介1,HDFS(hadoop分布式文件系统)是hadoop体系中数据存储管理的基础。他是一个高度容错的系统,能检测和应对硬件故障。client:切分文件,访问HDFS,与那么弄得交互,获取文件位置信息,与DataNode交互,读取和写入数据。namenode:master节点,在hadoop1.x中只有一个,管理HDFS的名称空间和数据块映射信息,配置副本…

    2022年5月22日
    41

发表回复

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

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