C++上机

1、选秀节目打分,分为专家评委和大众评委,score[]数组里面存储每个评委打的分数,judge_type[]里存储与 score[]数组对应的评委类别,judge_type[i] == 1,表示专家评委,judge_type[i] == 2,表示大众评委,n表示评委总数。打分规则如下:专家评委和大众评委的分数先分别取一个平均分(平均分取整),然后,总分 = 专家评委平均分  * 0.6 +大众评委 * 0.4,总分取整。如果没有大众评委,则总分 =专家评委平均分,总分取整。函数最终返回选手得分。

            函数接口   intcal_score(int score[], int judge_type[], int n) 

[cpp]  view plain  copy
 print ?
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int cal_score(int score[],int judge_type[],int n)  
  5. {  
  6.     int expert = 0;  
  7.     int publicJudge = 0;  
  8.   
  9.     int number_of_expert = 0;  
  10.     int averageScore = 0;  
  11.       
  12.     int index;  
  13.       
  14.     for(index = 0; index < n;index++)  
  15.     {  
  16.         if(judge_type[index] = 1)  
  17.         {  
  18.             expert += score[index];  
  19.             number_of_expert ++;  
  20.         }  
  21.         else  
  22.         {  
  23.             publicJudge += score[index];  
  24.         }  
  25.     }  
  26.   
  27.     if(number_of_expert == n)  
  28.         averageScore = expert/n;  
  29.     else  
  30.         averageScore = expert/number_of_expert*0.6 + publicJudge/(n-number_of_expert)*0.4;  
  31.   
  32.     return averageScore;  
  33. }  
  34.   
  35. int main()  
  36. {  
  37.     int n = 10;  
  38.     int score[10] = {80,85,90,80,75,95,80,90,95};  
  39.     int judge_type[10] = {1,2,1,1,2,2,1,1,2,1};  
  40.     int average_score = cal_score(score,judge_type,n);  
  41.     cout << average_score << endl;  
  42.     return 0;  
  43. }  


      2、给定一个数组input[],如果数组长度n为奇数,则将数组中最大的元素放到 output[]数组最中间的位置,如果数组长度n为偶数,则将数组中最大的元素放到 output[]数组中间两个位置偏右的那个位置上,然后再按从大到小的顺序,依次在第一个位置的两边,按照一左一右的顺序,依次存放剩下的数。

      例如:input[] = {3, 6,1, 9, 7}   output[] = {3, 7, 9, 6,1};            input[] = {3, 6, 1, 9, 7, 8}    output[] = {1, 6, 8, 9, 7,3}

             函数接口   voidsort(int input[[, int n, int output[])

[cpp]  view plain  copy
 print ?
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. #define N 1000  
  5.   
  6. void bubbleSort(int a[],int len)  
  7. {  
  8.     int i,j,temp;  
  9.     for(i = 0;i < len-1;i++)  
  10.         for(j = i+1;j < len;j++)  
  11.         {  
  12.             if(a[i] > a[j])  
  13.             {  
  14.                 temp = a[i];  
  15.                 a[i] = a[j];  
  16.                 a[j] = temp;  
  17.             }  
  18.         }  
  19. }  
  20.   
  21. void sort(int input[], int len, int output[])  
  22. {  
  23.   
  24.     int mid = len/2;  
  25.     int left =  mid - 1;  
  26.     int right = mid + 1;  
  27.       
  28.   
  29.     bubbleSort(input,len);  
  30.   
  31.     int index = len - 1;  
  32.     output[mid] = input[index--];  
  33.     while(index >= 0)  
  34.     {  
  35.         output[left--] = input[index--];  
  36.         output[right++] = input[index--];  
  37.     }  
  38.   
  39.     for(left = 0;left < len;left++)  
  40.         cout << output[left] << " ";  
  41. }  
  42.   
  43. int main()  
  44. {  
  45.     int arr[] = {3,6,1,9,7,8};  
  46.     int destArr[N];  
  47.     sort(arr,6,destArr);  
  48.   
  49.     cout << endl;  
  50.   
  51.     int arr1[] = {31,6,15,9,72,8,99,5,6};  
  52.     int destArr1[N];  
  53.     sort(arr1,9,destArr1);  
  54.       
  55.     return 0;  
  56. }  

      3操作系统任务调度问题。操作系统任务分为系统任务和用户任务两种。其中,系统任务的优先级 < 50,用户任务的优先级 >= 50 <= 255。优先级大于255的为非法任务,应予以剔除。现有一任务队列task[],长度为ntask中的元素值表示任务的优先级,数值越小,优先级越高。函数scheduler实现如下功能,将task[]中的任务按照系统任务、用户任务依次存放到 system_task[]数组和 user_task[]数组中(数组中元素的值是任务在task[]数组中的下标),并且优先级高的任务排在前面,优先级相同的任务按照入队顺序排列(即先入队的任务排在前面),数组元素为-1表示结束。

      例如:task[] = {0, 30,155, 1, 80, 300, 170, 40, 99}    system_task[] = {0, 3, 1, 7,-1}    user_task[] = {4, 8, 2, 6, -1}

            函数接口   void scheduler(int task[], int n, int system_task[], int user_task[])

[cpp]  view plain  copy
 print ?
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. typedef struct stask{  
  5.     int priority;  
  6.     int index;  
  7. }task_node;  
  8.   
  9. void scheduler(int task[], int n, int system_task[], int user_task[])  
  10. {  
  11.     int i;  
  12.     task_node *task_list;  
  13.     task_list = new task_node[n*sizeof(task_node)];  
  14.     //将task序列中的任务优先级和下标存入任务结构中.  
  15.     for(i = 0;i < n; i++)  
  16.     {  
  17.         task_list[i].priority = task[i];  
  18.         task_list[i].index = i;  
  19.     }  
  20.       
  21.     //根据优先级对任务排序.  
  22.     task_node temp;  
  23.     for(i = 0;i < n - 1;i++)  
  24.     {  
  25.         for(int j = i+1;j < n;j++)  
  26.             if(task_list[i].priority > task_list[j].priority)  
  27.             {  
  28.                 temp = task_list[i];  
  29.                 task_list[i] = task_list[j];  
  30.                 task_list[j] = temp;  
  31.             }  
  32.     }  
  33.     int k1= 0,k2 = 0;  
  34.     //将任务归类存入不同数组.  
  35.     for(i = 0;i < n - 1;i++)  
  36.     {  
  37.         if(task_list[i].priority<50)  
  38.             system_task[k1++] = task_list[i].index;  
  39.         else  
  40.             if(task_list[i].priority >= 50 && task_list[i].priority <= 255)  
  41.                 user_task[k2++] = task_list[i].index;  
  42.     }  
  43.     system_task[k1] = -1;  
  44.     user_task[k2] = -1;  
  45.       
  46.     //输出归类结果.  
  47.     for(i = 0;i <= k1;i++)  
  48.         cout << system_task[i] << " ";  
  49.     cout << endl;  
  50.     for(i = 0;i <= k2;i++)  
  51.         cout << user_task[i] << " ";  
  52.       
  53.     delete [] task_list;  
  54. }  
  55.   
  56. int main()  
  57. {  
  58.     int task[] = {0, 30,155, 1, 80, 300, 170, 40, 99};  
  59.     int system_task[10];  
  60.     int user_task[10];  
  61.     scheduler(task, 9, system_task, user_task);  
  62.           
  63.         return 0;  
  64. }  

4.身份证号码合法性判断 问题描述 

我国公民的身份证号码特点如下: 

1.长度为18位 

2.1-17位只能为数字 

3.第十八位可以是数字或者小写英文字母 

4.身份证号码的第7-14位表示持有人生日的年月日信息 

 
请实现身份证号码合法性判断的函数,除满足以上要求外,需要对持有人生日的年月日信息进行校验,年份大于等于1900,小于等于2100年。需要考虑闰年、大小月的情况。所谓闰年,能被4整除且不能被100整除或能被400整除的年份。闰年2月份为29天,非闰年的2月份为28天。其他情况的合法性校验,不用考虑  
函数返回值: 
1.如果身份证号合法,返回0 
2.如果身份证号长度不合法,返回1 
3.如果身份证号第1-17位含有非数字的字符,返回2 
4.如果第十八位既不是数字也不是英文小写字母x,返回3 5.如果身份证号的年信息非法,返回4 
6.如果身份证号的月信息非法,返回5 
7.如果身份证号的日信息非法,返回6(请注意闰年的情况) 
 
注:除成功的情况外,以上其他情况合法性判断的优先级依次降低,也就是说,如果判断出长度不合法,直接返回1即可,不需要再做其他合法性判断  
要求实现函数 
int verifyIDCard(char *input)  
示例: 
1.输入:"511002 111222"返回1 

2.输入:"511002 abc123456789" 返回2

 3.输入:"511002 19880808123a"返回3

 4.输入:"511002 188808081234" 返回4

 5.输入:"511002 198813081234" 返回5 

6.输入:"511002 198808321234"返回6

 7.输入:"511002 198902291234"返回7 

8.输入:"511002 198808081234"返回0

5. 链表逆序

[cpp]  view plain  copy
 print ?
  1. #include <iostream>  
  2. #include <assert.h>  
  3. using namespace std;  
  4.   
  5. typedef struct NODE{  
  6.     int value;  
  7.     NODE *pNext;  
  8. }Node,*pNode;  
  9.   
  10. //创建链表.     
  11. Node* creat()      
  12. {  
  13.     int nuberOfNode = 5;  
  14.     int data;  
  15.     pNode head = new Node;    
  16.       
  17.     if(NULL == head)  
  18.         cout << "分配内存失败\r\n";  
  19.       
  20.     head->pNext = NULL;      
  21.       
  22.     pNode p = head;  
  23.     pNode s = NULL;  
  24.     cout << "请输入数据:\r\n";  
  25.     while(nuberOfNode--)      
  26.     {      
  27.         cin >> data;            
  28.         s = new Node;         
  29.         assert(NULL != s);        
  30.         s->value = data;      
  31.         p->pNext = s;      
  32.         p = s;   
  33.     }      
  34.     p->pNext = NULL;      
  35.     return(head);      
  36. }      
  37.   
  38. //实现链表逆序.  
  39. pNode reverseList(pNode Header)  
  40. {  
  41.     //空链表或只有一个节点.  
  42.     if(Header == NULL || Header->pNext == NULL)  
  43.         return 0;  
  44.       
  45.     //pre,cur分别指向首节点,第二个节点.  
  46.     pNode pre = Header->pNext;  
  47.     pNode cur = pre->pNext;  
  48.     pre->pNext = NULL;  
  49.     pNode next = NULL;  
  50.       
  51.     while(NULL != cur)  
  52.     {  
  53.         next = cur->pNext;  
  54.         cur->pNext = pre;  
  55.         pre = cur;  
  56.         cur = next;  
  57.     }  
  58.     Header->pNext = pre;//有头结点的链表.  
  59.       
  60.     return Header;  
  61. }  
  62.   
  63. //打印链表.  
  64. void printList(pNode head)  
  65. {  
  66.     pNode start = head->pNext;  
  67.     while(start)  
  68.     {  
  69.         cout << start->value << " ";  
  70.         start = start->pNext;  
  71.     }  
  72. }  
  73.   
  74. int main()  
  75. {  
  76.     pNode pl = creat();  
  77.     printList(pl);  
  78.     cout << endl;  
  79.     printList(reverseList(pl));  
  80.       
  81.     return 0;  
  82. }  

6. 查找子字符串出现次数,并从原字符串中删除。

[cpp]  view plain  copy
 print ?
  1. /* 
  2. 编写函数,string deletestring(string str,string sub_str)从str中查找 
  3. 匹配的字符串sub_str,采用最左匹配,且输出形式为str+"_"+匹配的次数。 
  4. */  
  5. #include <iostream>  
  6. using namespace std;  
  7.   
  8. //删除指定位置字符串.  
  9. void delSpecifiedSubString(char *str,int start,int end)  
  10. {  
  11.     char *p1 = &str[start];  
  12.     char *p2 = &str[end + 1];  
  13.     while((*p1++ = *p2++) != '\0');  
  14. }  
  15.   
  16. int delSubString(char *str,char *subStr)  
  17. {  
  18.     int count = 0;  
  19.     int index = 0;  
  20.   
  21.     char *pstr = str;  
  22.     char *psubStr = subStr;  
  23.   
  24.     if(str == NULL ||subStr == NULL)  
  25.         return 0;  
  26.   
  27.     while(*pstr != '\0')  
  28.     {  
  29.         if(*pstr == *psubStr)  
  30.         {  
  31.             pstr++;  
  32.             psubStr++;  
  33.             index++;  
  34.         }  
  35.         else  
  36.         {  
  37.             psubStr = subStr;  
  38.             pstr = pstr - index + 1;  
  39.             index = 0;  
  40.         }  
  41.         if(*psubStr == '\0')  
  42.         {  
  43.             count++;  
  44.             psubStr = subStr;  
  45.             delSpecifiedSubString(str,  
  46.                 pstr - index - str,  
  47.                 pstr - 1 - str);  
  48.         }  
  49.     }  
  50.     cout << str << " " << count << endl;  
  51.     return count;  
  52. }  
  53.   
  54. int main()  
  55. {  
  56.     char str[50];  
  57.     char subStr[30];  
  58.     cout << "输入str和subStr:" << endl;  
  59.     gets(str);  
  60.     gets(subStr);  
  61.     delSubString(str,subStr);  
  62.   
  63.     return 0;  
  64. }  


[cpp]  view plain  copy
 print ?
  1. <span style="color:#ff0000;">//错误有做法!!!</span>  
  2. #include <iostream>  
  3. #include <string>  
  4. using namespace std;  
  5.   
  6. char *fun(char *str,char *subStr)  
  7. {  
  8.     string s(str);  
  9.     while(s.find(subStr) != string::npos)  
  10.     {  
  11.         cout << s.find(subStr) << endl;  
  12.         //从s中删除找到的子字符串subStr.  
  13.         s.erase(s.find(subStr),strlen(subStr));  
  14.     }  
  15.     char *m = new char[50];  
  16.     strcpy(m,s.c_str());  
  17.     //注意这边的string转成char*返回值是const char*,不能直接将其return,  
  18.     //而且不能直接赋给char*,需要用strcpy来做。  
  19.   
  20.     return m;  
  21. }  
  22.   
  23. int main()  
  24. {  
  25.     char str[50];  
  26.     char subStr[30];  
  27.     gets(str);  
  28.     gets(subStr);  
  29.     cout << str << endl;  
  30.     cout << subStr << endl;  
  31.     cout << fun(str,subStr) << endl;  
  32.   
  33.     return 0;  
  34. }  


[cpp]  view plain  copy
 print ?
  1. <span style="color:#ff0000;">//功能:删除str中的subStr.  
  2. //正确的方法!!!</span>  
  3. #include <iostream>  
  4. #include <string>  
  5. using namespace std;  
  6.   
  7. char *fun(char *str,char *subStr)  
  8. {  
  9.     string s(str);  
  10.     int index = 0;  
  11.     while(s.find(subStr,index) != string::npos)  
  12.     {  
  13.          index = s.find(subStr,index);  
  14.         //从s中删除找到的子字符串subStr.  
  15.         s.erase(s.find(subStr),strlen(subStr));  
  16.         index++;  
  17.     }  
  18.     char *m = new char[50];  
  19.     strcpy(m,s.c_str());  
  20.     //注意这边的string转成char*返回值是const char*,不能直接将其return,  
  21.     //而且不能直接赋给char*,需要用strcpy来做。  
  22.   
  23.     return m;  
  24. }  
  25.   
  26. int main()  
  27. {  
  28.     char str[50];  
  29.     char subStr[30];  
  30.     gets(str);  
  31.     gets(subStr);  
  32.     cout << "str:" << str << endl;  
  33.     cout <<  "subStr:" << subStr << endl;  
  34.     cout <<  "result:" << fun(str,subStr) << endl;  
  35.   
  36.     return 0;  
  37. }  


[cpp]  view plain  copy
 print ?
  1. /*将一个字符串的元音字母复制到另一个字符串,并排序(30分) 
  2. 问题描述: 
  3. 有一字符串,里面可能包含英文字母(大写、小写)、数字、特殊字符,现在需要实现一函数,将此字符串中的元音字母挑选出来,存入另一个字符串中,并对字符串中的字母进行从小到大的排序(小写的元音字母在前,大写的元音字母在后,依次有序)。 
  4.   
  5. 说明: 
  6. 1、  元音字母是a,e,i,o,u,A,E,I,O,U。 
  7. 2、  筛选出来的元音字母,不需要剔重; 
  8.   
  9. 最终输出的字符串,小写元音字母排在前面,大写元音字母排在后面,依次有序。 
  10.   
  11. 要求实现函数: 
  12. void sortVowel (char* input, char* output); 
  13. 【输入】  char* input,表示输入的字符串 
  14. 【输出】  char* output,排好序之后的元音字符串。 
  15. 【返回】  无 
  16.   
  17. 示例 
  18. 输入:char *input = “Abort!May Be Some Errors In Out System. “ 
  19. 输出:char *output =“aeeeooAEIO */  
  20. #include <iostream>  
  21. using namespace std;  
  22.   
  23. void sortVowel(char *input,char *output)  
  24. {  
  25.     char arr[10] = {'a','e','i','o','u','A','E','I','O','U'};  
  26.     int number[10] = {0};  
  27.     int i = 0;  
  28.     int sum = 0;  
  29.       
  30.     while('\0'!= *input)  
  31.     {  
  32.         for(i = 0;i < 10;i++)  
  33.         {  
  34.             if(arr[i] == *input)  
  35.             {  
  36.                 number[i]++;  
  37.                 break;  
  38.             }  
  39.         }  
  40.         input++;  
  41.     }  
  42.       
  43.     //计算总的出现元音字母的次数.  
  44.     for(i = 0;i < 10;i++)  
  45.         sum += number[i];  
  46.   
  47.     output = new char[sum+1];  
  48.     char *start = output;  
  49.       
  50.     for(i = 0;i < 10;i++)  
  51.     {  
  52.         while(number[i]--)  
  53.             *start++ = arr[i];  
  54.     }  
  55.     *start = '\0';  
  56.     while(*output!= '\0')  
  57.         cout << *output++;  
  58.       
  59.     cout << endl;       
  60. }  
  61.   
  62. int main()  
  63. {  
  64.     char input[] = "Abort!May Be Some Errors In Out System.";  
  65.     char *output = NULL;  
  66.     sortVowel(input,output);  
  67.       
  68.     return 0;  
  69. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值