第10周


10.1 結構

兩個同類型的結構變量,可以互相賦值,但不能進行比較運算。結構的成員變量可以是任何類型的。

10.2 全局變量,局部變量,靜態變量

定義在函數的內部的變量為局部變量(函數的形參也是局部變量),只能在定義它的函數內部使用;定義在所有函數的外面的變量為全局變量,在所有函數中都可以使用。

全局變量都是靜態變量,局部變量定義時如果添加了static關鍵字則也為靜態變量。靜態變量的存放地址在整個程序運行期間都固定不變。

非靜態變量一定是局部變量,其地址每次函數調用時都可能不同,在函數的一次執行期間不變。

如果未初始化,則靜態變量會被自動初始化為全0(每個bit都是0),局部非靜態變量的值隨機。

10.3 變量的作用域和生存期

變量名,函數名和類型名統稱為標識符,其能夠起作用的的范圍稱之為作用域。單文件程序中,結構、函數和全局變量的作用域是其定義所在的整個文件。

函數形參的作用域是整個函數。局部變量的作用域,是從定義它的語句開始,到包含它的最內層的那一對大括號的右大括號為止。for循環的控制變量作用域是整個for循環。

同名標識符的作用域,可能存在一個被另一個包含,則在小的作用域里,作用域大的那個標識符被屏蔽不起作用。

變量的生存期是指在此期間變量占有內存空間,只能歸其使用,而生存期終止后,不再占有該內存空間,該空間隨時可能派做他用。

全局變量的生存期,從程序被裝入內存開始到整個程序結束。靜態局部變量的生存期,從定義它語句第一次被執行開始,到整個程序結束為止。

函數形參的生存期從函數執行開始,到函數返回時結束。非靜態局部變量的生存期,一旦程序執行到了作用域之外即告終止。

10.4 簡單排序

 1 void SelectionSort(int a[], int size)  2 {  3     for(int i=0; i<size-1; i++) {  4         int tmpMin = i;  5         for(int j=i+1; j<size; j++) {  6             if(a[j] < a[tmpMin])  7                 tmpMin = j;  8  }  9         int tmp = a[i]; 10         a[i] = a[tmpMin]; 11         a[tmpMin] = tmp; 12  } 13 }
 1 void InsertionSort(int a[], int size)  2 {  3     for(int i=1; i<size; i++) {  4         for(int j=0; j<i; j++) {  5             if(a[j] > a[i]) {  6                 int tmp = a[i];  7                 for(int k=i; k>j; k--)  8                     a[k] = a[k-1];  9                 a[j] = tmp; 10                 break; 11  } 12  } 13  } 14 }
 1 void BubbleSort(int a[], int size)  2 {  3     for(int i=size-1; i>0; i--) {  4         for(int j=0; j<i; j++) {  5             if(a[j] > a[j+1]) {  6                 int tmp = a[j];  7                 a[j] = a[j+1];  8                 a[j+1] = tmp;  9  } 10  } 11  } 12 }

 

作業

1.成績排序

Description:給出班里某門課程的成績單,請你按成績從高到低對成績單排序輸出,如果有相同分數則名字字典序小的在前。

Input:

第一行為n (0 < n < 20),表示班里的學生數目;

接下來的n行,每行為每個學生的名字和他的成績, 中間用單個空格隔開。名字只包含字母且長度不超過20,成績為一個不大於100的非負整數。

Output:把成績單按分數從高到低的順序進行排序並輸出,每行包含名字和分數兩項,之間有一個空格。

Sample Input:

4

Kitty 80

Hanmeimei 90

Joey 92

Tim 28

Sample Output:

Joey 92

Hanmeimei 90

Kitty 80

Tim 28

 1 #include <iostream>
 2 #include <cstring>
 3 
 4 using namespace std;  5 
 6 struct Student{  7     char name[30];  8     int score;  9 } students[30]; 10 
11 int main() 12 { 13     int n; 14     cin >> n; 15     for(int i=0; i<n; ++i) 16         cin >> students[i].name >> students[i].score; 17     for(int i=n-1; i>=0; --i) 18         for(int j = 0; j < i; ++ j) 19             if(students[j].score<students[j+1].score || students[j].score==students[j+1].score && strcmp(students[j].name,students[j+1].name)>0) { 20  Student tmp; 21                 tmp = students[j]; 22                 students[j] = students[j+1]; 23                 students[j+1] = tmp; 24  } 25 
26     for(int i=0; i<n; ++i) 27         cout << students[i].name << " " << students[i].score << endl; 28 
29     return 0; 30 }

2.分數線划定

Description:

