嵌套循环渐近分析(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);      
}

相关问答

更多

检测stdin是终端还是管道?(Detect if stdin is a terminal or pipe?)

使用isatty : #include <stdio.h> #include <io.h> ... 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 <stdio.h> #include...

原始终端模式 - 如何接受输入?(raw terminal mode - how to take in input?)

我为此推荐GNU readline库 。 它负责获取输入行的繁琐工作,并允许用户使用退格键,左右箭头等编辑他的行,并使用向上箭头调用较旧的命令,甚至使用^ R搜索较旧的命令,等等。Readline安装了典型的类似linux的unix发行版,但如果你没有它,你可以在这里找到它 编辑:这是一个最小的readline示例: #include <stdio.h> #include <readline/readline.h> #include <readline/history.h> int main(i...

如何在Golang中始终将终端输入保持在底部?(How to keep terminal input always at bottom in Golang?)

使用github.com/gizak/termui您将朝着正确的方向前进。 要明白为什么你不能那样做 我希望在终端的主要部分更新实时更新,而“输入命令>”始终保持在底部 部分整理出来,对计算历史的一点点游览即将到来。 ;-) 问题是,您的终端模拟器¹默认工作的模式源于计算机在早于字母数字显示器的时代与操作员通信的方式 - 他们将使用行式打印机打印他们的响应。 现在想一想:行式打印机的工作原理如下:它可以在一张纸上打印发送给它的任何内容。 什么是输出,输出。 新输出总是在物理上低于旧输出。 当字母数...

如何使用tcl将原始模式终端中的按键处理委托给rlwrap(How to delegate key press handling to rlwrap in raw mode terminal with tcl)

唉,终端处理系统的工作方式并不那么简单。 但这不是不可能的。 我能想到的最简单的机制(的确是唯一一个不会让我对此感到畏缩的机制)是使用Expect扩展的interact命令的一些更高级的功能。 特别是, interact将Expect spawn的程序有效地连接到外部世界,但您也可以添加模式以允许扩展行为。 package require Expect spawn /your/program yourarguments... interact { "\t" { # Do...

Docker显示在交互式终端模式中(Docker display issues in interactive terminal mode)

当我使用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 w...

lanterna终端输入不断重复(lanterna terminal input keeps repeating)

while (hidenWord.equals(word) == false) { // ... while (bool == false) { // ... try { if (key.getKind() == Key.Kind.Enter && guess != null) { bool = true; } 您在第一次看到“Enter”时将bool设置为true...

如何从终端使用LLDB(How to use LLDB from Terminal)

来自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/buil...

如何设置jsch终端模式?(How to set jsch terminal mode?)

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/window...

从脚本中退出Windows终端交互模式(Exit windows terminal interactive mode from within a script)

我假设你可以控制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. T...

在同一个终端执行'stty raw'命令?(Execute 'stty raw' command in same terminal?)

由于JVM重定向stdio / stdout / stderr,您可以尝试这样的事情: String[] cmd = {"/bin/sh", "-c", "stty raw </dev/tty"}; Runtime.getRuntime().exec(cmd); 注意stty(通常)在stdin而不是stdout上运行。 Since the JVM redirects stdio/stdout/stderr, you might try something like this: String[]...

相关文章

更多

计算机病毒及其防治 Computer Virus Analysis and Antivirus

Episode I 计算机病毒的历史 FUDAN UNIVERSITY 3 ...

zz Data Analysis Process

An interesting article....easy to understand. Summa ...

企业级搜索引擎Solr 第二章 Schema和文本分析(Schema and Text Analysis)[2]

文章转载自网易博客,原文地址:http://quweiprotoss.blog.163.com/blo ...

Solr参数(Analysis Collection Common CoreAdmin)

一.Analysis 1.analysis.query:Holds the query to be a ...

Solr官方文档系列——Text Analysis

Text fields are typically indexed by breaking the t ...

java.lang.NoClassDefFoundError: org/apache/lucene/analysis/synonym/SynonymFilter

2013-6-24 13:28:51 org.apache.solr.common.SolrExcep ...

java WHILE 循环问题

有没有办法让循环在5秒以后自动退出? 问题补充: clshanghe 写道 ...

怎么递归json嵌套json?

比如JSONObject: {&quot;temp1&quot;:{&quot;b&quot;:&q ...

spring 嵌套事务 的 加锁 问题

有两个service:ServiceA 和 ServiceB ServiceA { metho ...

Storm数据流模型的分析及讨论

转自:http://www.cnblogs.com/panfeng412/archive/2012/0 ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本