• 1257閱讀
  • 14回復

c++ 問題, 向高手求救 [復制鏈接]

上一主題 下一主題
離線plmplm
 
發帖
3850
好友元
6641
閱讀權限
3850
貢獻值
0
只看樓主 倒序閱讀 使用道具 樓主   發表于: 2010-11-04
而家要design一個program...

eg.

input no_of_ operands :4
input: 2    4   2    1
answer: 3

Output:

5
/++
+--
*/-
+/*
+//


吾理加減乘除先後,
最多可以有8個operands....
有無巴打可以教下我丫
離線智者
發帖
14653
好友元
46535
閱讀權限
37357
貢獻值
2
只看該作者 1  發表于: 2010-11-04
唔係好明問咩,可唔可以解釋多d
  
離線plmplm
發帖
3850
好友元
6641
閱讀權限
3850
貢獻值
0
只看該作者 2  發表于: 2010-11-04
引用第1樓智者2010-11-04 22:32發表的“”:
唔係好明問咩,可唔可以解釋多d

即係要你 input幾個數字...
例如

1   2   3

再input 一個答案,

6

咁你就要搵中間既符號出黎~~

1+2+3=6

output番

++
離線智者
發帖
14653
好友元
46535
閱讀權限
37357
貢獻值
2
只看該作者 3  發表于: 2010-11-04
我估.....
7個case用for loop

case 1:2 operand
case 2:3 operand
...


當1=+,2=-,3=*,4=/

12345678

咁就有7層for loop

對答案,arm架話咪store哂d for loop 既數目落array到
再變番+-*/
  
離線ab34
發帖
17725
好友元
0
閱讀權限
17729
貢獻值
0
只看該作者 4  發表于: 2010-11-05
引用第3樓智者2010-11-04 23:58發表的“”:
我估.....
7個case用for loop [表情]
case 1:2 operand
case 2:3 operand
.......

逐個試?
離線智者
發帖
14653
好友元
46535
閱讀權限
37357
貢獻值
2
只看該作者 5  發表于: 2010-11-05
引用第4樓ab342010-11-05 00:00發表的“”:
逐個試?

試+++++++,對答案,試到///////為止
  
離線真秘密
發帖
12556
好友元
23797
閱讀權限
12557
貢獻值
1
只看該作者 6  發表于: 2010-11-05
就咁睇似乎用recursive去做最簡潔.
不過c++自從做完fyp之後都無再寫..呢幾年做野都係用java多..c++ d syntax都忘記得七七八八..
不過寫program最緊要都係有概念..syntax呢d有睇番notes自然有.
離線plmplm
發帖
3850
好友元
6641
閱讀權限
3850
貢獻值
0
只看該作者 7  發表于: 2010-11-05
TIPS都係要我地用RECURSIVE做丫..
但係諗落去好難...

我開左個超大ARRAY...
運行曬所有既數,再SEARCH
離線plmplm
發帖
3850
好友元
6641
閱讀權限
3850
貢獻值
0
只看該作者 8  發表于: 2010-11-05
引用第5樓智者2010-11-05 00:02發表的“”:
試+++++++,對答案,試到///////為止[表情]

咁會有4^7 既野要試WOR
離線plmplm
發帖
3850
好友元
6641
閱讀權限
3850
貢獻值
0
只看該作者 9  發表于: 2010-11-05
#include <iostream>
using namespace std;


void output(int array[], int no_of_operands, int answer)
{
   int total_case=1;
   int count=1;

   while (count<no_of_operands)
      {
           total_case=total_case*4;
        count++;
      }
   int u=0;
   while (u<total_case)
         {
         if (array==answer)
         {
             int r=u;
             while (r>=1)
             {
                 if (r%4==0){cout<<"+";}
                 else if (r%4==1){cout<<"-";}
                 else if (r%4==2){cout<<"*";}
                 else if (r%4==3){cout<<"/"<<endl;}
                 r=r/4;
             }
        }

        u++;
        }
}