世博會志願者的選拔工作正在 A 市如火如荼的進行。為了選拔最合適的人才,A市對所有報名的選手進行了筆試,筆試分數達到面試分數線的選手方可進入面試。面試分數線根據計划錄取人數的150%划定,即如果計划錄取m名志願者,則面試分數線為排名第m*150%(向下取整)名的選手的分數,而最終進入面試的選手為筆試成績不低於面試分數線的所有選手。現在就請你編寫程序划定面試分數線,並輸出所有進入面試的選手的報名號和筆試成績。

Input:

第一行,兩個整數n,m(5 ≤ n ≤ 5000,3 ≤ m ≤ n),中間用一個空格隔開,其中n 表示報名參加筆試的選手總數,m 表示計划錄取的志願者人數。輸入數據保證m*150%向下取整后小於等於n。第二行到第 n+1 行,每行包括兩個整數,中間用一個空格隔開,分別是選手的報名號k(1000 ≤ k ≤ 9999)和該選手的筆試成績s(1 ≤ s ≤ 100)。數據保證選手的報名號各不相同。

Output:

第一行,有兩個整數,用一個空格隔開,第一個整數表示面試分數線;第二個整數為進入面試的選手的實際人數。從第二行開始,每行包含兩個整數,中間用一個空格隔開,分別表示進入面試的選手的報名號和筆試成績,按照筆試成績從高到低輸出,如果成績相同,則按報名號由小到大的順序輸出。

Sample Input:

6 3
1000 90
3239 88
2390 95
7231 84
1005 95
1001 88

Sample Output:

88 5
1005 95
2390 95
1000 90
1001 88
3239 88

 1 #include <iostream>
 2 #include <cstring>
 3 
 4 using namespace std;  5 
 6 struct Student{  7     int id;  8     int score;  9 } students[5010]; 10 
11 int main() 12 { 13     int n, m; 14     cin >> n >> m; 15     for(int i=0; i<n; ++i) 16         cin >> students[i].id >> students[i].score; 17 
18     for(int i=n-1; i>=0; --i) 19         for(int j=0; j<i; ++j) 20             if(students[j].score<students[j+1].score || students[j].score==students[j+1].score && students[j].id>students[j+1].id) { 21  Student tmp; 22                 tmp = students[j]; 23                 students[j] = students[j+1]; 24                 students[j+1] = tmp; 25  } 26     int k = m*1.5; 27     int lineScore = students[k-1].score; 28 
29     while(students[k].score == lineScore) 30         ++k; 31     cout << lineScore << " " << k << endl; 32     for(int i=0; i<k; ++i) 33             cout << students[i].id<< " " << students[i].score << endl; 34 
35     return 0; 36 }

3.病人排隊

Description:

病人登記看病,編寫一個程序,將登記的病人按照以下原則排出看病的先后順序: 

1. 老年人(年齡 >= 60歲)比非老年人優先看病。 

2. 老年人按年齡從大到小的順序看病,年齡相同的按登記的先后順序排序。 

3. 非老年人按登記的先后順序看病。

Input:

第1行,輸入一個小於100的正整數,表示病人的個數;

后面按照病人登記的先后順序,每行輸入一個病人的信息,包括:一個長度小於10的字符串表示病人的ID(每個病人的ID各不相同且只含數字和字母),一個整數表示病人的年齡,中間用單個空格隔開。

Output:按排好的看病順序輸出病人的ID,每行一個。

Sample Input:

5
021075 40
004003 15
010158 67
021033 75
102012 30

Sample Output:

021033
010158
021075
004003
102012

 1 #include <iostream>
 2 #include <cstring>
 3 
 4 using namespace std;  5 
 6 struct Patient{  7     char id[20];  8     int age;  9     int No; 10 } patients[110]; 11 
12 bool priority( Patient p1, Patient p2) 13 { 14     if(p1.age>=60 && p2.age<60) 15         return true; 16     if(p1.age<60 && p2.age>=60) 17         return false; 18     if(p1.age<60 &&p2.age<60) 19         return p1.No < p2.No; 20     if(p1.age>=60 && p2.age>=60) 21         return p1.age > p2.age; 22 } 23 
24 int main() 25 { 26     int n; 27     cin >> n; 28     for(int i=0; i<n; ++i) { 29         cin >> patients[i].id >> patients[i].age; 30         patients[i].No = i; 31  } 32     for(int i=n-1; i>=0; --i) 33         for(int j=0; j<i; ++j) 34             if(priority(patients[j+1], patients[j])) { 35  Patient tmp; 36                 tmp = patients[j]; 37                 patients[j] = patients[j+1]; 38                 patients[j+1] = tmp; 39  } 40 
41     for(int i=0; i<n; ++i) 42         cout << patients[i].id << endl; 43 
44     return 0; 45 }

4.mysort

