HUAWEI

 2012華爲校園招聘機考試題總結(廈門大學)

 1、刪除子串,只要是原串中有相同的子串就刪掉,不管有多少個,返回子串個數。

2、約瑟夫環是一個數學的應用問題:已知n個人(以編號123...n分別表示)圍坐在一張圓桌周圍。從編號爲k的人開始報數,數到m的那個人出列;他的下一個人又從1開始報數,數到m的那個人又出列;依此規律重複下去,直到圓桌周圍的人全部出列。

3、比較一個數組的元素 是否爲迴文數組

4、 數組比較(20分)n lang=EN-US>

5、約瑟夫問題

6、 手機號碼合法性判斷(20分)

7、 數組比較(20分)

8、簡單四則運算

9、選秀節目打分

10、給定一個數組input[]

11、操作系統任務調度問題。

12 從兩個數組的最後一個元素比較兩個數組中不同元素的個數,

13、輸入一個字符串,用指針求出字符串的長度。

14、使用C語言實現字符串中子字符串的替換

15、編寫一個程序實現功能:將字符串”Computer Secience”賦給一個字符數組,然後從第一個字母開始間隔的輸出該串,用指針完成。

16、使用C語言實現字符串中子字符串的替換

17、編寫一個程序實現功能:將兩個字符串合併爲一個字符串並且輸出,用指針實現。

18、算分數的問題,去掉一個最高分一個最低分,求平均分 

19、對一個數組,將數組中偶數從大到小排序,奇數從小到大排序,;

20、簡單四則運算

21、選秀節目打分,

22、給定一個數組input[] guage:ZH-CN;mso-bidi-language:AR-SA'>、選秀節目打分,

23、操作系統任務調度問題。

1、刪除子串,只要是原串中有相同的子串就刪掉,不管有多少個,返回子串個數。

#include <stdio.h>

#include <stdlib.h>

#include <assert.h>

#include <string.h>

int delete_sub_str(const char *str,constchar *sub_str,char *result)

{

     assert(str!= NULL && sub_str != NULL);

     constchar *p,*q;

     char *t,*temp;

     p= str;

     q =sub_str;

     t= result;

     intn,count = 0;

     n= strlen(q);

     tmep= (char *)malloc(n+1);

     memset(temp,0x00,n+1);

     while(*p)

     {

         memcpy(temp,p,n);

         if(strcmp(temp,q)== 0 )

         {

              count++;

              memset(temp;0x00,n+1);

              p= p + n;

         }

         else

         {   

              *t= *p;

              p++;

              t++;

              memset(temp,0x00,n+1);

         }   

     }

     free(temp);

     returncount;

}

int main()

{

     chars[100] = {‘\0’};

     intnum = delete_sub_str(“123abc12de234fg1hi34j123k”,”123”,s);

     printf(“Thenumber of sub_str is %d\r\n”,num);

     printf(“Theresult string is %s\r\n”,s);

}

2、約瑟夫環是一個數學的應用問題:已知n個人(以編號1,2,3...n分別表示)圍坐在一張圓桌周圍。從編號爲k的人開始報數,數到m的那個人出列;他的下一個人又從1開始報數,數到m的那個人又出列;依此規律重複下去,直到圓桌周圍的人全部出列。

#include<stdio.h>

#include<stdlib.h>

typedef struct Node

{   

     intnum;

     structNode *next;

}LinkList;

LinkList *creat(int n)

{

     LinkList*p,*q,*head;

     inti=1; 

     p=(LinkList*)malloc(sizeof(LinkList));

   p->num=i;

     head=p;

   for(i=2;i<=n;i++)

   {

       q=(LinkList *)malloc(sizeof(LinkList));

       q->num=i;

       p->next=q;

       p=q; 

   }

   p->next=head;          /*使鏈表尾指向鏈表頭 形成循環鏈表*/

  return head;

}

void fun(LinkList *L,int m)

{

     inti;

     LinkList*p,*s,*q;

     p=L;

     printf("出列順序爲:");

     while(p->next!=p)

     {

         for(i=1;i<m;i++)

         {    q=p;

              p=p->next;

         }

         printf("%5d",p->num);

         s=p;

         q->next=p->next;

         p=p->next;

         free(s);

     }

     printf("%5d\n",p->num);

}

int main()

{

     LinkList*L;

     intn, m;

     n=9;

     m=5;

     L=creat(n);

     fun(L,m);

     return0;

}

3、比較一個數組的元素 是否爲迴文數組

#include<stdio.h>

#include<string.h>

 

 

int huiwen(charstr[])

{

     int i,len,k=1;

     len=strlen(str);

     for(i=0;i<len/2;i++)

     {

         if(str[i]!=str[len-i-1])

         {

              k=1;

              break;

         }

     }

     if(k==0)

        printf("%s 不是一個迴文數\n",str);

     else

         printf("%s 是一個迴文數\n",str);

}

void main()

{      

     char str[100] = {0};    

     int i;       

     int len;

     printf("Input a string:");        /*提示輸入Input a string:*/

     scanf("%s", str);                  /*scan()函數輸入一個字符串:*/

     huiwen(str);

}

4、 數組比較(20分)
• 問題描述: 
比較兩個數組,要求從數組最後一個元素開始逐個元素向前比較,如果2個數組長度不等,則只比較較短長度數組個數元素。請編程實現上述比較,並返回比較中發現的不相等元素的個數
比如:
數組{1,3,5}和數組{77,21,1,3,5}按題述要求比較,不相等元素個數爲0
數組{1,3,5}和數組{77,21,1,3,5,7}按題述要求比較,不相等元素個數爲3
• 要求實現函數: 
int array_compare(int len1, int array1[], int len2, int array2[])
【輸入】 int len1:輸入被比較數組1的元素個數;
int array1[]:輸入被比較數組1;
int len2:輸入被比較數組2的元素個數;
int array2[]:輸入被比較數組2;
【輸出】 無 
【返回】 不相等元素的個數,類型爲int
• 示例 
1) 輸入:int array1[] = {1,3,5},int len1 = 3,int array2[] = {77,21,1,3,5},int len2 = 5
函數返回:0
2) 輸入:int array1[] = {1,3,5},int len1 = 3,int array2[] = {77,21,1,3,5,7},int len2 = 6
函數返回:3

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

intarray_compare(int len1, int array1[], int len2, int array2[])

{

     int count=0;

     for( ;len1>=0&&len2>=0;len1--,len2--)

     {

              if(array1[len1-1]==array2[len2-1])

              {

             count++;

              }   

     }

     return count;

}

int main()

