首页 \ 问答 \ 嵌套循环渐近分析(Nested Loops Asymptotic analysis)

嵌套循环渐近分析(Nested Loops Asymptotic analysis)

需要帮助找出这个例子的人。 该示例说明0(n)运行时间。 我看到outter循环是O(logn)我无法弄清楚如何描述与n相关的内循环。 非常感谢帮助。

for (int i = 1; i <= N; i = i*2) // log n 
    for (int j = 0; j < i; j++)  // less than n i don't know how to describe the growth
        sum++;

答案:0(n)


Need help figuring this hw example out guys. the example states 0(n) running time. I see that the outter loop is O(logn) I cant figure out how to describe the inner loop in relation to n. Help is much appreciated.

for (int i = 1; i <= N; i = i*2) // log n 
    for (int j = 0; j < i; j++)  // less than n i don't know how to describe the growth
        sum++;

Answer:: 0(n)


原文:https://stackoverflow.com/questions/35250801
更新时间:2021-11-28 17:11

最满意答案

我为此推荐GNU readline库 。 它负责获取输入行的繁琐工作,并允许用户使用退格键,左右箭头等编辑他的行,并使用向上箭头调用较旧的命令,甚至使用^ R搜索较旧的命令,等等。Readline安装了典型的类似linux的unix发行版,但如果你没有它,你可以在这里找到它

编辑:这是一个最小的readline示例:

#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>

int main(int argc, char ** argv)
{
    while(1)
    {
        char * line = readline("> ");
        if(!line) break;
        if(*line) add_history(line);
        /* Do something with the line here */
    }
}

用gcc -o test编译test.c -lreadline -lncurses。

如果你不能使用readline,getline是另一种选择:

#include <stdio.h>
int main()
{
    char * line = NULL;
    size_t len;
    while(getline(&line, &len, stdin) >= 0)
       printf("I got: %s", line);
}

如果即使getline是不可接受的,你可以使用fgets。 它不会动态分配合适大小的缓冲区,所以太长的行会被截断。 但至少它是标准的C:

