【发布时间】:2021-09-04 02:04:38
【问题描述】:
我试图通过创建一个程序来有效地找到highly composite numbers 来感受C++,但我面临着不便。控制台等待,然后立即打印所有内容。我希望它像 Java 和 Python 那样一次打印一个东西。我该怎么做?
代码如下:
#include <iostream>
int main(){
int record=0; //Highest number of factors found in a number so far
for(int n=1; n<2147483647; n++){
int factors=1;
for(int PPF=2; PPF<=n; PPF++){ //PPF="Possible Prime Factor"
bool isPrime=true;
for(int i=2; i<PPF; i++){ //
if(PPF%i==0){ //Determining if the PPF is prime
isPrime=false; //
break;
}
}
if(isPrime){
if(n%PPF==0){ //Here is where I calculate the number of factors n has based on its prime factorization.
int PRP=1; //PRP="Prime Raised to Power"
int pow;
for(pow=1; n%(PRP*PPF)==0; pow++){ //Finding the exponent of a specific prime factor
PRP*=PPF;
}
factors*=pow;
}else{
break;
}
}
}
if(factors>record){
record=factors;
std::cout<<n; //Print the highly composite number
printf("\n"); //Gotta make a new line the old-fashioned way cuz this is a low-level language
}
}
}
我正在使用 CodeRunner 和所有必要的 C/C++ 扩展在 VS Code 上运行它。
【问题讨论】:
-
将
std::cout<<n; //Print the highly composite number printf("\n");替换为std::cout << n << std::endl;看看是否会有所不同。 -
做到了。谢谢!