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?#include <iostream> using namespace std; int cal_score(int score[],int judge_type[],int n) { int expert = 0; int publicJudge = 0; int number_of_expert = 0; int averageScore = 0; int index; for(index = 0; index < n;index++) { if(judge_type[index] = 1) { expert += score[index]; number_of_expert ++; } else { publicJudge += score[index]; } } if(number_of_expert == n) averageScore = expert/n; else averageScore = expert/number_of_expert*0.6 + publicJudge/(n-number_of_expert)*0.4; return averageScore; } int main() { int n = 10; int score[10] = {80,85,90,80,75,95,80,90,95}; int judge_type[10] = {1,2,1,1,2,2,1,1,2,1}; int average_score = cal_score(score,judge_type,n); cout << average_score << endl; return 0; }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?#include <iostream> using namespace std; #define N 1000 void bubbleSort(int a[],int len) { int i,j,temp; for(i = 0;i < len-1;i++) for(j = i+1;j < len;j++) { if(a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } void sort(int input[], int len, int output[]) { int mid = len/2; int left = mid - 1; int right = mid + 1; bubbleSort(input,len); int index = len - 1; output[mid] = input[index--]; while(index >= 0) { output[left--] = input[index--]; output[right++] = input[index--]; } for(left = 0;left < len;left++) cout << output[left] << " "; } int main() { int arr[] = {3,6,1,9,7,8}; int destArr[N]; sort(arr,6,destArr); cout << endl; int arr1[] = {31,6,15,9,72,8,99,5,6}; int destArr1[N]; sort(arr1,9,destArr1); return 0; }3、操作系統任務調度問題。操作系統任務分為系統任務和用戶任務兩種。其中,系統任務的優先級 < 50,用戶任務的優先級 >= 50且 <= 255。優先級大于255的為非法任務,應予以剔除。現有一任務隊列task[],長度為n,task中的元素值表示任務的優先級,數值越小,優先級越高。函數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?#include <iostream> using namespace std; typedef struct stask{ int priority; int index; }task_node; void scheduler(int task[], int n, int system_task[], int user_task[]) { int i; task_node *task_list; task_list = new task_node[n*sizeof(task_node)]; //將task序列中的任務優先級和下標存入任務結構中. for(i = 0;i < n; i++) { task_list[i].priority = task[i]; task_list[i].index = i; } //根據優先級對任務排序. task_node temp; for(i = 0;i < n - 1;i++) { for(int j = i+1;j < n;j++) if(task_list[i].priority > task_list[j].priority) { temp = task_list[i]; task_list[i] = task_list[j]; task_list[j] = temp; } } int k1= 0,k2 = 0; //將任務歸類存入不同數組. for(i = 0;i < n - 1;i++) { if(task_list[i].priority<50) system_task[k1++] = task_list[i].index; else if(task_list[i].priority >= 50 && task_list[i].priority <= 255) user_task[k2++] = task_list[i].index; } system_task[k1] = -1; user_task[k2] = -1; //輸出歸類結果. for(i = 0;i <= k1;i++) cout << system_task[i] << " "; cout << endl; for(i = 0;i <= k2;i++) cout << user_task[i] << " "; delete [] task_list; } int main() { int task[] = {0, 30,155, 1, 80, 300, 170, 40, 99}; int system_task[10]; int user_task[10]; scheduler(task, 9, system_task, user_task); return 0; }
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?#include <iostream> #include <assert.h> using namespace std; typedef struct NODE{ int value; NODE *pNext; }Node,*pNode; //創建鏈表. Node* creat() { int nuberOfNode = 5; int data; pNode head = new Node; if(NULL == head) cout << "分配內存失敗/r/n"; head->pNext = NULL; pNode p = head; pNode s = NULL; cout << "請輸入數據:/r/n"; while(nuberOfNode--) { cin >> data; s = new Node; assert(NULL != s); s->value = data; p->pNext = s; p = s; } p->pNext = NULL; return(head); } //實現鏈表逆序. pNode reverseList(pNode Header) { //空鏈表或只有一個節點. if(Header == NULL || Header->pNext == NULL) return 0; //pre,cur分別指向首節點,第二個節點. pNode pre = Header->pNext; pNode cur = pre->pNext; pre->pNext = NULL; pNode next = NULL; while(NULL != cur) { next = cur->pNext; cur->pNext = pre; pre = cur; cur = next; } Header->pNext = pre;//有頭結點的鏈表. return Header; } //打印鏈表. void printList(pNode head) { pNode start = head->pNext; while(start) { cout << start->value << " "; start = start->pNext; } } int main() { pNode pl = creat(); printList(pl); cout << endl; printList(reverseList(pl)); return 0; }6. 查找子字符串出現次數,并從原字符串中刪除。
[cpp] view plain copy print?/* 編寫函數,string deletestring(string str,string sub_str)從str中查找 匹配的字符串sub_str,采用最左匹配,且輸出形式為str+"_"+匹配的次數。 */ #include <iostream> using namespace std; //刪除指定位置字符串. void delSpecifiedSubString(char *str,int start,int end) { char *p1 = &str[start]; char *p2 = &str[end + 1]; while((*p1++ = *p2++) != '/0'); } int delSubString(char *str,char *subStr) { int count = 0; int index = 0; char *pstr = str; char *psubStr = subStr; if(str == NULL ||subStr == NULL) return 0; while(*pstr != '/0') { if(*pstr == *psubStr) { pstr++; psubStr++; index++; } else { psubStr = subStr; pstr = pstr - index + 1; index = 0; } if(*psubStr == '/0') { count++; psubStr = subStr; delSpecifiedSubString(str, pstr - index - str, pstr - 1 - str); } } cout << str << " " << count << endl; return count; } int main() { char str[50]; char subStr[30]; cout << "輸入str和subStr:" << endl; gets(str); gets(subStr); delSubString(str,subStr); return 0; }[cpp] view plain copy print?<span style="color:#ff0000;">//錯誤有做法!!!</span> #include <iostream> #include <string> using namespace std; char *fun(char *str,char *subStr) { string s(str); while(s.find(subStr) != string::npos) { cout << s.find(subStr) << endl; //從s中刪除找到的子字符串subStr. s.erase(s.find(subStr),strlen(subStr)); } char *m = new char[50]; strcpy(m,s.c_str()); //注意這邊的string轉成char*返回值是const char*,不能直接將其return, //而且不能直接賦給char*,需要用strcpy來做。 return m; } int main() { char str[50]; char subStr[30]; gets(str); gets(subStr); cout << str << endl; cout << subStr << endl; cout << fun(str,subStr) << endl; return 0; }
[cpp] view plain copy print?<span style="color:#ff0000;">//功能:刪除str中的subStr. //正確的方法!!!</span> #include <iostream> #include <string> using namespace std; char *fun(char *str,char *subStr) { string s(str); int index = 0; while(s.find(subStr,index) != string::npos) { index = s.find(subStr,index); //從s中刪除找到的子字符串subStr. s.erase(s.find(subStr),strlen(subStr)); index++; } char *m = new char[50]; strcpy(m,s.c_str()); //注意這邊的string轉成char*返回值是const char*,不能直接將其return, //而且不能直接賦給char*,需要用strcpy來做。 return m; } int main() { char str[50]; char subStr[30]; gets(str); gets(subStr); cout << "str:" << str << endl; cout << "subStr:" << subStr << endl; cout << "result:" << fun(str,subStr) << endl; return 0; }
[cpp] view plain copy print?/*將一個字符串的元音字母復制到另一個字符串,并排序(30分) 問題描述: 有一字符串,里面可能包含英文字母(大寫、小寫)、數字、特殊字符,現在需要實現一函數,將此字符串中的元音字母挑選出來,存入另一個字符串中,并對字符串中的字母進行從小到大的排序(小寫的元音字母在前,大寫的元音字母在后,依次有序)。 說明: 1、 元音字母是a,e,i,o,u,A,E,I,O,U。 2、 篩選出來的元音字母,不需要剔重; 最終輸出的字符串,小寫元音字母排在前面,大寫元音字母排在后面,依次有序。 要求實現函數: void sortVowel (char* input, char* output); 【輸入】 char* input,表示輸入的字符串 【輸出】 char* output,排好序之后的元音字符串。 【返回】 無 示例 輸入:char *input = “Abort!May Be Some Errors In Out System. “ 輸出:char *output =“aeeeooAEIO */ #include <iostream> using namespace std; void sortVowel(char *input,char *output) { char arr[10] = {'a','e','i','o','u','A','E','I','O','U'}; int number[10] = {0}; int i = 0; int sum = 0; while('/0'!= *input) { for(i = 0;i < 10;i++) { if(arr[i] == *input) { number[i]++; break; } } input++; } //計算總的出現元音字母的次數. for(i = 0;i < 10;i++) sum += number[i]; output = new char[sum+1]; char *start = output; for(i = 0;i < 10;i++) { while(number[i]--) *start++ = arr[i]; } *start = '/0'; while(*output!= '/0') cout << *output++; cout << endl; } int main() { char input[] = "Abort!May Be Some Errors In Out System."; char *output = NULL; sortVowel(input,output); return 0; }
新聞熱點
疑難解答
圖片精選