#include <stdio.h>
int main()
{
    char buf[1000];
    while(fgets(buf, sizeof(buf), stdin)
        printf("I got: %s, line);      
}

I recommend the GNU readline library for this. It takes care of the tedious work of getting lines of input, and allows the user to edit his line with backspace, left and right arrows, etc, and to recall older command using the up arrow and even search for older command using ^R, etc. Readline comes installed with typical unix-like distributions like linux, but if you don't have it, you can find it here

Edit: Here is a minimal readline example:

#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>

int main(int argc, char ** argv)
{
    while(1)
    {
        char * line = readline("> ");
        if(!line) break;
        if(*line) add_history(line);
        /* Do something with the line here */
    }
}

Compile with gcc -o test test.c -lreadline -lncurses.

If you can't use readline, getline is an alternative:

#include <stdio.h>
int main()
{
    char * line = NULL;
    size_t len;
    while(getline(&line, &len, stdin) >= 0)
       printf("I got: %s", line);
}

If even getline is unacceptable, you can use fgets. It will not dynamically allocate a buffer of suitable size, so too long lines will be truncated. But at least it is standard C:

#include <stdio.h>
int main()
{
    char buf[1000];
    while(fgets(buf, sizeof(buf), stdin)
        printf("I got: %s, line);      
}

相关问答

更多
  • 使用isatty : #include #include ... if (isatty(fileno(stdin))) printf( "stdin is a terminal\n" ); else printf( "stdin is a file or a pipe\n"); (在Windows上,它们以下划线为前缀: _isatty , _fileno ) Use isatty: #include #include
  • 我为此推荐GNU readline库 。 它负责获取输入行的繁琐工作,并允许用户使用退格键,左右箭头等编辑他的行,并使用向上箭头调用较旧的命令,甚至使用^ R搜索较旧的命令,等等。Readline安装了典型的类似linux的unix发行版,但如果你没有它,你可以在这里找到它 编辑:这是一个最小的readline示例: #include #include #include int main(int ar ...
  • 使用github.com/gizak/termui您将朝着正确的方向前进。 要明白为什么你不能那样做 我希望在终端的主要部分更新实时更新,而“输入命令>”始终保持在底部 部分整理出来,对计算历史的一点点游览即将到来。 ;-) 问题是,您的终端模拟器¹默认工作的模式源于计算机在早于字母数字显示器的时代与操作员通信的方式 - 他们将使用行式打印机打印他们的响应。 现在想一想:行式打印机的工作原理如下:它可以在一张纸上打印发送给它的任何内容。 什么是输出,输出。 新输出总是在物理上低于旧输出。 当字母数字显示(屏 ...
  • 唉,终端处理系统的工作方式并不那么简单。 但这不是不可能的。 我能想到的最简单的机制(的确是唯一一个不会让我对此感到畏缩的机制)是使用Expect扩展的interact命令的一些更高级的功能。 特别是, interact将Expect spawn的程序有效地连接到外部世界,但您也可以添加模式以允许扩展行为。 package require Expect spawn /your/program yourarguments... interact { "\t" { # Do some ...
  • 当我使用Docker CE 17.05或17.06 Edge版本时,我认为我有类似的经验,并且存在线路馈送(换行符)没有正确连接的错误。 IIRC已经修复了17.06.0-ce-rc2之类的东西。 尝试使用更高版本。 截至目前的最新版本是Mac OS中的17.09.0-ce-rc1-mac28(19152)。 I think I have similar experience when I used Docker CE 17.05 or 17.06 Edge version and there was a ...
  • while (hidenWord.equals(word) == false) { // ... while (bool == false) { // ... try { if (key.getKind() == Key.Kind.Enter && guess != null) { bool = true; } 您在第一次看到“Enter”时将bool设置为true,之后不会 ...
  • 来自Apple文档: 指定要调试的程序 首先,您需要将程序设置为debug。 与GDB一样,您可以启动LLDB并使用命令行指定要调试的文件。 类型: $ lldb /Projects/Sketch/build/Debug/Sketch.app当前可执行文件设置为'/Projects/Sketch/build/Debug/Sketch.app'(x86_64)。 或者,您可以使用file命令指定可在运行后运行的可执行文件: $ lldb(lldb)file /Projects/Sketch/build/Deb ...
  • Jsch不支持ascii转移。 如果要在不同的文本文件类型之间进行转换,则必须以编程方式进行转换。 如果是unix / windows问题,请查看本文如何将文件从Dos转换为Unix Jsch does not support ascii transfers. If you want to convert between different text file types, you will have to do it programatically. If it is a unix/windows iss ...
  • 我假设你可以控制python返回的退出代码。 如果要继续,只需让python返回0,如果要退出,则返回1。 然后在INIT.BAT中修改对python的调用,如下所示: python app.py -i || exit I assume you can control the exit code that python returns. Simply have python return 0 if you want to continue, or 1 if you want to exit. Then m ...
  • 由于JVM重定向stdio / stdout / stderr,您可以尝试这样的事情: String[] cmd = {"/bin/sh", "-c", "stty raw

相关文章

更多

最新问答

更多
  • 如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)
  • maven中snapshot快照库和release发布库的区别和作用
  • arraylist中的搜索元素(Search element in arraylist)
  • 从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)
  • Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)
  • 如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • 从iframe访问父页面的id元素(accessing id element of parent page from iframe)
  • linux的常用命令干什么用的
  • Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)
  • 怎么删除禁用RHEL/CentOS 7上不需要的服务
  • 为什么Gradle运行测试两次?(Why does Gradle run tests twice?)
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在android中的活动之间切换?(Switching between activities in android?)
  • Perforce:如何从Depot到Workspace丢失文件?(Perforce: how to get missing file from Depot to Workspace?)
  • Webform页面避免运行服务器(Webform page avoiding runat server)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 内存布局破解(memory layout hack)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • 我们可以有一个调度程序,你可以异步添加东西,但会同步按顺序执行吗?(Can we have a dispatcher that you can add things todo asynchronously but will be executed in that order synchronously?)
  • “FROM a,b”和“FROM a FULL OUTER JOIN b”之间有什么区别?(What is the difference between “FROM a, b” and “FROM a FULL OUTER JOIN b”?)
  • Java中的不可变类(Immutable class in Java)
  • bat批处理文件结果导出到txt
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • 德州新起点计算机培训学校主要课程有什么?
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • “latin1_german1_ci”整理来自哪里?(Where is “latin1_german1_ci” collation coming from?)