搜尋

首頁  >  問答  >  主體

java - 解一道C語言的算法題?求各位大神幫忙

##

仅有的幸福仅有的幸福2840 天前665

全部回覆(3)我來回復

  • 大家讲道理

    大家讲道理2017-05-16 13:26:29

    我想說不考慮演算法選擇要求這就是個2x+b = a 求x的小學數學題,如果對演算法選擇沒要求就是輸入a,b,輸出(a-b)/2 和(a+b)/ 2 .
    輸入的時候校驗下a,b是不是同為奇數或偶數以及數字是不是超變量類型的可用範圍,然後自己生成的case也要考慮這個問題

    回覆
    0
  • 为情所困

    为情所困2017-05-16 13:26:29

    雷雷

    回覆
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:26:29

    透過你提供的材料,用c++編碼如下,望指正!

    /*
      if Natalia's number of apples is x  (x>0)
      apples:the number of apple
      extra:the number that Klaudia more tha Natalia
      (apples>extra>0)
      so
      x+extra+x=apples
      x = (apples-extra)/2
    */
    #include <iostream>
    int main(int argc, char const *argv[]) {
      int apples,extra;   //apples:the number of apple  extra:the number that Klaudia more tha Natalia
    
      for (size_t i = 0; i < 10; i++) {
            std::cin>>apples>>extra;   //input
            try{
              //To determine whether the input is legal
                if(apples<=0 || extra<=0 || apples<extra || (apples-extra)%2!=0) throw apples;
                int Klaudia,Natalia;
                Natalia = (apples-extra)/2;    //calculate
                Klaudia=Natalia+2;
                std::cout<<Klaudia<<'\n'<<Natalia<<'\n';    //output
          }
            catch(int e){
                //ERROR
                std::cerr << "ERROR! the number of applse is error.\n" << '\n';
            }
      }
      return 0;
    }
    

    運行結果

    回覆
    0
  • 取消回覆