• 第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;
  • 相关阅读:
    文件同步
    Renesas PPP Mode
    PYTHON 常用API ***
    Python DB
    Python VIL Realse
    C BIN加密
    Python VIL Service Bin
    Python 定期检查Build_setting的编译情况
    Python 字串处理
    IP分片(IP Fragment)
  • 原文地址:https://www.cnblogs.com/VincentValentine/p/5677353.html
Copyright © 2020-2023  润新知