iT邦幫忙

0

我想詢問出百題因式分解的c++程式

c++
  • 分享至 

  • xImage

我最近想出一百題因式分解給學生寫,但我不想要自己出,所以我想詢問有沒有可以出百題因式分解的c++程式。
(補充限制,各係數在1000以內)
因式分解大概就類似x²+3x+2分解成(x+2)(x+1)
補充:是十字交乘的因式分解
(答案需要提出因數)

看更多先前的討論...收起先前的討論...
淺水員 iT邦大師 6 級 ‧ 2022-09-20 22:34:05 檢舉
先想好題目的限制條件
不然單純跑亂數亂出就好了
(A+B)²
(A+B) × (A-B)
(A-B)²
這類的?
淺水員 iT邦大師 6 級 ‧ 2022-09-21 09:28:49 檢舉
打算等發問者補充限制條件
不然係數沒限制不就幾千幾萬都可以?
先整理出題型
再隨機產生答案,反推回題目型式

重點 : 偏文字化的程式,可不可以只會用C++?C#或PYTHON不香嗎?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
bsexp301479
iT邦新手 3 級 ‧ 2022-09-23 12:43:39
最佳解答
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <iostream>
using namespace std;

int main()
{
    int x1_Value;
    int x2_Value;
    int Constant1_Value;
    int Constant2_Value;
    string Operation;
    string Operation1;
    string Operation2;
    string Operation3;
    string AnswerConstant1;
    string AnswerConstant2;
    string Answer;
    srand( (unsigned)time(NULL));
    for(int i=0; i<10 ; ++i){
        x1_Value = rand() % 200 + 1 - 100;
        x2_Value = rand() % 200 + 1 - 100;
        Constant1_Value = rand() % 200 + 1 - 100;
        Constant2_Value = rand() % 200 + 1 - 100;
        Operation1 = to_string(x1_Value * x2_Value);
        if((x1_Value * Constant2_Value)+(x2_Value * Constant1_Value)>0)
        {
            Operation2 = "+"+to_string((x1_Value * Constant2_Value)+(x2_Value * Constant1_Value));
        }
        else
        {
            Operation2 = to_string((x1_Value * Constant2_Value)+(x2_Value * Constant1_Value));
        }
        if(Constant1_Value * Constant2_Value>0)
        {
            Operation3 = "+"+to_string(Constant1_Value * Constant2_Value);
        }
        else
        {
            Operation3 = to_string(Constant1_Value * Constant2_Value);
        }
        Operation = Operation1+"x^2"+Operation2+"x"+Operation3;
        if(Constant1_Value>0)
        {
            AnswerConstant1 = "+"+to_string(Constant1_Value);
        }
        else
        {
            AnswerConstant1 = to_string(Constant1_Value);
        }
        if(Constant2_Value>0)
        {
            AnswerConstant2 = "+"+to_string(Constant2_Value);
        }
        else
        {
            AnswerConstant2 = to_string(Constant2_Value);
        }
        Answer = "("+to_string(x1_Value)+"x"+AnswerConstant1+")"+"("+to_string(x2_Value)+"x"+AnswerConstant2+")";
        std::cout << "Operation: " << Operation+"\n";
        std::cout << "Answer: " << Answer+"\n";
    }
}

我閒閒沒事用C++寫的但我覺得係數用到-+100學生就會算到哭出來了吧
所以就沒設定到1000了
執行完效果跑10次迴圈

Operation: -770x^2-2035x+2310
Answer: (77x-66)(-10x-35)
Operation: -370x^2+7251x-5546
Answer: (5x-94)(-74x+59)
Operation: -3672x^2-8601x-5005
Answer: (72x+91)(-51x-55)
Operation: 2700x^2+94x-3904
Answer: (-50x-61)(-54x+64)
Operation: -426x^2+4025x-4459
Answer: (6x-49)(-71x+91)
Operation: 1012x^2-4652x+3280
Answer: (11x-41)(92x-80)
Operation: 1386x^2-6035x+5500
Answer: (-77x+100)(-18x+55)
Operation: 810x^2-1449x-153
Answer: (27x-51)(30x+3)
Operation: 5265x^2+725x-500
Answer: (-81x+20)(-65x-25)
Operation: -1000x^2+4910x-5934
Answer: (-40x+86)(25x-69)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <iostream>
using namespace std;

int
main ()
{
  int x1_Value;
  int x2_Value;
  int Constant1_Value;
  int Constant2_Value;
  string Operation;
  string Operation1;
  string Operation2;
  string Operation3;
  string AnswerConstant1;
  string AnswerConstant2;
  string Answer;
  int QuestionType;
  srand ((unsigned) time (NULL));
  for (int i = 0; i < 100; ++i)
    {
      x1_Value = rand () % 200 + 1 - 100;
      x2_Value = rand () % 200 + 1 - 100;
      Constant1_Value = rand () % 200 + 1 - 100;
      Constant2_Value = rand () % 200 + 1 - 100;
      QuestionType = rand () % 2;
      if (QuestionType == 1)
	{
	  x2_Value = x1_Value;
	  Constant2_Value = Constant1_Value;
	}
      if (x1_Value * x2_Value != 0)
	{
	  Operation1 = to_string (x1_Value * x2_Value) + "x^2";
	}
      else
	{
	  Operation1 = "";
	}
      if ((x1_Value * Constant2_Value) + (x2_Value * Constant1_Value) > 0)
	{
	  Operation2 =
	    "+" + to_string ((x1_Value * Constant2_Value) +
			     (x2_Value * Constant1_Value)) + "x";
	}
      else if ((x1_Value * Constant2_Value) + (x2_Value * Constant1_Value) <
	       0)
	{
	  Operation2 =
	    to_string ((x1_Value * Constant2_Value) +
		       (x2_Value * Constant1_Value)) + "x";
	}
      else
	{
	  Operation2 = "";
	}
      if (Constant1_Value * Constant2_Value > 0)
	{
	  Operation3 = "+" + to_string (Constant1_Value * Constant2_Value);
	}
      else if (Constant1_Value * Constant2_Value < 0)
	{
	  Operation3 = to_string (Constant1_Value * Constant2_Value);
	}
      else
	{
	  Operation3 = "";
	}
      Operation = Operation1 + Operation2 + Operation3;

      if (Constant1_Value > 0)
	{
	  AnswerConstant1 = "+" + to_string (Constant1_Value);
	}
      else
	{
	  AnswerConstant1 = to_string (Constant1_Value);
	}
      if (Constant2_Value > 0)
	{
	  AnswerConstant2 = "+" + to_string (Constant2_Value);
	}
      else
	{
	  AnswerConstant2 = to_string (Constant2_Value);
	}
      Answer =
	"(" + to_string (x1_Value) + "x" + AnswerConstant1 + ")" + "(" +
	to_string (x2_Value) + "x" + AnswerConstant2 + ")";
      std::cout << "Operation: " << Operation + "\n";
      std::cout << "Answer: " << Answer + "\n";
    }
}

又再加了個亂數
依亂數判斷是(ax+b)(cx+d)或(ax+b)^2

0
海綿寶寶
iT邦大神 1 級 ‧ 2022-09-20 23:44:00

沒有

我要發表回答

立即登入回答