void finding_all_ans (int no_of_operands, int operands[],int answer)
{
   int space[17000];
   int temp[17000];

if(no_of_operands>1)   // calculating all the possible with 2 operands
{
   temp[0]=operands[0]+operands[1];
   temp[1]=operands[0]-operands[1];
   temp[2]=operands[0]*operands[1];
   temp[3]=operands[0]/operands[1];
}

int count2=0;
if(no_of_operands>2) // calculating all the possible with 3 operands
{
      while (count2<4){

         space[4*count2]=temp[count2]+operands[2];
         space[4*count2+1]=temp[count2]-operands[2];
         space[4*count2+2]=temp[count2]*operands[2];
         space[4*count2+3]=temp[count2]/operands[2];
      count2++;}
        }

int count3=0;
if(no_of_operands>3) // calculating all the possible with 4 operands
{
      while (count3<16)
      {
          temp[4*count3]=space[count3]+operands[3];
          temp[4*count3+1]=space[count3]-operands[3];
          temp[4*count3+2]=space[count3]*operands[3];
          temp[4*count3+3]=space[count3]/operands[3];
      count3++;
      }
}

int count4=0;
  if(no_of_operands>4) // calculating all the possible with 5 operands
  {
        while (count4<64)
        {
            space[4*count4]=temp[count4]+operands[4];
            space[4*count4+1]=temp[count4]-operands[4];
            space[4*count4+2]=temp[count4]*operands[4];
            space[4*count4+3]=temp[count4]/operands[4];
        count4++;
        }
    }
int count5=0;
    if(no_of_operands>5) // calculating all the possible with 6 operands
    {
          while (count5<256)
          {
             temp[4*count5]=space[count5]+operands[5];
             temp[4*count5+1]=space[count5]-operands[5];
             temp[4*count5+2]=space[count5]*operands[5];
             temp[4*count5+3]=space[count5]/operands[5];
          count5++;
          }
    }
int count6=0;
    if(no_of_operands>6) // calculating all the possible with 7 operands
    {
        while (count6<1024)
        {
            space[4*count6]=temp[count6]+operands[6];
            space[4*count6+1]=temp[count6]-operands[6];
            space[4*count6+2]=temp[count6]*operands[6];
            space[4*count6+3]=temp[count6]/operands[6];
            count6++;
        }

    }
int count7=0;
    if(no_of_operands>7) // calculating all the possible with 8 operands
    {
        while (count7<4096)
        {
            temp[4*count7]=space[count7]+operands[7];
            temp[4*count7+1]=space[count7]-operands[7];
            temp[4*count7+2]=space[count7]*operands[7];
            temp[4*count7+3]=space[count7]/operands[7];
            count7++;
        }
      }

    if (no_of_operands==3||no_of_operands==5||no_of_operands==7)
         output(space,no_of_operands,answer);
    else
         output(temp,no_of_operands,answer);
}


int main()
{
       int no_of_operands; // inputing numbers of operands
       cin>>no_of_operands;

       int operands[8]; // maximum size of no of operands = 8
       int x=0;
       int no_of_solution;
       while (x<no_of_operands)
       {
              cin>>operands[x]; // inputing the operands
              x++;
       }

       int answer;   // inputing the answer
       cin>>answer;


       if(no_of_operands=1)
       {
           if (operands[1]=answer)
               {no_of_solution = no_of_solution+1;
               cout<<no_of_solution;}
           else
               {no_of_solution = 0;
               cout<<no_of_solution;}
       }

       if(no_of_operands>1)
           {
           finding_all_ans (no_of_operands,operands,answer);}



       return 0;
}

而家寫左堆咁既野....但係OUTPUT吾到+-*/出黎
離線BVEsun
發帖
7256
好友元
15
閱讀權限
7256
貢獻值
1
只看該作者 10  發表于: 2010-11-05
如果要用 a[1]....a[n] 計出 X

即係可以當拆成 4 個 procedure

b[1] a[3]....a[n]
b[2] a[3]....a[n]
b[3] a[3]....a[n]
b[4] a[3]....a[n]


而 b[1] 就係+, 2 係 減... 如此類推

咁就可以 recursion

每1 次都 call 落去

計到剩返 2 個
對就 output
錯就 ignore

個邏輯係咁諗
離線★BEBE★
發帖
1791
好友元
11696
閱讀權限
1791
貢獻值
0
只看該作者 11  發表于: 2010-11-06
呢份咪係ust comp104 assignment2?
離線ab34
發帖
17725
好友元
0
閱讀權限
17729
貢獻值
0
只看該作者 12  發表于: 2010-11-06
引用第11樓c.朗拿度∼7號2010-11-06 12:00發表的“”:
呢份咪係ust comp104 assignment2?

好彩我唔係讀呢個course
離線plmplm
發帖
3850
好友元
6641
閱讀權限
3850
貢獻值
0
只看該作者 13  發表于: 2010-11-06
引用第11樓c.朗拿度∼7號2010-11-06 12:00發表的“”:
呢份咪係ust comp104 assignment2?

yayaya
岩岩已完成
離線智者
發帖
14653
好友元
46535
閱讀權限
37357
貢獻值
2
只看該作者 14  發表于: 2010-11-06
引用第13樓plmplm2010-11-06 12:38發表的“”:
yayaya [表情]
岩岩已完成 [表情]


恭喜