简体   繁体   English

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

[英]Need help creating a program to factorize numbers

The task is to create a program that can do the maximum factorial possible by the machine using "for" cycle.任务是创建一个程序,该程序可以使用“for”循环进行机器可能的最大阶乘。

I understood i have to use bigger data types (the biggest one is "long long", correct me if i'm wrong), and i also understood the concept of the factorial.我知道我必须使用更大的数据类型(最大的数据类型是“long long”,如果我错了,请纠正我),我也理解阶乘的概念。

Otherwise, i really do not know how to apply what i know in c++.否则,我真的不知道如何应用我在 C++ 中所知道的。

these is the idea at the base of what i wrote down as of now:这些是我现在写下的想法的基础:

#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;
}


Problems are:问题是:

  1. I don't know if the code it's wrote correctly不知道代码写的对不对
  2. by doing "factorial=factorial*i", the "i=factorial" in the "for" cycle doesn't make sense, since it will stop the program before it has to.通过执行“factorial=factorial*i”,“for”循环中的“i=factorial”没有意义,因为它会在程序停止之前停止。

This being said, I would like to point out that i am in high school, my programming knowledge is really poor, and for this reason every kind of advice and information is very well accepted :).话虽如此,我想指出我在高中,我的编程知识真的很差,因此各种建议和信息都被很好地接受:)。

Thanks in advance提前致谢

Maybe you want to compute the maximum factorial that will fit in an unsigned long long data type.也许您想计算适合unsigned long long数据类型的最大阶乘。

But let us look at the horrible program.但是让我们看看这个可怕的程序。 I add comments with the problems.我添加了对问题的评论。

#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
}

Maybe the below could give you an idea也许下面可以给你一个想法

#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