国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++上機(jī)

2019-11-11 03:05:55
字體:
供稿:網(wǎng)友

1、選秀節(jié)目打分,分為專家評(píng)委和大眾評(píng)委,score[]數(shù)組里面存儲(chǔ)每個(gè)評(píng)委打的分?jǐn)?shù),judge_type[]里存儲(chǔ)與 score[]數(shù)組對(duì)應(yīng)的評(píng)委類別,judge_type[i] == 1,表示專家評(píng)委,judge_type[i] == 2,表示大眾評(píng)委,n表示評(píng)委總數(shù)。打分規(guī)則如下:專家評(píng)委和大眾評(píng)委的分?jǐn)?shù)先分別取一個(gè)平均分(平均分取整),然后,總分 = 專家評(píng)委平均分  * 0.6 +大眾評(píng)委 * 0.4,總分取整。如果沒有大眾評(píng)委,則總分 =專家評(píng)委平均分,總分取整。函數(shù)最終返回選手得分。

            函數(shù)接口   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、給定一個(gè)數(shù)組input[],如果數(shù)組長(zhǎng)度n為奇數(shù),則將數(shù)組中最大的元素放到 output[]數(shù)組最中間的位置,如果數(shù)組長(zhǎng)度n為偶數(shù),則將數(shù)組中最大的元素放到 output[]數(shù)組中間兩個(gè)位置偏右的那個(gè)位置上,然后再按從大到小的順序,依次在第一個(gè)位置的兩邊,按照一左一右的順序,依次存放剩下的數(shù)。

      例如: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}

             函數(shù)接口   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、操作系統(tǒng)任務(wù)調(diào)度問題。操作系統(tǒng)任務(wù)分為系統(tǒng)任務(wù)和用戶任務(wù)兩種。其中,系統(tǒng)任務(wù)的優(yōu)先級(jí) < 50,用戶任務(wù)的優(yōu)先級(jí) >= 50且 <= 255。優(yōu)先級(jí)大于255的為非法任務(wù),應(yīng)予以剔除。現(xiàn)有一任務(wù)隊(duì)列task[],長(zhǎng)度為n,task中的元素值表示任務(wù)的優(yōu)先級(jí),數(shù)值越小,優(yōu)先級(jí)越高。函數(shù)scheduler實(shí)現(xiàn)如下功能,將task[]中的任務(wù)按照系統(tǒng)任務(wù)、用戶任務(wù)依次存放到 system_task[]數(shù)組和 user_task[]數(shù)組中(數(shù)組中元素的值是任務(wù)在task[]數(shù)組中的下標(biāo)),并且優(yōu)先級(jí)高的任務(wù)排在前面,優(yōu)先級(jí)相同的任務(wù)按照入隊(duì)順序排列(即先入隊(duì)的任務(wù)排在前面),數(shù)組元素為-1表示結(jié)束。

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

            函數(shù)接口   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序列中的任務(wù)優(yōu)先級(jí)和下標(biāo)存入任務(wù)結(jié)構(gòu)中.      for(i = 0;i < n; i++)      {          task_list[i].priority = task[i];          task_list[i].index = i;      }            //根據(jù)優(yōu)先級(jí)對(duì)任務(wù)排序.      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;      //將任務(wù)歸類存入不同數(shù)組.      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;            //輸出歸類結(jié)果.      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.身份證號(hào)碼合法性判斷 問題描述 

我國(guó)公民的身份證號(hào)碼特點(diǎn)如下: 

1.長(zhǎng)度為18位 

2.1-17位只能為數(shù)字 

3.第十八位可以是數(shù)字或者小寫英文字母 