{

     int result=0;

     int array1[]={1,3,5};

     int len1=3;

     int array2[]={77,12,1,3,5};

     int len2=5;

     result=array_compare( len1, array1,  len2, array2);  ///result=array_compare( len1, array1[],  len2, array2[]);不能這樣

                                                        // 函數形參中永遠只是傳得首地址,不能傳數組                       切記切記!!!!!!

     printf("the result is %d",result);

}
5、約瑟夫問題


• 問題描述: 
輸入一個由隨機數組成的數列(數列中每個數均是大於0的整數,長度已知),和初始計數值m。從數列首位置開始計數,計數到m後,將數列該位置數值替換計數值m,並將數列該位置數值出列,然後從下一位置從新開始計數,直到數列所有數值出列爲止。如果計數到達數列尾段,則返回數列首位置繼續計數。請編程實現上述計數過程,同時輸出數值出列的順序

比如: 輸入的隨機數列爲:3,1,2,4,初始計數值m=7,從數列首位置開始計數(數值3所在位置)
第一輪計數出列數字爲2,計數值更新m=2,出列後數列爲3,1,4,從數值4所在位置從新開始計數
第二輪計數出列數字爲3,計數值更新m=3,出列後數列爲1,4,從數值1所在位置開始計數
第三輪計數出列數字爲1,計數值更新m=1,出列後數列爲4,從數值4所在位置開始計數
最後一輪計數出列數字爲4,計數過程完成。
輸出數值出列順序爲:2,3,1,4。

• 要求實現函數: 
void array_iterate(int len, int input_array[], int m, int output_array[])
【輸入】 int len:輸入數列的長度;
int intput_array[]:輸入的初始數列
int m:初始計數值
【輸出】 int output_array[]:輸出的數值出列順序
【返回】 無

• 示例 
輸入:int input_array[] = {3,1,2,4},int len = 4, m=7
輸出:output_array[] = {2,3,1,4}

////////////循環鏈表實現//////////////////////

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

typedef structNode

{

     int num;

     struct node *next;

} node;

node *creat(intlen , int input_array[])

{

     node *h,*s,*p;

     int i;

     h=(node*)malloc(sizeof(node));

     h->num=input_array[0];

     p=h;

     for(i=1;i<len;i++)

      {

          s=(node*)malloc(sizeof(node));

          s->num=input_array[i];

          p->next=s;

          p=s;

      }

      p->next=h;

      

      return (h);

}

voidarray_iterate(int len, int input_array[], int m)

{

     node *q,*p,*s;

     int i=0,j=0,k;

     int output_array[4];

     p=creat(len,input_array);

     while(p->next!=p)

     {

         for(i=1;i<m;i++)

         {

              q=p;

              p=p->next;

         }

         m=p->num;

        printf("%5d",m);

         output_array[j++]=m;

             

         s=p;

         q->next=p->next;

         p=p->next;

         free(s);

         s=NULL;

     }

         m=p->num;

        printf("%5d\n",m);

         output_array[j]=p->num;

         k=j;

         for(j=0 ; j<=k; j++)

         {

              printf("%5d",output_array[j]);

         }

 

}

int main()

{

     int input_array[]={3,1,2,4};

     int len=4;

     int m=7;

     int output_array[4];

     array_iterate(len,  input_array, m, output_array);

}

 

6、 手機號碼合法性判斷(20分)

l         問題描述:

我國大陸運營商的手機號碼標準格式爲:國家碼+手機號碼,例如:8613912345678。特點如下:

1、  長度13位;

2、  以86的國家碼打頭;

3、  手機號碼的每一位都是數字。

 

請實現手機號碼合法性判斷的函數要求:

1)  如果手機號碼合法,返回0;

2)  如果手機號碼長度不合法,返回1

3)  如果手機號碼中包含非數字的字符,返回2;

4)  如果手機號碼不是以86打頭的,返回3;

【注】除成功的情況外,以上其他合法性判斷的優先級依次降低。也就是說,如果判斷出長度不合法,直接返回1即可,不需要再做其他合法性判斷。

l         要求實現函數:

intverifyMsisdn(char* inMsisdn)

【輸入】 char* inMsisdn,表示輸入的手機號碼字符串。

【輸出】  無

【返回】  判斷的結果,類型爲int。

l         示例

輸入:  inMsisdn = “869123456789“

輸出:  無

返回:  1

輸入:  inMsisdn = “88139123456789“

輸出:  無

返回:  3

輸入:  inMsisdn = “86139123456789“

輸出:  無

返回:  0

 

#include<stdio.h>

#include<stdlib.h>

#include<assert.h>

#include<string.h>

#defineLENGTH  13

 

intverifyMsisdn(char *inMsisdn)

{

    

     char *pchar=NULL;

     assert(inMsisdn!=NULL);

     if(LENGTH==strlen(inMsisdn))

     {

         if(('8'==*inMsisdn)&&(*(inMsisdn+1)=='6'))

         {

              while(*inMsisdn!='\0')

              {

                   if((*inMsisdn>='0')&&(*inMsisdn<='9'))

                   inMsisdn++;

                   else

                   return 2 ;

              }

         }

         else

              return 3;

     }

     else

         return 1;

     return 0;

}

 

int main()

{

     char *pchar=NULL;

     unsigned char ichar=0;

     int result;

     switch(ichar)

     {

     case 0:

         pchar="8612345363789";break;

     case 1:

         pchar="861111111111111";break;

     case 2:

         pchar="86s1234536366"; break;

     default:

         break;

     }

   result =verifyMsisdn(pchar);

    printf("result is %d\n",result);

}

 

 

7、 數組比較(20分)
• 問題描述: 
比較兩個數組,要求從數組最後一個元素開始逐個元素向前比較,如果2個數組長度不等,則只比較較短長度數組個數元素。請編程實現上述比較,並返回比較中發現的不相等元素的個數
比如:
數組{1,3,5}和數組{77,21,1,3,5}按題述要求比較,不相等元素個數爲0
數組{1,3,5}和數組{77,21,1,3,5,7}按題述要求比較,不相等元素個數爲3

• 要求實現函數: 
int array_compare(int len1, int array1[], int len2, int array2[])

【輸入】 int len1:輸入被比較數組1的元素個數;
int array1[]:輸入被比較數組1;
int len2:輸入被比較數組2的元素個數;
int array2[]:輸入被比較數組2;
【輸出】 無 
【返回】 不相等元素的個數,類型爲int

• 示例 
1) 輸入:int array1[] = {1,3,5},int len1 = 3,int array2[] = {77,21,1,3,5},int len2 = 5
函數返回:0
2) 輸入:int array1[] = {1,3,5},int len1 = 3,int array2[] = {77,21,1,3,5,7},int len2 = 6
函數返回:3

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

 

intarray_compare(int len1, int array1[], int len2, int array2[])

{

     int count=0;

 

     for( ;len1>=0&&len2>=0;len1--,len2--)

     {

              if(array1[len1-1]==array2[len2-1])

              {

             count++;

              }   

     }

     return count;

}

 

int main()

{

     int result=0;

     int array1[]={1,3,5};

     int len1=3;

 

     int array2[]={77,12,1,3,5};

     int len2=5;

 

     result=array_compare(len1, array1, len2,array2); 

///result=array_compare(len1, array1[],  len2, array2[]);不能這樣

// 函數形參中永遠只是傳得首地址,不能傳數組                       切記切記!!!!!!

     printf("the result is %d",result);

}


8、簡單四則運算

• 問題描述: 輸入一個只包含個位數字的簡單四則運算表達式字符串,計算該表達式的值
注: 1、表達式只含 +, -, *, / 四則運算符,不含括號
2、表達式數值只包含個位整數(0-9),且不會出現0作爲除數的情況
3、要考慮加減乘除按通常四則運算規定的計算優先級
4、除法用整數除法,即僅保留除法運算結果的整數部分。比如8/3=2。輸入表達式保證無0作爲除數情況發生
5、輸入字符串一定是符合題意合法的表達式,其中只包括數字字符和四則運算符字符,除此之外不含其它任何字符,不會出現計算溢出情況
• 要求實現函數: 
int calculate(int len,char *expStr)
【輸入】 int len: 字符串長度;
char *expStr: 表達式字符串;
【輸出】 無
【返回】 計算結果

• 示例 
1) 輸入:char *expStr = “1+4*5-8/3”
函數返回:19
2) 輸入:char *expStr = “8/3*3”
函數返回:6

#include <stdio.h>

 

/*

 *author by wanww

 *time: 2011-09-07

 */

using namespace std;

int array_compare(int len1, intarray1[], int len2, int array2[])

{

   if(len1 == len2) {

         intcount = 0;

         for(int i=0;i<len1;i++)

         {

              if(array1[i]!=array2[i])count++;

         }

         returncount;

     }else if(len1<len2) {

         returnarray_compare(len1, array1,len1,array2+len2-len1);

     }else {

         returnarray_compare(len2,array1+len1-len2,len2,array2);

     }

}

 

void array_iterate(int len, intinput_array[], int m,int output_array[])

{

     int* flag = new int[len];

     memset(flag,0,len*4);

     inthasout=0;  //已經出列的數字個數

     intstart = 0; //開始的下標號

     intj=0;      //當前以報到的數字

     while(true)

     {

         if(flag[start]== 0)   //當前元素還沒出列

         {   

            j++;      

              if(j==m) //已經計數到m,當前start下標的元素出列

              {

                  output_array[hasout] = input_array[start];

                   flag[start]= 1;   //標記當前元素已經出列

                   hasout++;

                   if(hasout== len) break; //所有的元素都已經出列,結束程序

                   //初始化下一輪的數字

                   j= 0;

                   m= input_array[start];

              }

         }  

         start++;

         if(start==len)start = 0;

        

     }

     delete[] flag;

    

}

int calculate(int len,char *expStr)

{

     struct  {

         charopdata[200];

         inttop;

     }opstack;

     //定義操作符棧

     opstack.top= -1;

     inti=0;//遍歷字符串的下標

     intt=0;//當前後綴表達式的長度

     charch = expStr[i];

     while(ch!='\0')

     {

         switch(ch)

         {

              case'+':

              case'-':

                   while(opstack.top != -1)

                   {

                       expStr[t]= opstack.opdata[opstack.top];

                       opstack.top--;

                       t++;

                   }

                   opstack.top++;

                   opstack.opdata[opstack.top]= ch;

                   break;

              case'*':

              case'/':

                   while(opstack.top != -1 && (opstack.opdata[opstack.top] =='*' ||opstack.opdata[opstack.top] =='/') )

                   {

                       expStr[t]= opstack.opdata[opstack.top];

                       opstack.top--;

                       t++;

                   }

                   opstack.top++;

                   opstack.opdata[opstack.top]= ch;

                   break;

              default:

                   expStr[t] = ch;

                   t++;

                   break;

         }

         i++;

         ch= expStr[i];

     }

     while(opstack.top != -1)//將棧中所有的剩餘的運算符出棧

     {

         expStr[t] = opstack.opdata[opstack.top];

         opstack.top--;

         t++;

     }

     expStr[t]='\0';

     struct   {

         intnumeric[200];

         inttop;

     }data;

     data.top = -1;

     i=0;

     ch = expStr[i];

    while (ch!='\0')

   {

         if(ch>='0' && ch <= '9' )

         {

              data.top++;

              data.numeric[data.top]= ch-'0';

         }

         elseif('+' == ch)

         {

              inttmp = data.numeric[data.top-1]  +data.numeric[data.top];

              data.top--;

              data.numeric[data.top]= tmp;

         }

         elseif('-' == ch)

         {

              inttmp = data.numeric[data.top-1]  -data.numeric[data.top];

              data.top--;

              data.numeric[data.top]= tmp;

         }

         elseif('*' == ch)

         {

              inttmp = data.numeric[data.top-1]  *data.numeric[data.top];

              data.top--;

              data.numeric[data.top]= tmp;

         }

         elseif('/' == ch)

         {

             if(data.numeric[data.top] == 0)

              {

                   printf("cannotbe zero of the divide\n");

                   exit(1);

              }

              inttmp = data.numeric[data.top-1] / data.numeric[data.top];

              data.top--;

              data.numeric[data.top]= tmp;

         }

         i++;

         ch= expStr[i];

   }

     returndata.numeric[data.top];

}

void main()

{

     intarray1[] = {1,3,5};

     intlen1 = 3;

     intarray2[] = {77,21,1,3,5,7};

     intlen2 = 6;

     intcount =   array_compare(sizeof(array1)/sizeof(int),array1,sizeof(array2)/sizeof(int),array2);

     printf("%d\n",count);

        printf("*****************************************************\n");

     intinput_array[] = {3,1,2,4};

     intlen = 4;

     intm=7;

    

     int* output_array = new int[sizeof(input_array)/sizeof(int)];

    

     array_iterate(4,input_array,7,output_array);

         

     for(int i=0;i<sizeof(input_array)/sizeof(int);i++)

     {

         printf("%d",output_array[i]);

     }

        

     delete[] output_array;

     printf("\n*****************************************************\n");

     charexpStr[] = "8/3*3";

   int result = calculate(strlen(expStr),expStr);

     printf("%s\n",expStr);

     printf("%d\n",result);

}

