繁体   English   中英

需要帮助创建一个程序来分解数字

[英]Need help creating a program to factorize numbers

任务是创建一个程序,该程序可以使用“for”循环进行机器可能的最大阶乘。

我知道我必须使用更大的数据类型(最大的数据类型是“long long”,如果我错了,请纠正我),我也理解阶乘的概念。

否则,我真的不知道如何应用我在 C++ 中所知道的。

这些是我现在写下的想法的基础:

#include <cstdlib>
#include <iostream>
#include <math.h>

include namespace std;

int main(int argc, char *argv[])

{
   long long i, factorial;
   cin<<factorial;

   {
      for(long long i=1; i=factorial; i++)
      {
         factorial=factorial*i
      }
   }

  system("PAUSE");
  return EXIT_SUCCESS;
}


问题是:

  1. 不知道代码写的对不对
  2. 通过执行“factorial=factorial*i”,“for”循环中的“i=factorial”没有意义,因为它会在程序停止之前停止。

话虽如此,我想指出我在高中,我的编程知识真的很差,因此各种建议和信息都被很好地接受:)。

提前致谢

也许您想计算适合unsigned long long数据类型的最大阶乘。

但是让我们看看这个可怕的程序。 我添加了对问题的评论。

#include <cstdlib> // Not to be used in C++
#include <iostream>
#include <math.h>   // Not needed

include namespace std;   // Completely wrong statement. But even if you would have done it syntactically correct, it should never be used

int main(int argc, char *argv[]) // Neither args nor argv is used
{
   long long i, factorial; // i will be redefine later. factorial is not initialized
   cin<<factorial;  // You want to stream somethin into std::cin?

   {
      for(long long i=1; i=factorial; i++) // Thats heavy stuff. i=factorial? Nobody know, what will happen
      {
         factorial=factorial*i   // Semicolon missing
      }
   }

  system("PAUSE");  // Do not use in C++
  return EXIT_SUCCESS;   // EXIT_SUCCESS is not defined
}

也许下面可以给你一个想法

#include <iostream>

int main() {
    unsigned long long number{};
    unsigned long long factorial{1};

    std::cin >> number;

    for (unsigned long long i{ 1 }; i <= number; ++i)
        factorial = factorial * i;

    std::cout << factorial;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM