Board logo

標題: 304 函式與陣列 (倍數判斷) [打印本頁]

作者: 陳育霖    時間: 2023-8-18 22:03     標題: 304 函式與陣列 (倍數判斷)

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

2. 設計說明:
請撰寫一程式,包含名為compute()的函式,接收主程式傳遞的一個陣列,陣列中有六個整數,compute()判斷陣列中有幾個3的倍數並回傳至主程式輸出。

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

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

輸出說明
有幾個3的倍數

範例輸入
10
20
30
40
50
60

範例輸出
2

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

作者: 曾宥程    時間: 2023-8-19 11:42

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int q[6];
  4. int compute(int q[6])
  5. {
  6.     int a=0;
  7.     for(int i=0 ; i<6 ; i++)
  8.         if(q[i]%3 == 0)
  9.             a++;

  10.     return a;
  11. }
  12. int main()
  13. {
  14.     for(int i=0 ; i<6 ; i++)
  15.         cin >> q[i];
  16.     cout << compute(q) << endl;
  17.     return 0;
  18. }
複製代碼

作者: 王銘鴻    時間: 2023-8-19 11:58

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int compute(int n[6])
  4. {
  5.     int sum=0;
  6.     for(int i=0;i<6;i++)
  7.     {
  8.         if(n[i]%3==0)
  9.         {
  10.             sum++;
  11.         }
  12.     }

  13. return sum;
  14. }
  15. int main()
  16. {
  17.     int num[6];
  18.     for(int i=0;i<6;i++)
  19.         cin>> num[i];
  20.     cout<<compute(num)<<endl;

  21.     return 0;
  22. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2