9、選秀節目打分,分爲專家評委和大衆評委,score[] 數組裏面存儲每個評委打的分數,judge_type[] 裏存儲與 score[] 數組對應的評委類別,judge_type[i] == 1,表示專家評委,judge_type[i] == 2,表示大衆評委,n表示評委總數。打分規則如下:專家評委和大衆評委的分數先分別取一個平均分(平均分取整),然後,總分 = 專家評委平均分  * 0.6 + 大衆評委 * 0.4,總分取整。如果沒有大衆評委,則 總分 = 專家評委平均分,總分取整。函數最終返回選手得分。

 函數接口   int cal_score(intscore[], int judge_type[], int n) 

#include<stdio.h>

#include<string.h>

#include<iostream.h>

#include<conio.h>

#define N 5

 

int cal_score(int score[],int judge_type[], int n)

{

     int expert=0;

    int dazhong=0;

     int zongfen=0;

     int i;

     int number=0;

    

     for(i=0;i<N;i++)

     {

         if(judge_type[i]==1)

         {

              expert=expert+score[i];

              number++;

         }

         else dazhong=dazhong+score[i];

     }

     if(number==N)

     {

         zongfen=(int)(expert/N);

     }

     else

    {

         expert=(int)(expert/number);

         dazhong=(int)(dazhong/(N-number));

         zongfen=int(0.6*expert+0.4*dazhong);

     }

     return zongfen;

}

int main()

{

     int score[N];

     int judge_type[N];

     int numberlast=0;

     int i;

     printf("please input the %d score:\n",N);

     for(i=0;i<N;i++)

         scanf("%d",&score[i]);

     printf("please input thelevel(1:expert,2:dazhong)\n");

     for(i=0;i<N;i++)

         scanf("%d",&judge_type[i]);

     numberlast=cal_score(score,judge_type,N);

     printf("the last score is %d\n",numberlast);

     return 0;

}

10、給定一個數組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}

#include<stdio.h>

#include<string.h>

#include<conio.h>

void sort(int input[], int n, intoutput[])

{

     inti,j;

     intk=1;

     inttemp;

     intmed;

     for(i=0;i<n;i++)

         for(j=0;j<n-i;j++)

              if(input[j]>input[j+1])

              {temp=input[j];input[j]=input[j+1];input[j+1]=temp;}

              if(n%2!=0)

              {

                   for(i=0;i<n;i++)

                       printf("%2d",input[i]);

                   printf("\n");

                   med=(n-1)/2;

                   output[med]=input[n-1];

                   for(i=1;i<=med;i++)

                   {

                       output[med-i]=input[n-1-k];

                       output[med+i]=input[n-2-k];

                       k=k+2;

                      

                   }

              }

              else

              {

                  

                   for(i=0;i<n;i++)

                       printf("%2d",input[i]);

                   printf("\n");

                   med=n/2;

                   output[med]=input[n-1];

                   for(i=1;i<=med-1;i++)

                   {

                       output[med-i]=input[n-1-k];

                       output[med+i]=input[n-2-k];

                       k=k+2;  

                   }

                   output[0]=input[0];                 

              }   

              for(i=0;i<n;i++)

                       printf("%2d",output[i]);

                   printf("\n");

}

int main()

{

     inta[6]={3,6,1,9,7,8};

     intb[6]={0};

     for(inti=0;i<6;i++)

         printf("%2d",a[i]);

     printf("\n");

     sort(a,6,b);

     return0;

}

 

11、操作系統任務調度問題。操作系統任務分爲系統任務和用戶任務兩種。其中,系統任務的優先級 < 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, intsystem_task[], int user_task[])

#include<stdio.h>

#include<string.h>

#include<malloc.h>

#include<iostream.h>

 

void scheduler1(int task[], int n, intsystem_task[], int user_task[])

{

     inti;

     intj=0;

     int*p,*pp,*p_user,*pp_user;

     intindex=0;

     intcount,count2;

     intmin=0;

     intk=0;

     p=(int*)malloc(sizeof(int)*n);

     for(i=0;i<n;i++)

         p[i]=0;

     pp=(int*)malloc(sizeof(int)*n);

     for(i=0;i<n;i++)

         pp[i]=0;

     p_user=(int*)malloc(sizeof(int)*n);

     for(i=0;i<n;i++)

         p_user[i]=0;

     pp_user=(int*)malloc(sizeof(int)*n);

     for(i=0;i<n;i++)

         pp_user[i]=0;

    

     for(i=0;i<n;i++)

     {

         if(task[i]<50)

         {

              {

                   system_task[j]=task[i];

                   pp[j]=i;

                   j++;

              }

              count=j;

         }

        

         elseif(task[i]<=255)

         {

        

              {

                   user_task[k]=task[i];

                   pp_user[k]=i;

                   k++;

              }

              count2=k;

         }

         elsetask[i]=task[i];

    

     }

    

     for(i=0;i<count;i++)

         printf("%3d",system_task[i]);

     printf("\n");

    

    

     for(i=0;i<count;i++)

     {

         min=system_task[0];

         for(j=1;j<count;j++)

         {

             

              if(system_task[j]<min)

              {

                   min=system_task[j];   

                   p[i]=j;      

              }

             

         }

         system_task[p[i]]=51;

     }

    

   pp[count]=-1;

     for(i=0;i<count;i++)

         printf("%3d",pp[p[i]]);

     printf("%3d\n",pp[count]);

    

    

     /***********************************************************/

    

     for(i=0;i<count2;i++)

         printf("%4d",user_task[i]);

     printf("\n");

    

     for(i=0;i<count2;i++)

     {

         min=user_task[0];

         for(j=1;j<count2;j++)

         {

             

              if(user_task[j]<min)

              {

                   min=user_task[j]; 

                   p_user[i]=j;      

              }

             

         }

         user_task[p_user[i]]=256;

     }

    

   pp_user[count2]=-1;

     for(i=0;i<count2;i++)

         printf("%4d",pp_user[p_user[i]]);   

     printf("%3d\n",pp_user[count2]);

    

}

 

int main()

{

     inttask[9]={0, 30, 155, 1, 80, 300,170, 40, 99};

     intsystem_task[9]={0};

     intuser_task[9]={0};

     scheduler1(task,9,system_task,user_task);

     return0;

}

