返回列表 發帖

陣列 (一)

假設五年級共有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[]={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 n[]={30, 31, 33, 32, 31, 35, 28, 29};
  7.     for(int i=0; i<8; i++)
  8.     {
  9.        cout<<"五年"<<i+1<<"班有"<<n[i]<<"人"<<endl;
  10.     }
  11.    
  12. system("pause");
  13. return 0;      
  14. }
複製代碼

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=1;i<=8;i++)
  8.    
  9.     {
  10.        cout<<"五年"<<i<<"班"<<n[i-1]<<"人"<<endl;
  11.     }

  12.   system("pause");
  13.   return 0;   
  14. }
複製代碼

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=1;i<=8;i++)
  8.     {
  9.         cout<<"五年"<<i<<"班有"<<n[i-1] <<"人"<<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 x[]={30 ,31 ,33 ,32 ,31 ,35 ,28 ,29};
  7.     for(int i=1; i<=8; i++)
  8.     {
  9.             cout<<"五年"<<i<<"班有"<<x[i-1]<<"人"<<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[]={30,31,33,32,31,35,28,29};
  7. for(int i=1;i<=8;i++)
  8. cout<<"5年"<<i<<"班有"<<n[i-1]<<"人"<<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=1; i<8; i++)
  8.         cout<<"5年"<<i<<"班有"<<n[i-1]<<"人"<<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 pp[]={30,31,33,32,31,35,28,29};
  7.     for(int i=0; i<8; i++)
  8.         cout<<"5年"<<(i+1)<<"班有"<<pp[i]<<"人"<<endl;
  9.     system("pause");
  10.     return 0;   
  11. }
複製代碼

TOP

返回列表