返回列表 發帖

陣列 (一)

假設五年級共有8個班: 1班有30人, 2班有31人, 3班有33人, 4班有32人, 5班有31人, 6班有35人, 7班有28人, 8班有29人. 試利用陣列與for迴圈, 列出五年級各班的人數.

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {      
  6.     int n[8]={30,31,33,32,31,35,28,29};
  7.     for(int i=0; i<8; i++)
  8.         cout<<"5年"<<i+1<<"班有"<<n[i]<<"人"<<endl;
  9.     system("pause");
  10.     return 0;   
  11. }
複製代碼

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {   
  6.     int j[]={30,31,33,32,31,35,28,29};
  7.     for(int i=0; i<8; i++)
  8.         cout<<"五年"<<i+1<<"班有"<<j[i]<<"人"<<\\endl;

  9.     system("pause");
  10.     return 0;   
  11. }
複製代碼
hahahahahahahaha

TOP

本帖最後由 陳宥穎 於 2019-11-22 20:56 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int n[]={30,31,33,32,31,35,28,29};//0 30 1 31...
  7.     for(int i=0; i<8; i++)
  8.     {
  9.      cout<<"5年"<<i+1<<"班有"<<n[i]<<"人"<<endl;                 
  10.     }
  11.     system("pause");
  12.     return 0;   
  13. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int n[7]={27,29,28,24,30,31,17};
  7.     for(int i=0; i<7; i++)
  8.         cout<<"六年"<<i+1<<"班有"<<n[i]<<"人"<<endl;
  9.     system("pause");
  10.     return 0;   
  11. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a[]={30,31,33,32,31,35,28,29};
  7.     for(int i=0; i<8; i++)
  8.         cout<<"5年"<<i+1<<"班有" <<n[i]<<"人"<<endl;
  9.     system("pause")
  10.     return 0;
  11. }
複製代碼
李宇澤Oscar

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int n[]={30,31,33,32,31,35,28,29};
  7.     for(int i=0; i<8; i++)
  8.        cout<<"5年"<<i+1<<"班有"<<n[i]<<"人"<<endl;
  9.    
  10.     system("pause");
  11.     return 0;   
  12. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int n[]={30,31,33,32,31,35,28,29};
  7.     for(int i=0; i<8; i++)
  8.         cout<<"5年"<<i+1<<"班有"<<n[i]<<"人"<<endl;
  9.     system("pause");
  10.     return 0;
  11. }
複製代碼
Huang chenhao

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {      
  6.     int n[8]={30,31,33,32,31,35,28,29};
  7.     for(int i=0; i<8; i++)
  8.         cout<<"7年"<<i+1<<"班有"<<n[i]<<"人"<<endl;
  9.     system("pause")
  10.     return 0;;   
  11. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {      
  6.     int n[]={30,31,33,32,31,35,28,29};
  7.     for(int i=0; i<8; i++)
  8.         cout<<"5年"<<i+1<<"班有"<<n[i]<<"人"<<endl;
  9.     system("pause");
  10.     return 0;   
  11. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {      
  6.     int x[8]={30,31,33,32,31,35,28,29};
  7.     for(int y=0; y<8; y++)
  8.          cout<<"5年"<<y+1<<"班有"<<x[y]<<"人"<<endl;
  9.     system("pause");
  10.     return 0;   
  11. }
複製代碼

TOP

返回列表