12、 從兩個數組的最後一個元素比較兩個數組中不同元素的個數,如有array1[5]={77,21,1,3,5},array2[3]={1,3,5},從array1[4]與array2[2]比較開始,到array1[2]與array[0]比較結束。這樣得出它們不同的元素個數爲0,若array1[6]={77,21,1,3,5,7},那麼他們不同的元素爲3。

  函數原型爲 int compare_array( int len1, intarray1[], int len2, int array2[] );

  其中,len1與len2分別爲數組array1[]和array2[]的長度,函數返回值爲兩個數組不同元素的個數。

   以下是上題的函數完整實現:

//diff_num.cpp

 

#include<stdio.h>

int compare_array(int len1,int array1[],intlen2,int array2[])

{

     inti,t,small,num=0;

     //把兩數組倒置

     for(i=0;i<len1/2;i++)

     {

         t=array1[i];

         array1[i]=array1[len1-i-1];

         array1[len1-i-1]=t;

     }

     for(i=0;i<len2/2;i++)

     {

         t=array2[i];

         array2[i]=array2[len2-i-1];

         array2[len2-i-1]=t;

     }

     //輸出倒置後的兩數組

/*   for(i=0;i<len1;i++)

         printf("%d",array1[i]);

     printf("\n");

     for(i=0;i<len2;i++)

         printf("%d",array2[i]);

*/   printf("\n");

     if(len1>len2)

         small=len2;

     else

         small=len1;

     num=small;

     for(i=0;i<small;i++)

     {

         if(array1[i]==array2[i])

              num--;

     }

     printf("num=%d\n",num);

     returnnum;

}

void main()

{

     intarray1[5]={77,21,1,3,5},array2[3]={1,3,5};

     intlen1=5,len2=3;

     compare_array(len1,array1,len2,array2);

}

13、輸入一個字符串,用指針求出字符串的長度。

答案:

#include <stdio.h>

int main()

{

   char str[20],  *p;

   int length=0;

   printf(“Please input a string: ”);

   gets(str);

   p=str;

   while(*p++)

   {

length++;

}

printf(“The length of stringis %d\n”, length);

return 0;

}

14、使用C語言實現字符串中子字符串的替換

描述:編寫一個字符串替換函數,如函數名爲 StrReplace(char* strSrc,char* strFind, char* strReplace),strSrc爲原字符串,strFind是待替換的字符串,strReplace爲替換字符串。

舉個直觀的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”這個字符串,把其中的“RST”替換爲“ggg”這個字符串,結果就變成了:

ABCDEFGHIJKLMNOPQgggUVWXYZ

答案一:

#include <stdio.h>

#include <string.h>

void StrReplace(char* strSrc, char*strFind, char* strReplace);

#define M 100;

void main()

{chars[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char s1[]="RST";

char s2[]="ggg";

StrReplace(s,s1,s2);

printf("%s\n",s);

}

void StrReplace(char* strSrc, char*strFind, char* strReplace)

{

        int i=0;

        int j;

        int n=strlen(strSrc);

        int k=strlen(strFind);

        for(i=0;i<n;i++)

       {

                if(*(strSrc+i)==*strFind)

                {

                        for(j=0;j<k;j++)

                        {

                               if(*(strSrc+i+j)==*(strFind+j))

                                {

                                       *(strSrc+i+j)=*(strReplace+j);

                                }

                               elsecontinue;

                        }

                }

       }

}

答案二:

#include <stdio.h>

#define MAX 100

StrReplace(char *s, char *s1, char *s2){

   char *p;

   for(; *s; s++) {

       for(p = s1; *p && *p != *s; p++);

       if(*p) *s = *(p - s1 + s2);

   }

}

int main()

{

   char s[MAX];            //s是原字符串

   char s1[MAX], s2[MAX];  //s1是要替換的

                            //s2是替換字符串

   puts("Please input the string for s:");

   scanf("%s", s);

   puts("Please input the string for s1:");

   scanf("%s", s1);

   puts("Please input the string for s2:");

   scanf("%s", s2);

   StrReplace(s, s1, s2);

   puts("The string of s after displace is:");

   printf("%s\n", s);

   return 0;

}

答案三:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define M 100

void StrReplace(char* strSrc, char*strFind, char* strReplace);

int main()

{

     chars[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

     chars1[]="RST";

     chars2[]="gggg";

     StrReplace(s,s1,s2);

     printf("%s\n",s);

     return0;

}

void StrReplace(char* strSrc, char*strFind, char* strReplace)

{

     while(*strSrc!= '\0')

     {

         if(*strSrc== *strFind)

         {

              if(strncmp(strSrc,strFind,strlen(strFind))== 0 ) 

              {

                   inti = strlen(strFind);

                   intj = strlen(strReplace);

                   printf("i= %d,j = %d\n",i,j);

                   char *q = strSrc + i;

                   printf("*q= %s\n",q);

                   while((*strSrc++= *strReplace++) != '\0');

                   printf("strSrc- 1 = %s\n",strSrc - 1);

                   printf("*q= %s\n",q);

                   while((*strSrc++= *q++) != '\0');

              }

              else

              {

                   strSrc++;

              }

         }

         else

         {

              strSrc++;

         }

     }

}

15、編寫一個程序實現功能:將字符串”Computer Secience”賦給一個字符數組,然後從第一個字母開始間隔的輸出該串,用指針完成。

答案:

#include <stdio.h>

#include <string.h>

int main()

{

     charstr[]=”Computer Science”;

   int flag=1;

     char*p=str;

     while(*p)

{

   if ( flag )

   {

      printf(“%c”,*p);

      }

       flag = (flag + 1) % 2;

       p++;

    }

     printf(“\n”);

    return 0;

}

16、使用C語言實現字符串中子字符串的替換

描述:編寫一個字符串替換函數,如函數名爲 StrReplace(char* strSrc,char* strFind, char* strReplace),strSrc爲原字符串,strFind是待替換的字符串,strReplace爲替換字符串。

舉個直觀的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”這個字符串,把其中的“RST”替換爲“ggg”這個字符串,結果就變成了:

ABCDEFGHIJKLMNOPQgggUVWXYZ

答案一:

#include <stdio.h>

#include <string.h>

void StrReplace(char* strSrc, char*strFind, char* strReplace);

#define M 100;

void main()

{chars[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char s1[]="RST";

char s2[]="ggg";

StrReplace(s,s1,s2);

printf("%s\n",s);

}

void StrReplace(char* strSrc, char*strFind, char* strReplace)

{

        int i=0;

        int j;

        int n=strlen(strSrc);

        int k=strlen(strFind);

        for(i=0;i<n;i++)

       {

                if(*(strSrc+i)==*strFind)

                {

                        for(j=0;j<k;j++)

                        {

                               if(*(strSrc+i+j)==*(strFind+j))

                                {

                                       *(strSrc+i+j)=*(strReplace+j);

                               }

                                else continue;

                        }

                }

       }

}

答案二:

#include <stdio.h>

#define MAX 100

StrReplace(char *s, char *s1, char *s2){

   char *p;

   for(; *s; s++) {

       for(p = s1; *p && *p !=*s; p++);

       if(*p) *s = *(p - s1 + s2);

   }

}

int main()

{

   char s[MAX];            //s是原字符串

   char s1[MAX], s2[MAX];  //s1是要替換的

                            //s2是替換字符串

   puts("Please input the string for s:");

   scanf("%s", s);

   puts("Please input the string for s1:");

   scanf("%s", s1);

   puts("Please input the string for s2:");

   scanf("%s", s2);

   StrReplace(s, s1, s2);

   puts("The string of s after displace is:");

   printf("%s\n", s);

   return 0;

}

答案三:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define M 100

void StrReplace(char* strSrc, char*strFind, char* strReplace);

int main()

{

     chars[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

     chars1[]="RST";

     chars2[]="gggg";

 

     StrReplace(s,s1,s2);

     printf("%s\n",s);

 

     return0;

}

void StrReplace(char* strSrc, char*strFind, char* strReplace)

{

     while(*strSrc!= '\0')

     {

         if(*strSrc== *strFind)

         {

              if(strncmp(strSrc,strFind,strlen(strFind))== 0 ) 

              {

                   inti = strlen(strFind);

                   intj = strlen(strReplace);

                   printf("i= %d,j = %d\n",i,j);

                   char *q = strSrc + i;

                   printf("*q= %s\n",q);

                   while((*strSrc++= *strReplace++) != '\0');

                   printf("strSrc- 1 = %s\n",strSrc - 1);

                   printf("*q= %s\n",q);

                   while((*strSrc++= *q++) != '\0');

              }

              else

              {

                   strSrc++;

              }

         }

         else

         {

              strSrc++;

         }

     }

}

17、編寫一個程序實現功能:將兩個字符串合併爲一個字符串並且輸出,用指針實現。

  char str1[20]={“Hello ”}, str2[20]={“World ”};

答案:

#include <stdio.h>

 

int main()

{

  char str1[20]={“Hello ”}, str2[20]={“World ”};

  char *p=str1, *q=str2;

 

  while( *p ) p++;

  while( *q )

  {

     *p = *q;

     p++;

     q++;

   }

   *p = ‘\0’;

   printf(“%s\n”, str1);

 

   return 0;

}

18、算分數的問題,去掉一個最高分一個最低分,求平均分 

1.      #include<stdio.h>   

2.      float avescore(float score[],int n)  

3.      {  

4.          float min=0;  

5.          float max=0;  

6.          int minindex=0;  

7.          int maxindex=0;  

8.          float sum=0;  

9.          min=score[0];  

10.       for(int i=0;i<n;i++)  

11.           if(score[i]<min)  

12.           {  

13.               min=score[i];  

14.               minindex=i;  

15.           }  

16.           score[minindex]=0;  

17.           max=score[0];  

18.           for(i=0;i<n;i++)  

19.               if(score[i]>max)  

20.               {  

21.                   max=score[i];  

22.                   maxindex=i;  

23.               }  

24.               score[maxindex]=0;  

25.               for(i=0;i<n;i++)  

26.                   sum+=score[i];  

27.               sum=sum/(n-2);  

28.               return sum;           

29.   }  

30.   void main()  

31.   {  

32.       float score[6]={70,80,90,98,87,86};  

33.       float lastscore;  

34.       lastscore=avescore(score,6);  

35.       printf("the last score is :%5.2f\n",lastscore);  

36.         

37.   }  

運行結果:

the last score is :85.75

19、對一個數組,將數組中偶數從大到小排序,奇數從小到大排序,奇數和偶數交叉着放且輸出數組第一位放奇數  若奇數和偶數不等長,則把剩下的直接放到數組中。

思路:先進行奇偶判斷,得到奇數和偶數數組。然後對兩數組排序,進行長度判斷,最後組織數據。

#include<stdio.h>   

1.      #include<malloc.h>   

2.        

3.     void jiou(int a[],int n)  

4.      {  

5.          int *p1;  

6.          int *p2;  

7.          int i,j;  

8.          int k=0;  

9.          int kk=0;  

10.       int count1=0;  

11.       int count2=0;  

12.       int temp;  

13.       int temp2;  

14.       int m=0;  

15.       p1=(int*)malloc(sizeof(int)*n);  

16.       p2=(int*)malloc(sizeof(int)*n);  

17.       for(i=0;i<n;i++)  

18.       {  

19.           p1[i]=0;  

20.           p2[i]=0;  

21.       }  

22.         

23.       for(i=0;i<n;i++)  

24.       {  

25.             

26.           if((a[i]%2)!=0)  

27.           {p2[kk++]=a[i];}  

28.           else  

29.           {p1[k++]=a[i];}  

30.       }  

31.       count1=k;  

32.       count2=kk;  

33.         

34.       for(i=0;i<count2;i++)  

35.           printf("%3d",p2[i]);  

36.       printf("\n");  

37.         

38.       for(i=0;i<count2;i++)  

39.           for(j=0;j<count2-1-i;j++)  

40.               if(p2[j]>p2[j+1])  

41.               {temp2=p2[j];p2[j]=p2[j+1];p2[j+1]=temp2;}  

42.               for(i=0;i<count2;i++)  

43.                   printf("%3d",p2[i]);  

44.               printf("\n");  

45.                 

46.               for(i=0;i<count1;i++)  

47.                   printf("%3d",p1[i]);  

48.               printf("\n");  

49.                 

50.                 

51.               for(i=0;i<count1;i++)  

52.                   for(j=0;j<count1-i;j++)  

53.                       if(p1[j]<p1[j+1])  

54.                       {temp=p1[j];p1[j]=p1[j+1];p1[j+1]=temp;}  

55.                       for(i=0;i<count1;i++)  

56.                           printf("%3d",p1[i]);  

57.                       printf("\n");  

58.                         

59.                         

60.                         

61.                       if(count1>count2)  

62.                             

63.                       {  

64.                           for(i=0;i<count2;i++)  

65.                           {  

66.                               a[i+m]=p2[i];  

67.                               a[i+1+m]=p1[i];  

68.                               m=m+1;  

69.                           }  

70.                           for(i=0;i<count1-count2;i++)  

71.                               a[2*count2+i]=p1[i+count2];  

72.                             

73.                       }  

74.                       else  

75.                       {  

76.                           for(i=0;i<count1;i++)  

77.                           {  

78.                               a[i+m]=p2[i];  

79.                               a[i+1+m]=p1[i];  

80.                               m=m+1;  

81.                                 

82.                           }  

83.                           for(i=0;i<count2-count1;i++)  

84.                               a[2*count1+i]=p2[i+count1];  

85.                             

86.                       }  

87.                       for(i=0;i<n;i++)  

88.                           printf("%3d",a[i]);  

89.                       printf("%\n");                

90.                         

91.   }  

92.   void main()  

93.   {  

94.       int a[10]={2,3,14,6,2,15,12,14,4,11};  

95.       jiou(a,10);  

96.         

97.   }  

運行結果:
 3 15 11
 3 11 15
 2 14  6  2 12 14  4
 14 14 12  6  4  2  2
 3 14 11 14 15 12  6  4  2  2

 

20、簡單四則運算

• 問題描述: 輸入一個只包含個位數字的簡單四則運算表達式字符串,計算該表達式的值
注: 1、表達式只含 +, -, *, / 四則運算符,不含括號
2、表達式數值只包含個位整數(0-9),且不會出現0作爲除數的情況
3、要考慮加減乘除按通常四則運算規定的計算優先級
4、除法用整數除法,即僅保留除法運算結果的整數部分。比如8/3=2。輸入表達式保證無0作爲除數情況發生
5、輸入字符串一定是符合題意合法的表達式,其中只包括數字字符和四則運算符字符,除此之外不含其它任何字符,不會出現計算溢出情況
• 要求實現函數: 
int calculate(int len,char *expStr)
【輸入】 int len: 字符串長度;
char *expStr: 表達式字符串;
【輸出】 無
【返回】 計算結果

• 示例 
1) 輸入:char *expStr = “1+4*5-8/3”
函數返回:19
2) 輸入:char *expStr = “8/3*3”
函數返回:6

#include <stdio.h>

 

/*

 *author by wanww

 *time: 2011-09-07

 */

using namespace std;

int array_compare(int len1, intarray1[], int len2, int array2[])

{

   if(len1 == len2) {

         intcount = 0;

         for(int i=0;i<len1;i++)

         {

              if(array1[i]!=array2[i])count++;

         }

         returncount;

     }else if(len1<len2) {

         returnarray_compare(len1, array1,len1,array2+len2-len1);

     }else {

         returnarray_compare(len2,array1+len1-len2,len2,array2);

     }

}

 

void array_iterate(int len, intinput_array[], int m,int output_array[])

{

     int* flag = new int[len];

     memset(flag,0,len*4);

     inthasout=0;  //已經出列的數字個數

     intstart = 0; //開始的下標號

     intj=0;      //當前以報到的數字

     while(true)

     {

         if(flag[start]== 0)   //當前元素還沒出列

         {   

            j++;      

              if(j==m) //已經計數到m,當前start下標的元素出列

              {

                  output_array[hasout] = input_array[start];

                   flag[start]= 1;   //標記當前元素已經出列

                   hasout++;

                   if(hasout== len) break; //所有的元素都已經出列,結束程序

                   //初始化下一輪的數字

                   j= 0;

                   m= input_array[start];

              }

         }  

         start++;

         if(start==len)start = 0;

        

     }

     delete[] flag;

    

}

int calculate(int len,char *expStr)

{

     struct  {

         charopdata[200];

         inttop;

     }opstack;

     //定義操作符棧

     opstack.top= -1;

     inti=0;//遍歷字符串的下標

     intt=0;//當前後綴表達式的長度

     charch = expStr[i];

     while(ch!='\0')

     {

         switch(ch)

         {

              case'+':

              case'-':

                   while(opstack.top != -1)

                   {

                       expStr[t]= opstack.opdata[opstack.top];

                       opstack.top--;

                       t++;

                   }

                   opstack.top++;

                   opstack.opdata[opstack.top]= ch;

                   break;

              case'*':

              case'/':

                   while(opstack.top != -1 && (opstack.opdata[opstack.top] =='*' ||opstack.opdata[opstack.top] =='/') )

                   {

                       expStr[t]= opstack.opdata[opstack.top];

                       opstack.top--;

                       t++;

                   }

                   opstack.top++;

                   opstack.opdata[opstack.top]= ch;

                   break;

              default:

                   expStr[t] = ch;

                   t++;

                   break;

         }

         i++;

         ch= expStr[i];

     }

     while(opstack.top != -1)//將棧中所有的剩餘的運算符出棧

     {

         expStr[t] = opstack.opdata[opstack.top];

         opstack.top--;

         t++;

     }

     expStr[t]='\0';

     struct   {

         intnumeric[200];

         inttop;

     }data;

     data.top = -1;

     i=0;

     ch = expStr[i];

    while (ch!='\0')

   {

         if(ch>='0' && ch <= '9' )

         {

              data.top++;

              data.numeric[data.top]= ch-'0';

         }

         elseif('+' == ch)

         {

              inttmp = data.numeric[data.top-1]  +data.numeric[data.top];

              data.top--;

              data.numeric[data.top]= tmp;

         }

         elseif('-' == ch)

         {

              inttmp = data.numeric[data.top-1]  -data.numeric[data.top];

              data.top--;

              data.numeric[data.top] = tmp;

         }

         elseif('*' == ch)

         {

              inttmp = data.numeric[data.top-1]  *data.numeric[data.top];

              data.top--;

              data.numeric[data.top]= tmp;

         }

         elseif('/' == ch)

         {

             if(data.numeric[data.top] == 0)

              {

                   printf("cannotbe zero of the divide\n");

                   exit(1);

              }

              inttmp = data.numeric[data.top-1] / data.numeric[data.top];

              data.top--;

              data.numeric[data.top]= tmp;

         }

         i++;

         ch= expStr[i];

   }

     returndata.numeric[data.top];

}

void main()

{

     intarray1[] = {1,3,5};

     intlen1 = 3;

     intarray2[] = {77,21,1,3,5,7};

     intlen2 = 6;

     intcount =   array_compare(sizeof(array1)/sizeof(int),array1,sizeof(array2)/sizeof(int),array2);

     printf("%d\n",count);

        printf("*****************************************************\n");

     intinput_array[] = {3,1,2,4};

     intlen = 4;

     intm=7;

    

     int* output_array = new int[sizeof(input_array)/sizeof(int)];

    

     array_iterate(4,input_array,7,output_array);

         

     for(int i=0;i<sizeof(input_array)/sizeof(int);i++)

     {

         printf("%d",output_array[i]);

     }

        

     delete[] output_array;

     printf("\n*****************************************************\n");

     charexpStr[] = "8/3*3";

   int result = calculate(strlen(expStr),expStr);

     printf("%s\n",expStr);

     printf("%d\n",result);

}

21、選秀節目打分,分爲專家評委和大衆評委,score[] 數組裏面存儲每個評委打的分數,judge_type[] 裏存儲與 score[] 數組對應的評委類別,judge_type[i] == 1,表示專家評委,judge_type[i] == 2,表示大衆評委,n表示評委總數。打分規則如下:專家評委和大衆評委的分數先分別取一個平均分(平均分取整),然後,總分 = 專家評委平均分  * 0.6 + 大衆評委 * 0.4,總分取整。如果沒有大衆評委,則 總分 = 專家評委平均分,總分取整。函數最終返回選手得分。

 函數接口   int cal_score(intscore[], int judge_type[], int n) 

#include<stdio.h>

#include<string.h>

#include<iostream.h>

#include<conio.h>

#define N 5

 

int cal_score(intscore[], int judge_type[], int n)

{

     int expert=0;

    int dazhong=0;

     int zongfen=0;

     int i;

     int number=0;

    

     for(i=0;i<N;i++)

     {

         if(judge_type[i]==1)

         {

              expert=expert+score[i];

              number++;

         }

         else dazhong=dazhong+score[i];

     }

     if(number==N)

     {

         zongfen=(int)(expert/N);

     }

     else

    {

         expert=(int)(expert/number);

         dazhong=(int)(dazhong/(N-number));

         zongfen=int(0.6*expert+0.4*dazhong);

     }

     return zongfen;

}

int main()

{

     int score[N];

     int judge_type[N];

     int numberlast=0;

     int i;

     printf("please input the %d score:\n",N);

     for(i=0;i<N;i++)

         scanf("%d",&score[i]);

     printf("please input thelevel(1:expert,2:dazhong)\n");

     for(i=0;i<N;i++)

         scanf("%d",&judge_type[i]);

     numberlast=cal_score(score,judge_type,N);

     printf("the last score is %d\n",numberlast);

     return 0;

}

22、給定一個數組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}

#include<stdio.h>

#include<string.h>

#include<conio.h>

void sort(int input[], int n, intoutput[])

{

     inti,j;

     intk=1;

     inttemp;

     intmed;

     for(i=0;i<n;i++)

         for(j=0;j<n-i;j++)

              if(input[j]>input[j+1])

              {temp=input[j];input[j]=input[j+1];input[j+1]=temp;}

              if(n%2!=0)

              {

                   for(i=0;i<n;i++)

                       printf("%2d",input[i]);

                   printf("\n");

                   med=(n-1)/2;

                   output[med]=input[n-1];

                   for(i=1;i<=med;i++)

                   {

                       output[med-i]=input[n-1-k];

                       output[med+i]=input[n-2-k];

                       k=k+2;

                      

                   }

              }

              else

              {

                  

                   for(i=0;i<n;i++)

                       printf("%2d",input[i]);

                   printf("\n");

                   med=n/2;

                   output[med]=input[n-1];

                   for(i=1;i<=med-1;i++)

                   {

                       output[med-i]=input[n-1-k];

                       output[med+i]=input[n-2-k];

                       k=k+2;  

                   }

                   output[0]=input[0];                 

              }   

              for(i=0;i<n;i++)

                       printf("%2d",output[i]);

                   printf("\n");

}

int main()

{

     inta[6]={3,6,1,9,7,8};

     intb[6]={0};

     for(inti=0;i<6;i++)

         printf("%2d",a[i]);

     printf("\n");

     sort(a,6,b);

     return0;

}

 

23、操作系統任務調度問題。操作系統任務分爲系統任務和用戶任務兩種。其中,系統任務的優先級 < 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, intsystem_task[], int user_task[])

