首页
\
问答
\
如何在另一个for循环中找到2个for循环的迭代次数?(How to find the number of iterations of 2 for-loops within another for-loop?)
如何在另一个for循环中找到2个for循环的迭代次数?(How to find the number of iterations of 2 for-loops within another for-loop?)
sum = 0
for in in range(0,n):
对于范围内的j(0,i):
总和+ = 1
对于范围内的k(0,20000):
总和+ = 1我如何计算出它的迭代次数? 我知道它至少与n和20,000有关。 提前致谢! 关于如何到达那里的一些数学也会很好!
sum = 0
for i in range (0, n):
for j in range(0,i):
sum += 1
for k in range(0, 20000):
sum+= 1How do I figure out the number of iterations this will have? I know it will be related to n and 20,000 at least. Thanks in advance! Some math on how to get there would be nice too!
原文:https://stackoverflow.com/questions/26416892
更新时间:2024-02-07 11:02
最满意答案
#include <cstdio> #include <iostream> #include <memory> #include <stdexcept> #include <string> #include <array> std::string exec(const char* cmd) { std::array<char, 128> buffer; std::string result; std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); if (!pipe) throw std::runtime_error("popen() failed!"); while (!feof(pipe.get())) { if (fgets(buffer.data(), 128, pipe.get()) != std::nullptr) result += buffer.data(); } return result; }
Pre-C ++ 11版本:
#include <iostream> #include <stdexcept> #include <stdio.h> #include <string> std::string exec(const char* cmd) { char buffer[128]; std::string result = ""; FILE* pipe = popen(cmd, "r"); if (!pipe) throw std::runtime_error("popen() failed!"); try { while (!feof(pipe)) { if (fgets(buffer, 128, pipe) != NULL) result += buffer; } } catch (...) { pclose(pipe); throw; } pclose(pipe); return result; }
用
_popen
和_pclose
替换popen
和pclose
。#include <cstdio> #include <iostream> #include <memory> #include <stdexcept> #include <string> #include <array> std::string exec(const char* cmd) { std::array<char, 128> buffer; std::string result; std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose); if (!pipe) { throw std::runtime_error("popen() failed!"); } while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { result += buffer.data(); } return result; }
Pre-C++11 version:
#include <iostream> #include <stdexcept> #include <stdio.h> #include <string> std::string exec(const char* cmd) { char buffer[128]; std::string result = ""; FILE* pipe = popen(cmd, "r"); if (!pipe) throw std::runtime_error("popen() failed!"); try { while (fgets(buffer, sizeof buffer, pipe) != NULL) { result += buffer; } } catch (...) { pclose(pipe); throw; } pclose(pipe); return result; }
Replace
popen
andpclose
with_popen
and_pclose
for Windows.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
如何使用POSIX执行命令并获取C ++中的命令输出?(How do I execute a command and get output of command within C++ using POSIX?)[2022-06-27]
#include#include #include #include #include #include std::string exec(const char* cmd) { std::array buffer; std::string result; std::shared_ptr pipe(popen(cmd, "r"), p ... -
用php执行命令(Execute command with php)[2023-07-27]
你试过sudo htpasswd -b /etc/apache2/.htpasswd username password吗? Have you tried sudo htpasswd -b /etc/apache2/.htpasswd username password? -
您不能使用String.execute()进行管道或重定向。 这在Java中不起作用,所以它在Groovy中也不起作用... 您可以使用Process.pipeTo和Groovy来简化事情: Process proca = 'uname -a'.execute() Process procb = 'awk {print\$2}'.execute() (proca | procb).text 更通用的版本可能是: String process = 'uname -a | awk {print\$2}' ...
-
捕获进程输出有两个主要选项: 将其重定向到文件,然后稍后读取该文件。 使用管道。 除了隐藏在公共流抽象之后,标准库不支持管道。 实际上,这意味着创建进程B的进程A无法通过管道与B通信,除非使用特定于平台的功能。 在Windows中,脚本支持Windows脚本宿主具有管道方法的直接功能,基于轮询来自B的输出。在更低的级别,您可以使用CreateProcess API函数。 但要做到这一点并非易事。 使用临时文件作为通信通道,并且仅使用标准C ++功能,您只需通过可重定向B输出的命令解释器调用程序B. 然后,此 ...
-
for /f %%i in ( '"C:\Program Files (x86)\JAM Software\SpamAssassin for Windows\spamc.exe" ^< %1' ) do set RES2=%%i ECHO %RES2:~0,1% 您的代码有三处更改。 回波线中的冒号(需要,它是sintax的一部分),转义的重定向和在设定线中等号周围移除的空间。 对于空格,变量名称包含一个空格,变量内部的值也有一个空格(它将是第一个,因此后面的echo命令将失败) for /f % ...
-
您将需要使用subprocess.Popen : >>> import subprocess >>> r = subprocess.Popen(['ls', '-l']) #List files on a linux system. Equivalent of dir on windows. >>> output, errs = r.communicate() >>> print(output) Total 72 # My file list here Popen -construtor接受一个参数列表作 ...
-
从技术上讲,您可以使用fork()和exec系列来完成您需要做的事情。 它可能像这样工作: pid_t PID = fork(); if(PID == 0) { execl("yourcommandhere"); exit(1); } //do Whatever kill(PID, 15); //Sends the SIGINT Signal to the process, telling it to stop. execl (或任何其他家庭成员,请看这里: http ...
-
执行命令并获取输出:popen和pclose undefined(Execute a command and get output: popen and pclose undefined)[2022-04-18]
由于使用了Microsoft编译器,因此在popen和pclose的开头需要一个下划线。 更换: std::shared_ptrpipe(popen(cmd, "r"), pclose); 有: std::shared_ptr pipe(_popen(cmd, "r"), _pclose); Since a Microsoft compiler is used, an underscore is needed at the beginning of popen and pclo ... -
执行命令行并允许命令行输出正在发生的事情(Execute command line and also allow the command line to output what's happening)[2022-04-16]
您正在寻找的是异步读取,这是通过BeginOutputReadline方法方法完成的,您已经按照您的方式激活了同步读取。 简而言之,这就是你需要做的: // use asynchronous method myProcess.OutputDataReceived += new DataReceivedEventHandler(MyHandler); // start process myProcess.Start(); // start the asynchronous read myProcess.B ...