返回列表 發帖

305 函式與陣列 (運算判斷)

1. 題目說明:
請依下列題意進行作答,使輸出值符合題意要求。

2. 設計說明:
請撰寫一程式,包含名為compute()的函式,接收主程式傳遞的一個陣列,陣列中有三個整數,陣列中索引值1代表運算符號(+或*),若輸入1則索引值前後數值相加;輸入2則相乘,compute()計算運算結果並回傳至主程式輸出。

提示:若使用 Java 語言答題,請以「JP」開頭命名包含 main 靜態方法的 class,評測系統才能正確評分。

3. 輸入輸出:
輸入說明
三個整數

輸出說明
根據輸入值輸出運算結果

範例輸入1
12
1
15

範例輸出1
27

範例輸入2
7
2
10

範例輸出2
70

本帖隱藏的內容需要回復才可以瀏覽

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int q[3];
  4. int compute(int q[3])
  5. {
  6.     if(q[1]==1)
  7.         return q[0]+q[2];
  8.     else
  9.         return q[0]*q[2];
  10. }
  11. int main()
  12. {
  13.     for(int i=0 ; i<3 ; i++)
  14.         cin >> q[i];
  15.     cout << compute(q) << endl;
  16.     return 0;
  17. }
複製代碼
Vincent

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int compute(int a[3])
  4. {
  5.     if(a[1]==1)
  6.         return a[0]+a[2];
  7.     else
  8.         return a[0]*a[2];
  9. }
  10. int main()
  11. {
  12.     int arr[3];
  13.     for(int i=0 ; i<3 ; i++)
  14.         cin >> arr[i];
  15.     cout << compute(arr) << endl;
  16.     return 0;
  17. }
複製代碼

TOP

返回列表