C/C++輸入數組


1 int n=0;
2 printf("please enter the number:\n");
3 scanf("%d",&n);
4   
5 int *number=new int[n];
6   
7 for(int i=0;i<n;++i)
8    scanf("%d ",&number[i]);

 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4  
 5  
 6  
 7  
 8  
 9 int main()
10 {
11     /*
12     已知數組的大小,使用動態數組
13     */
14     int i = 0;
15     int num;
16     int * a = new int[5];
17     while (cin >> num ) {
18         if (cin.get() == '\n')   //遇到回車,終止
19             break;
20         a[i++] = num;
21     }
22  
23     /*
24     數組大小未知時
25     */
26     vector<int>b;
27     while (cin >> num)
28     {
29         if (cin.get() == '\n')   //遇到回車,終止
30             break;
31         b.push_back(num);
32  
33     }
34     cout << "程序終止了" << endl;
35     system("pause");
36     return 0;
37 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM