• 几个算法编程题


    1. 输入30个数到一个56列数组,经排序后该数组各元素值按行从小到大排列,并显示该二维数组。要求:不能把次二维数组转存到一位数组中。

     1 //输入二维数组时,以行为单位,每行各个元素之间以空格隔开
     2 #include <stdio.h>
     3 
     4 #define row 5
     5 #define col 6
     6 
     7 int main() {
     8   int array[row][col], i=0, j=0, a, b, swap;
     9   printf("please enter your %d*%d array: \n", row, col);
    10   while(i < row){
    11     //no newline after %d sequence
    12     scanf("%d %d %d %d %d %d", 
    13       &array[i][0], &array[i][1], &array[i][2], &array[i][3], &array[i][4], &array[i][5]);
    14     i++;
    15   }
    16 
    17   printf("\nhere is your input: \n");
    18   for(i = 0; i < row; i++){
    19     for(j = 0; j < col; j++){
    20       printf("%d ", array[i][j]);
    21     }
    22     printf("\n");
    23   }
    24 
    25   for(i=0;i<row;i++){
    26     for(j=0;j<col;j++) {
    27       for(a=i;a<row;a++) {
    28     if(a == i)
    29       b = j + 1;
    30     else 
    31       b = 0;
    32     for(;b<col;b++) {
    33       if(array[i][j] > array[a][b]) {
    34         swap = array[i][j];
    35         array[i][j]=array[a][b];
    36         array[a][b]=swap;
    37       }
    38     }
    39       }
    40     }
    41   }  
    42 
    43   printf("\nafter sorting:\n");
    44   for(i = 0; i < row; i++){
    45     for(j = 0; j < col; j++){
    46       printf("%d ", array[i][j]);
    47     }
    48     printf("\n");
    49   }
    50   return 0;
    51 }

    2. 有n个人围成一圈,顺序编号。从第一个人开始报数(从13报数),凡报到3的人退出圈子,问最后留下的是原来的第几号的那位。

     1 #include <stdio.h>
     2 
     3 #define nmax 50
     4 
     5 
     6 int main()
     7 {
     8   int i,k,m,n,num[nmax],*p;
     9   printf("please input the total of numbers: ");
    10   scanf("%d",&n);
    11   while(n > nmax){
    12     printf("your input is too big, please choose a figure less than 50: ");
    13     scanf("%d",&n);
    14   }
    15   p=num;
    16   for(i=0;i<n;i++)
    17     *(p+i)=i+1;
    18   i=0;
    19   k=0;
    20   m=0;
    21   while(m < n-1){
    22     if(*(p+i)!=0) 
    23       k++;
    24     if(k==3){
    25       *(p+i)=0;
    26       k=0;
    27       m++;
    28     }
    29     i++;
    30     if(i==n)  //从头开始循环
    31       i=0;
    32   }
    33   while(*p==0) p++;
    34   printf("%d is left\n",*p);
    35 }

    3. A·B·C·D·E·F这六个变量排成如图所示的三角形,这六个变量分别取【16】上的整数,且均不相同。求使三角形三条边上的变量之和相等的全部解。如图就是一个解。

         2

      3            5

    6      1       4

    //以每行输出顺时针(或逆时针)构造三角形,其三个边之和相等
    #include <stdio.h>
    #define SIDE_N   3
    #define LENGTH   3
    #define VARIABLES   6
    
    int A,B,C,D,E,F;
    int *pt[]={&A,&B,&C,&D,&E,&F};
    int *side[SIDE_N][LENGTH]={&A,&B,&C,&C,&D,&E,&E,&F,&A};
    int side_total[SIDE_N];
    
    int main(){  
      int i,j,t,equal;
      for(j = 0; j < VARIABLES; j++)
        *pt[j]=j+1;
    
      while(1){
        for (i = 0;i < SIDE_N; i++){
          for (t = j = 0; j < LENGTH; j++)
        t += *side[i][j];
          side_total[i]=t;
        }
        for (equal = 1, i = 0; equal && i < SIDE_N-1; i++)
          if (side_total[i] != side_total[i+1])
        equal=0;
    
        if (equal){ 
          for (i = 0; i < VARIABLES; i++)
        printf("%2d",*pt[i]);
          printf("\n");
        }
        for(j = VARIABLES-1; j > 0; j--)
          if (*pt[j] > *pt[j-1])   
        break;
        if(j == 0)   
          break;
        for(i = VARIABLES-1; i >= j; i--)
          if (*pt[i] > *pt[j-1])   
        break;
        t=*pt[j-1];
        *pt[j-1] = *pt[i];
        *pt[i] = t;
        for(i = VARIABLES-1; i > j; i--,j++){
          t = *pt[j]; 
          *pt[j] = *pt[i];
          *pt[i] = t;
        }
      }
    }
    主题思想:将一个排列看作一个长整数,则所有排列对应着一组整数。将这组整数按从小到大的顺序排列排成一个整数,从对应最小的整数开始。按数列的递增顺序逐一列举每个排列对应的每个整数,这能更有效地完成排列的穷举。从一个排列找出对应数列的下一个排列可在当前排列的基础上作部分调整来实现。倘若当前排列为1,2,4,6,5,3,并令其对应的长整数为124653。要寻找比长整数124653更大的排列,可从该排列的最后一个数字顺序向前逐位考察,当发现排列中的某个数字比它前一个数字大时,如本例中的6比它的前一位数字4大,这说明还有对应更大整数的排列。但为了顺序从小到大列举出所有的排列,不能立即调整得太大,如本例中将数字6与数字4交换得到的排列126453就不是排列124653的下一个排列。为了得到排列124653的下一个排列,应从已经考察过的那部分数字中选出比数字大,但又是它们中最小的那一个数字,比如数字5,与数字4交换。该数字也是从后向前考察过程中第一个比4大的数字。5与4交换后,得到排列125643。在前面数字1,2,5固定的情况下,还应选择对应最小整数的那个排列,为此还需将后面那部分数字的排列顺序颠倒,如将数字6,4,3的排列顺序颠倒,得到排列1,2,5,3,4,6,这才是排列1,2,4,6,5,3的下一个排列。

    4. 编程实现若干个多项式相乘。多项式的输入输出格式为:系数在前,指数在后,各项按指数递增排列,每个多项式输入时以两个0结束。系数为0的项不输出。

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 #include<malloc.h>
      4 
      5 typedef struct Polynode
      6 {
      7   int coef;//系数
      8   int exp;//指数
      9   struct Polynode *next;
     10 }Polynode,*Polylist;
     11 
     12 //多项式链表升序排序
     13 Polylist Polysort(Polylist head)
     14 {
     15   Polynode *first,*move,*p,*q; //first移动指针变量 move 被移动项指针变量p,q临时指针变量
     16   q=head;                          
     17   p=head->next;
     18   if(p==NULL) return head;      //判断链表是否为空;
     19   first=p->next;
     20   p->next=NULL;
     21   move=first;
     22   while(move!=NULL)                 //直接插入排序(链表排序)
     23     {
     24       first=first->next;  
     25       if(p->exp==move->exp)     //判断待插入项指数是否与首项相等;
     26     {
     27       p->coef+=move->coef;  //系数相加;
     28       free(move);              //释放空间;
     29       if(p->coef==0)        //若系数相加和为0;
     30         {    
     31           q->next=p->next;    
     32           free(p);          //释放空间;
     33         }
     34     }
     35       else if(p->exp>move->exp) //判断待插入项指数是否大于第一个项的指数;
     36     {
     37       head->next=move;
     38       move->next=p;
     39     }
     40       else if(p->next==NULL)   //判断下一项是否为空;
     41     {
     42       p->next=move;
     43       move->next=NULL;
     44     }
     45       else                          //待插入项指数插入位置在首末项之间;
     46     {     
     47       q=p;                    //移动临时变量指针p,q
     48       p=p->next;
     49       while(1)
     50         {
     51           if(p->exp==move->exp)  //判断待插入项指数是否与首项相等;
     52         {
     53           p->coef+=move->coef;//系数相加;
     54           free(move);            //释放空间;
     55           if(p->coef==0)        
     56             {    
     57               q->next=p->next;//若系数相加和为0;
     58               free(p);        //释放空间;
     59             }
     60           break;
     61         }
     62           if(p->exp>move->exp)  //判断待插入项指数是否大于当前项的指数;
     63         {
     64           q->next=move;
     65           move->next=p;
     66           break;
     67         }
     68           if(p->next==NULL)     //判断下一项是否为空;
     69         {
     70           p->next=move;
     71           move->next=NULL;
     72           break;
     73         }
     74           q=p;                 //移动临时变量指针p,q;
     75           p=p->next;
     76         }
     77     }
     78       p=head->next;                //使p,q指针重新指到初始化位置;
     79       q=head;
     80       move=first;
     81     }
     82   return head;                     //返回头结点;
     83 }
     84 
     85 
     86 Polylist creat()
     87 {
     88   Polynode *head,*p,*newnode;
     89 
     90   int c,e;                  
     91   head=(Polynode *)malloc(sizeof(Polynode));
     92   p=head;
     93   printf("please input the ceofficients and expoents:\n");
     94   scanf("%d %d",&c,&e);
     95   while(c||e)                           
     96     {
     97       if(c==0) 
     98     {scanf("%d %d",&c,&e);continue;}
     99       newnode=(Polynode *)malloc(sizeof(Polynode));
    100       newnode->coef=c;
    101       newnode->exp=e;
    102       p->next=newnode;
    103       p=newnode;
    104       scanf("%d %d",&c,&e);
    105     }
    106   p->next=NULL;        
    107   head=Polysort(head); 
    108   return head;
    109 }
    110 
    111 Polylist Polymul(Polylist LA,Polylist LB)
    112 {
    113   Polynode *head,*p,*q,*t,*newnode; //head:头指针 newnode:新结点指针 p,q,t:临时指针变量;
    114   p=LA->next;
    115   q=LB->next;
    116   head=(Polynode *)malloc(sizeof(Polynode));//开辟一个新结点,并使之成为新链表的头结点;
    117   t=head;
    118   while(p!=NULL)
    119     {
    120       while(q!=NULL)
    121     {
    122       newnode=(Polynode *)malloc(sizeof(Polynode));//开辟一个新结点;
    123       t->next=newnode;
    124       t=t->next;
    125       t->coef=p->coef*q->coef;        //项之系数为LA,LB两项系数之积;
    126       t->exp=p->exp+q->exp;           //项之指数为LA,LB两项指数之和;
    127       q=q->next;
    128     }
    129       p=p->next;                          //p指针移动;
    130       q=LB->next;                         //q指针复位为LB->next;
    131     }
    132   t->next=NULL;                           //为最后的结点的next赋空;
    133   head=Polysort(head);                    //调用Polysort排序函数对多项式链表进行降序排序;
    134   return head;                            //返回头结点;
    135 }
    136 //输出函数
    137 void print(Polylist head)
    138 {
    139   Polynode *p;
    140 
    141   p=head->next;
    142   if(p==NULL) 
    143     printf("0");
    144   else 
    145     while(p!=NULL)
    146       {   
    147     //系数输出
    148     if(p->coef==-1)  
    149       printf("-");
    150     else if(p->coef!=1)  
    151       printf("%d",p->coef);
    152 
    153     //符号输出
    154     if(p->exp!=0&&p->exp!=1)  
    155       printf("X^");
    156     else if(p->exp==1)  
    157       printf("X");
    158 
    159     //指数输出
    160     if(p->exp==0&&(p->coef==-1||p->coef==1))   
    161       printf("1");
    162     if(p->exp<0)   
    163       printf("(%d)",p->exp);
    164     else if(p->exp!=1&&p->exp!=0)    
    165       printf("%d",p->exp);
    166     p=p->next;
    167     if(p!=NULL&&p->coef>0) 
    168       printf("+");
    169       }
    170   printf("\n");
    171 }
    172 
    173 void main()
    174 {
    175   Polylist LA,LB,LC;
    176   printf("For polynode A, ");
    177   LA=creat();                             //创建多项式LA;
    178   print(LA);                              //输出多项式LA;
    179 
    180   printf("\nFor polynode B: ");
    181   LB=creat();                             //创建多项式LB;
    182   print(LB);                              //输出多项式LB;
    183 
    184   LC=Polymul(LA,LB);                      //对多项式LA,LB相乘;
    185 
    186   printf("\nThe multiply of the polynodes is: \n");
    187   print(LC);                              //输出多项式LC;
    188 }

    5. 两个乒乓球队进行比赛,各出3人。甲队为A,BC  3人,乙队为X,Y,Z 3人。已抽签决定比赛名单。有人向队员打听比赛的名单。A说他不和X比,C说他不和X,Z比,请编程序找出3对赛手的名单。

     1 #include <stdio.h>
     2 
     3 int main()
     4 {
     5   char i,j,k; /*i是a的对手,j是b的对手,k是c的对手*/
     6   for(i='x';i<='z';i++)
     7     for(j='x';j<='z';j++)
     8       {
     9     if(i!=j)
    10       for(k='x';k<='z';k++)
    11         {
    12           if(i!=k&&j!=k)
    13         {
    14           if(i!='x'&&k!='x'&&k!='z'){
    15             printf("order is a--%c\tb--%c\tc--%c\n",i,j,k);
    16             return 0;
    17           }
    18         }
    19         }
    20       }
    21 
    22 } 

    6. 一个小孩买了价值少于1美元的糖,并将1美元的钱交给售货员。售货员希望用数目最少的硬币找给小孩。假设提供了数目不限的面值为25美分·10美分·5美分·及1美分的硬币。

     1 #include <stdio.h>
     2 #define max 100
     3 
     4 int main(){
     5   
     6   int input, diff, reminder, num25, num10, num5, num1;
     7 
     8   printf("the total cost of your good(in cents): ");
     9   scanf("%d", &input);
    10   while(input > max){
    11     printf("please choose another one under 100 cents: ");
    12     scanf("%d", &input);
    13   }
    14 
    15   diff = max - input;
    16   if(diff == 0){
    17     printf("\njust suitable, no change for you...\n");
    18     return 0;
    19   }
    20 
    21   num25 = diff / 25;
    22   reminder = diff % 25;
    23   num10 = reminder / 10;
    24   reminder = reminder % 10;
    25   num5 = reminder / 5;
    26   num1 = reminder % 5;
    27 
    28   printf("\nyour change is: \n");
    29 
    30   if(num25 != 0)
    31     printf("number of 25s: %d\n", num25);
    32   if(num10 != 0)
    33     printf("number of 10s: %d\n", num10);
    34   if(num5 != 0)
    35     printf("number of  5s: %d\n", num5);
    36   if(num1 != 0)
    37     printf("number of  1s: %d\n", num1);
    38 
    39   return 0;
    40 }
  • 相关阅读:
    Android 自定义seekbar中,thumb被覆盖掉一部分问题
    Android SeekBar实现音量调节
    Android 设置thumb图片大小
    Android 图片处理方法
    Android 三档自定义滑动开关,禁止点击功能的实现,用默认的seekbar组件实现
    Android SeekBar自定义使用图片和颜色显示
    Android 使控件各占屏幕的一半
    Android ExpandableListView使用+获取SIM卡状态信息
    Android的5样的调试信息
    Js创建对象的做法
  • 原文地址:https://www.cnblogs.com/beanmoon/p/2776302.html
Copyright © 2020-2023  润新知