這是老師小考的題目,因為做不太出來,不知道有沒有哪位大大 可以提供解答作為參考 謝謝!

以下是題目內容

請用c++寫出以下的程式

1到100 除3整除的整數存在A[ ]
除3餘1的數存在B[ ]
寫出A [ ]裡的數
寫出B [ ]裡的數
C=A[ ]加總裡的數≦1000 =???
D=B[ ]加總裡的數≦1000 =???

解釋 : 
因為這個題目比較基本 , 覺得應該看註解文字就可以了解了
如果有不清楚的地方歡迎留言討論 : )



#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    // 宣告 A[] B[]
    int A[100],B[100];
    int i=0,j=0,k=0;
    int c=0,d=0;
   
    for(i=1;i<=100;i++){
      if(i%3==0){
        A[j] = i;
        j++;
      }
      else if(i%3==1){
        B[k] = i;
        k++;
      }
    }
   
   
    //印出 A , 並加總 A 於 C
    cout << "print A[] :" <<endl;
    for(i=0;i<j;i++){
     cout << A[i] << " ";
     c+=A[i];
     if(i%5==4) cout << endl;
    }
    cout << endl;
    //印出 B , 並加總 B 於 d
    cout << "print B[] :" <<endl;
    for(i=0;i<k;i++){
     cout << B[i] << " ";
     d+=B[i];
     if(i%5==4) cout << endl;
    }
    cout << endl;
    cout << "C= " << c <<endl;
    cout << "D= " << d <<endl;
    system("PAUSE");
    return 0;
}

創作者介紹
創作者 itest6的部落格 的頭像
itest6

itest6的部落格

itest6 發表在 痞客邦 留言(0) 人氣( 2 )