#include<stdio.h>

#include<string.h>

#include<malloc.h>

#include<iostream.h>

 

void scheduler1(int task[], int n, intsystem_task[], int user_task[])

{

     inti;

     intj=0;

     int*p,*pp,*p_user,*pp_user;

     intindex=0;

     intcount,count2;

     intmin=0;

     intk=0;

     p=(int*)malloc(sizeof(int)*n);

     for(i=0;i<n;i++)

         p[i]=0;

     pp=(int*)malloc(sizeof(int)*n);

     for(i=0;i<n;i++)

         pp[i]=0;

     p_user=(int*)malloc(sizeof(int)*n);

     for(i=0;i<n;i++)

         p_user[i]=0;

     pp_user=(int*)malloc(sizeof(int)*n);

     for(i=0;i<n;i++)

         pp_user[i]=0;

    

     for(i=0;i<n;i++)

     {

         if(task[i]<50)

         {

              {

                   system_task[j]=task[i];

                   pp[j]=i;

                   j++;

              }

              count=j;

         }

        

         elseif(task[i]<=255)

         {

        

              {

                   user_task[k]=task[i];

                   pp_user[k]=i;

                   k++;

              }

              count2=k;

         }

         elsetask[i]=task[i];

    

     }

    

     for(i=0;i<count;i++)

         printf("%3d",system_task[i]);

     printf("\n");

    

    

     for(i=0;i<count;i++)

     {

         min=system_task[0];

         for(j=1;j<count;j++)

         {

             

              if(system_task[j]<min)

              {

                   min=system_task[j];   

                   p[i]=j;      

              }

             

         }

         system_task[p[i]]=51;

     }

    

   pp[count]=-1;

     for(i=0;i<count;i++)

         printf("%3d",pp[p[i]]);

     printf("%3d\n",pp[count]);

    

    

     /***********************************************************/

    

     for(i=0;i<count2;i++)

         printf("%4d",user_task[i]);

     printf("\n");

    

     for(i=0;i<count2;i++)

     {

         min=user_task[0];

         for(j=1;j<count2;j++)

         {

             

              if(user_task[j]<min)

              {

                   min=user_task[j]; 

                   p_user[i]=j;      

              }

             

         }

         user_task[p_user[i]]=256;

     }

    

   pp_user[count2]=-1;

     for(i=0;i<count2;i++)

         printf("%4d",pp_user[p_user[i]]);   

     printf("%3d\n",pp_user[count2]);

    

}

 

int main()

{

     inttask[9]={0, 30, 155, 1, 80, 300,170, 40, 99};

     intsystem_task[9]={0};

     intuser_task[9]={0};

     scheduler1(task,9,system_task,user_task);

     return0;

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章