首页 > 代码库 > c语言代码编程题汇总:升序,奇数在前,偶数在后

c语言代码编程题汇总:升序,奇数在前,偶数在后

升序,奇数在前,偶数在后

自己的代码:

 

 1 /*
 2     2017年3月14日12:52:39
 3     功能:升序,奇数在前,偶数在后
 4 */
 5 #include "stdio.h"
 6 int main ()
 7 {
 8     int j = 0, k = 0;
 9     int a[10];
10     int b[10];
11     int c[10];
12     int *pa = a;
13 
14     printf("please input 10 number: \n");
15     for (int i = 0; i < 10; i++)
16     {
17         printf("please input %dth number: ",i+1);
18         scanf("%d",&a[i]);
19         if(a[i] % 2 ==0)
20             b[j++] = a[i];
21         else 
22             c[k++] = a[i];
23 
24     }
25 
26     int *pb = b;
27     int *pc = c;
28 
29     for(int p = 0; p < j; p++)
30         for(int p1 = p; p1 < j; p1++)                                    //此处不能定义int p1 = 1;
31             if(b[p] >= b[p1])
32                 b[p1] = b[p];
33 
34     for(int q = 0; q < k; q++)
35         for(int q1 = q; q1 < k; q1++)
36             if(c[q] >= c[q1])
37                 c[q1] = c[q];
38 
39     while(j)                                                            //此处不能用while(*pc)语句跳出循环
40     {
41         *pa = *pc;
42         pa++;
43         pc++;
44         j--;
45     }
46     while(k)
47     {
48         *pa = *pb;
49         pa++;
50         pb++;
51         k--;
52     }
53     for (i = 0; i < 10; i++)
54         printf("%d   ",a[i]);
55 
56     return 0;
57 }
58 /*
59     总结:
60     在VC++6.0中显示的结果::
61     ——————————————————————————
62     please input 10 number:
63     please input 1th number: 1
64     please input 2th number: 2
65     please input 3th number: 3
66     please input 4th number: 4
67     please input 5th number: 5
68     please input 6th number: 6
69     please input 7th number: 7
70     please input 8th number: 8
71     please input 9th number: 9
72     please input 10th number: 10
73     1   3   5   7   9   2   4   6   8   10
74 
75     ——————————————————————————
76 
77 */

 

c语言代码编程题汇总:升序,奇数在前,偶数在后