4.身份證號(hào)碼的第7-14位表示持有人生日的年月日信息 

 請(qǐng)實(shí)現(xiàn)身份證號(hào)碼合法性判斷的函數(shù),除滿足以上要求外,需要對(duì)持有人生日的年月日信息進(jìn)行校驗(yàn),年份大于等于1900,小于等于2100年。需要考慮閏年、大小月的情況。所謂閏年,能被4整除且不能被100整除或能被400整除的年份。閏年2月份為29天,非閏年的2月份為28天。其他情況的合法性校驗(yàn),不用考慮  函數(shù)返回值: 1.如果身份證號(hào)合法,返回0 2.如果身份證號(hào)長(zhǎng)度不合法,返回1 3.如果身份證號(hào)第1-17位含有非數(shù)字的字符,返回2 4.如果第十八位既不是數(shù)字也不是英文小寫字母x,返回3 5.如果身份證號(hào)的年信息非法,返回4 6.如果身份證號(hào)的月信息非法,返回5 7.如果身份證號(hào)的日信息非法,返回6(請(qǐng)注意閏年的情況)  注:除成功的情況外,以上其他情況合法性判斷的優(yōu)先級(jí)依次降低,也就是說,如果判斷出長(zhǎng)度不合法,直接返回1即可,不需要再做其他合法性判斷  要求實(shí)現(xiàn)函數(shù) 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;    //創(chuàng)建鏈表.     Node* creat()      {      int nuberOfNode = 5;      int data;      pNode head = new Node;              if(NULL == head)          cout << "分配內(nèi)存失敗/r/n";            head->pNext = NULL;                pNode p = head;      pNode s = NULL;      cout << "請(qǐng)輸入數(shù)據(jù):/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);      }        //實(shí)現(xiàn)鏈表逆序.  pNode reverseList(pNode Header)  {      //空鏈表或只有一個(gè)節(jié)點(diǎn).      if(Header == NULL || Header->pNext == NULL)          return 0;            //pre,cur分別指向首節(jié)點(diǎn),第二個(gè)節(jié)點(diǎn).      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;//有頭結(jié)點(diǎn)的鏈表.            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. 查找子字符串出現(xiàn)次數(shù),并從原字符串中刪除。

[cpp] view plain copy print?/* 編寫函數(shù),string deletestring(string str,string sub_str)從str中查找 匹配的字符串sub_str,采用最左匹配,且輸出形式為str+"_"+匹配的次數(shù)。 */  #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;">//錯(cuò)誤有做法!!!</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轉(zhuǎn)成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轉(zhuǎn)成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?/*將一個(gè)字符串的元音字母復(fù)制到另一個(gè)字符串,并排序(30分) 問題描述: 有一字符串,里面可能包含英文字母(大寫、小寫)、數(shù)字、特殊字符,現(xiàn)在需要實(shí)現(xiàn)一函數(shù),將此字符串中的元音字母挑選出來,存入另一個(gè)字符串中,并對(duì)字符串中的字母進(jìn)行從小到大的排序(小寫的元音字母在前,大寫的元音字母在后,依次有序)。   說明: 1、  元音字母是a,e,i,o,u,A,E,I,O,U。 2、  篩選出來的元音字母,不需要剔重;   最終輸出的字符串,小寫元音字母排在前面,大寫元音字母排在后面,依次有序。   要求實(shí)現(xiàn)函數(shù): 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++;      }            //計(jì)算總的出現(xiàn)元音字母的次數(shù).      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;  }  
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 湛江市| 深圳市| 广元市| 淳安县| 宾阳县| 双辽市| 南皮县| 康定县| 太仆寺旗| 米泉市| 邹平县| 曲靖市| 保亭| 定日县| 丁青县| 泸水县| 安龙县| 高清| 偏关县| 黄浦区| 屏边| 定西市| 北流市| 平阳县| 沽源县| 临高县| 溧水县| 壤塘县| 庆城县| 葫芦岛市| 普安县| 鸡泽县| 上虞市| 陕西省| 镇巴县| 兴国县| 苏尼特右旗| 稷山县| 陇南市| 淅川县| 突泉县|