Description:程序填空題,自己編寫排序函數 mysort,使得其能夠對任意類型的數組排序

 1 #include <iostream>
 2 using namespace std;  3 struct A {  4     int nouse1;  5     int nouse2;  6     int n;  7 };  8 // Your Code Here
 9 int MyCompare1( const void * e1,const void * e2) 10 { 11     int * p1 = (int * ) e1; 12     int * p2 = (int * ) e2; 13     return * p1 - * p2; 14 } 15 int MyCompare2( const void * e1,const void * e2) 16 { 17     int * p1 = (int * ) e1; 18     int * p2 = (int * ) e2; 19     if( (* p1 %10) - (* p2 % 10)) 20         return (* p1 %10) - (* p2 % 10); 21     else
22         return * p1 - * p2; 23 } 24 int MyCompare3( const void * e1,const void * e2) 25 { 26     A * p1 = (A*) e1; 27     A * p2 = (A*) e2; 28     return p1->n - p2->n; 29 } 30 int a[20]; 31 A b[20]; 32 int main () 33 { 34     int n; 35     while(cin >> n) { 36         for(int i = 0;i < n; ++i) { 37             cin >> a[i]; 38             b[i].n = a[i]; 39  } 40         mysort(a,n,sizeof(int),MyCompare1); 41         for(int i = 0;i < n; ++i) 42             cout << a[i] << "," ; 43         cout << endl; 44         mysort(a,n,sizeof(int),MyCompare2); 45         for(int i = 0;i < n; ++i) 46             cout << a[i] << "," ; 47         cout << endl; 48         mysort(b,n,sizeof(A),MyCompare3); 49         for(int i = 0;i < n; ++i) 50             cout << b[i].n << "," ; 51         cout << endl; 52  } 53     return 0; 54 }

Input:多組數據。每組數據以整數 n開頭(n<10),然后是n個整數。

Output:

對每組數據,輸出三行。

第一行是整數從小倒大排序的結果;第二行是按個位數從小到大排序的結果(如果個位數相同,小的排在前面);第三行還是整數從小倒大排序的結果。

Sample Input:

5 21 3 76 48 445
6 73 29 45 8737 2 1

Sample Output:

3,21,48,76,445,
21,3,445,76,48,
3,21,48,76,445,
1,2,29,45,73,8737,
1,2,73,45,8737,29,
1,2,29,45,73,8737,

 1 int mysort(void* a, int n, int w, int(*compare)(const void* e1, const void* e2)) {  2     char* s = (char*)a;  3     for(int i=n-1; i>=0; --i)  4         for(int j=0; j<i; ++j) {  5             char* p1 = (char*)a+j*w;  6             char* p2 = (char*)a+j*w+w;  7             if(compare(p1,p2) > 0) {  8                 for(int k=0; k<w; ++k) {  9                     char tmp = p1[k]; 10                     p1[k] = p2[k]; 11                     p2[k] = tmp; 12  } 13  } 14  } 15 }

5.從字符串中取數

Description:編寫GetDoubleFromString函數,該函數可以不斷從字符串中取出正浮點數或整數,無數可取,則返回值小於0。

 1 #include <iostream>
 2 #include <iomanip>
 3 using namespace std;  4 double GetDoubleFromString(char * str)  5 {  6 // Your Code Here
 7 }  8 
 9 int main() 10 { 11     char line[300]; 12     while(cin.getline(line,280)) { 13         double n; 14         n = GetDoubleFromString(line); 15         while( n > 0) { 16             cout << fixed << setprecision(6) << n << endl; 17             n = GetDoubleFromString(NULL); 18  } 19  } 20     return 0; 21 }

Input:多組數據,每組數據一行。

Output:針對每組數據,將其中的數輸出來。每行一個數,保留小數點后面6位。輸入數據中只會有正數,不用考慮負號。兩個數之間有至少一個非數字非小數點的字符。

Sample Input:

please 121a1 stand 0.7 9.2 1010.3983 0.00001 black stand what 1324.3
12.34 45 78ab78.34

Sample Output:

121.000000
1.000000
0.700000
9.200000
1010.398300
0.000010
1324.300000
12.340000
45.000000
78.000000
78.340000

 1 static char* p;  2 if(str)  3     p = str;  4 double num = 0;  5 while(*p && !(*p>='0'&&*p<= '9'))  6     ++p;  7 if(*p == 0)  8     return -1;  9 while(*p>='0' && *p<='9') { 10     num = num*10+*p-'0'; 11     ++p; 12 } 13 if(*p == '.') { 14     ++p; 15     double i = 10; 16     while(*p>='0' && *p<='9') { 17         num += (*p-'0')/i; 18         ++p; 19         i *= 10; 20  } 21 } 22 
23 return num;

 


免責聲明!

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



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