嵌套循环渐近分析(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
满意答案
我为此推荐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?)
原始终端模式 - 如何接受输入?(raw terminal mode - how to take in input?)
如何在Golang中始终将终端输入保持在底部?(How to keep terminal input always at bottom in Golang?)
如何使用tcl将原始模式终端中的按键处理委托给rlwrap(How to delegate key press handling to rlwrap in raw mode terminal with tcl)
Docker显示在交互式终端模式中(Docker display issues in interactive terminal mode)
lanterna终端输入不断重复(lanterna terminal input keeps repeating)
如何从终端使用LLDB(How to use LLDB from Terminal)
如何设置jsch终端模式?(How to set jsch terminal mode?)
从脚本中退出Windows终端交互模式(Exit windows terminal interactive mode from within a script)
在同一个终端执行'stty raw'命令?(Execute 'stty raw' command in same terminal?)
相关文章
更多计算机病毒及其防治 Computer Virus Analysis and Antivirus
zz Data Analysis Process
企业级搜索引擎Solr 第二章 Schema和文本分析(Schema and Text Analysis)[2]
Solr参数(Analysis Collection Common CoreAdmin)
Solr官方文档系列——Text Analysis
java.lang.NoClassDefFoundError: org/apache/lucene/analysis/synonym/SynonymFilter
java WHILE 循环问题
怎么递归json嵌套json?
spring 嵌套事务 的 加锁 问题
Storm数据流模型的分析及讨论
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!