C編程(西安郵電大學MOOC)


1、最大與次大值

題目內容:編寫程序,找出給定的n個數中的最大值及次大值。

輸入格式:在第一行中輸入一個正整數n(1<n≤10)。第二行輸入n個整數,用英文逗號,隔開。

輸出格式:在一行中輸出最大值及次大值,中間用英文逗號,隔開。

輸入樣例:7

12,1,2,5,4,7,3

輸出樣例:12,7

 1 /* 
 2     求最大值、次大值可以不用排序
 3  */
 4 #include <stdio.h>
 5 #define N 100
 6 int main()
 7 {    
 8     int n, a[N] = {0};
 9     scanf("%d",&n);
10     for(int i=0; i<n-1; ++i){
11         scanf("%d,",&a[i]);
12     }
13     scanf("%d",&a[n-1]);
14     /* 只一個元素最大次大相同 */
15     int max1 = a[0], max2 = a[0];
16     a[1] > max1 ? max1 = a[1] : max2  = a[1];
17     for(int k=2; k<n; ++k){
18         if(a[k]>max1){
19             max2 = max1;
20             max1 = a[k];
21         }
22         else if(a[k]>max2){
23             max2 = a[k];
24         }
25     }    
26     printf("%d,%d\n",max1, max2);
27     return 0;
28 }
 1 /*  
 2 與只求最大值的程序一樣嗎,
 3 好像是, 可以排序。
 4 */
 5 #include <stdio.h>
 6 #define N 100
 7 int main()
 8 {    
 9     int n, a[N] = {0};
10     scanf("%d",&n);
11     for(int i=0; i<n-1; ++i){
12         scanf("%d,",&a[i]);
13     }
14     scanf("%d",&a[n-1]);
15     /* 插入排序 */
16     for(int i = 0; i<n; ++i){
17         int min = a[i];
18         int index = i;
19         for(int j = i+1; j<n; ++j){
20             if(min>a[j]){ 
21                 min = a[j];
22                 index = j;
23             }                
24         }
25         if(index != i){
26             a[index] = a[i];
27             a[i] = min;
28         }
29     }    
30     printf("%d,%d\n",a[n-1],a[n-2]);
31     return 0;
32 }

2、素數排序

題目內容:輸入10個正整數到a數組中,對a[10]數組中的素數升序排序。

輸入格式:在一行中輸入10個用英文逗號,隔開的正整數。

輸出格式:在一行中輸出升序的素數序列,每個數之間用英文逗號,隔開,末尾沒有多余的空格

輸入樣例:10,3,1,5,4,8,7,2,9,11

輸出樣例:2,3,5,7,11

輸入樣例:16,12,1,6,4,8,14,18,24,21

輸出樣例:Not found!

 1 /*  */
 2 #include <stdio.h>
 3 #define N 100
 4 /* 判斷素數 */
 5 int isPrime(int key)
 6 {
 7     if(key<2)
 8         return 0;
 9     for(int i=2; i<key; ++i)
10         if(key%i==0)
11             return 0;
12     return 1;
13 }
14 /* 升序插入 */
15 void InsertArr(int *arr, int n, int key)
16 {
17     int j=n;
18     while(j>=0 && key>arr[j]){
19         arr[j+1]=arr[j];/*移動,向后賦值*/
20         j--;
21     }
22     arr[j+1]=key;/**/   
23 }
24 int main()
25 {    
26     int n=0, arr[N] = {0},a[N] = {0};    
27     
28     for(int i=0; i<9; ++i){
29         scanf("%d,",&a[i]);
30         if(isPrime(a[i])){
31             InsertArr(arr,n,a[i]);
32             n++;
33         }            
34     }
35     scanf("%d",&a[9]);
36     if(isPrime(a[9])){
37         InsertArr(arr,n,a[9]);
38         n++;
39     }    
40     if(n==0)
41         printf("Not found!");
42     else{
43         for(int i=n-1; i>0; --i){
44             printf("%d,",arr[i]);
45         }
46         printf("%d",arr[0]);
47     }
48     return 0;
49 }

 3、循環右移

題目內容:一個數組A中存有N>0個整數,將每個整數循環向右移M≥0個位置,即將A中的數據由(A0A1⋯AN−1)變換為(AN−M⋯AN−1A0A1⋯AN−M−1)(最后M個數循環移至最前面的M個位置)。

輸入格式:第1行輸入N(1≤N≤100)和M(≥0)中間用英文逗號,分隔第2行輸入N個整數,中間用英文 逗號,分隔。

輸出格式:在一行中輸出循環右移M位以后的整數序列,中間用英文逗號,分隔,序列結尾不能有多余空格。

輸入樣例:7,3

1,2,3,4,5,6,7

輸出樣例:5,6,7,1,2,3,4

輸入樣例:7,0

1,2,3,4,5,6,7

輸出樣例:1,2,3,4,5,6,7

 1 #include <stdio.h>
 2 #define N 100
 3 
 4 /* 頭插入 */
 5 void InsertHead(int *arr, int index, int key)
 6 {    
 7     for(int j=index-1; j>=0; j--){
 8         arr[j+1]=arr[j];/*移動*/
 9     }
10     arr[0]=key;
11 }
12 int main()
13 {    
14     int n, m, arr[N] = {0};    
15     scanf("%d,%d",&n,&m);
16     
17     for(int i=0; i<n-1; ++i){
18         scanf("%d,",&arr[i]);        
19     }
20     scanf("%d",&arr[n-1]);
21     
22     while(m--)
23         InsertHead(arr,n-1,arr[n-1]);
24         
25     for(int i=0; i<n-1; ++i){
26         printf("%d,",arr[i]);
27     }
28     printf("%d",arr[n-1]);    
29     return 0;
30 }
 1 /* 算法2 整體右移動m位, 右m位全部移動到數組左邊 */
 2 #include <stdio.h>
 3 #define N 100
 4 
 5 int main()
 6 {    
 7     int n, m, arr[N] = {0};    
 8     scanf("%d,%d",&n,&m);
 9     
10     for(int i=0; i<n-1; ++i){
11         scanf("%d,",&arr[i]);        
12     }
13     scanf("%d",&arr[n-1]);
14     /* 1.數組整體向右移動m位 */
15     for(int i=n-1; i>=0; --i)
16         arr[i+m] = arr[i];
17     /* 2.右m位全部移動到數組左邊 */
18     for(int i=0; i<m; ++i)
19         arr[i] = arr[i+n];
20         
21     for(int i=0; i<n-1; ++i){
22         printf("%d,",arr[i]);
23     }
24     printf("%d",arr[n-1]);    
25     return 0;
26 }
 1 /* 算法3 置換圈 */
 2 #include <stdio.h>
 3 #define N 100
 4 int gcd(int n, int m)//最大公約數
 5 {
 6     while(n)
 7     {
 8         int r = m%n;
 9         m = n;
10         n = r;
11     }
12     return m;
13 }
14 int main()
15 {    
16     int n, m, arr[N] = {0};    
17     scanf("%d,%d",&n,&m);
18     
19     for(int i=0; i<n-1; ++i){
20         scanf("%d,",&arr[i]);        
21     }
22     scanf("%d",&arr[n-1]);
23     
24     int count = gcd(n,m);//圈數(最大公約數)
25     int length = n / count; //圈長度
26     for(int i=0; i<count; ++i)//循環處理count圈
27     {
28         int t = arr[i]; //圈頭元素
29         int start = i;  //圈頭下標
30         int end = (start-m+n)%n;//圈尾下標
31         for(int j=1; j<length; j++)//圈內移位
32         {
33             arr[start] = arr[end];
34             start  = end;
35             end = (end-m+n)%n;
36         }
37         arr[start] = t;//圈頭元素就位
38     }
39     
40     for(int i=0; i<n-1; ++i){
41         printf("%d,",arr[i]);
42     }
43     printf("%d",arr[n-1]);    
44     return 0;
45 }
 1 /* 算法4 逆序 */
 2 #include <stdio.h>
 3 #define N 100
 4 /* 逆序 */
 5 void reverse(int* arr, int left, int right)
 6 {
 7     while(left<right)
 8     {
 9         int t = arr[left];
10         arr[left] = arr[right];
11         arr[right] = t;
12         left++, right--;
13     }
14 }
15 int main()
16 {
17     int n, k, arr[N] = {0};    
18     scanf("%d,%d",&n,&k);
19     /* 輸入數組 */
20     for(int i=0; i<n-1; ++i){
21         scanf("%d,",&arr[i]);        
22     }
23     scanf("%d",&arr[n-1]);
24     /* 1.將數組分成兩段,左段長為n-k,右段長為k
25     分別將兩段逆轉(即元素排列次序與原次序相反) */
26     reverse(arr,0,n-k-1);    
27     reverse(arr,n-k,n-1);
28     /* 2.再數組元素整體逆轉 */
29     reverse(arr,0,n-1);
30     /* 打印 */
31     for(int i=0; i<n-1; ++i){
32         printf("%d,",arr[i]);
33     }
34     printf("%d",arr[n-1]);  
35     return 0;
36 }
 1 /* 算法5.輔助數組 */
 2 #include <stdio.h>
 3 #define N 100
 4 int main()
 5 {
 6     int n, k, arr1[N] = {0},arr2[N] = {0};    
 7     scanf("%d,%d",&n,&k);
 8     /* 輸入數組 */
 9     for(int i=0; i<n-1; ++i){
10         scanf("%d,",&arr1[i]);        
11     }
12     scanf("%d",&arr1[n-1]);
13     /* 復制 */
14      for(int i=0; i<n; ++i){
15         arr2[i] = arr1[(i-k+n)%n];//左移arr1[(i+k)%n];
16     }
17     /* 打印 */
18     for(int i=0; i<n-1; ++i){
19         printf("%d,",arr2[i]);
20     }
21     printf("%d",arr2[n-1]);  
22     return 0;
23 }
 1 /* 算法6.循環隊列理論,直接輸出 */
 2 #include <stdio.h>
 3 #define N 100
 4 int main()
 5 {
 6     int i, n, k, arr[N] = {0};    
 7     scanf("%d,%d",&n,&k);
 8     /* 輸入數組 */
 9     for(int i=0; i<n-1; ++i){
10         scanf("%d,",&arr[i]);        
11     }
12     scanf("%d",&arr[n-1]);
13     
14     /* 打印 */
15     for(i=0; i<n-1; ++i){
16         printf("%d,",arr[(i-k+n)%n]);
17     }
18     printf("%d",arr[(i-k+n)%n]);  
19     return 0;
20 }

4、爬樓梯

題目內容:可愛的小明特別喜歡爬樓梯,他有的時候一次爬一個台階,有的時候一次爬兩個台階,有的時候一次爬三個台階。如果這個樓梯有n個台階,小明一共有多少種爬法呢?n值從鍵盤輸入。

輸入格式:輸入一個整數n,(1<=n<46)。

輸出格式:輸出當樓梯階數是n時的上樓方式總數。

輸入樣例:1

輸出樣例:1

輸入樣例:4

輸出樣例:7

輸入樣例:24

輸出樣例:1389537

 1 #include <stdio.h>
 2 
 3 double fun(int n){
 4     if(n==1)
 5         return 1;
 6     else if(n==2)
 7         return 2;
 8     else if(n==3)
 9         return 4;
10     return fun(n-1) + fun(n-2)+ fun(n-3);
11 }
12 
13 int main()
14 {
15     int n;
16     scanf("%d",&n);
17     printf("%.f",fun(n));
18     return 0;
19 }

5、三天打魚兩天曬網

題目內容:中國有句俗語:“三天打魚兩天曬網”,某人從1990年1月1日起開始“三天打魚兩天曬網”。問這個人在以后的某一天是在“打魚”還是在“曬網”?

輸入樣例:1990-1-3

輸出樣例:他在打魚

輸入樣例:2018-11-13

輸出樣例:他在曬網

 1 #include<stdio.h>
 2 int inputdate();
 3 int main()
 4 {
 5     int n = inputdate();
 6    /*  if(-1==n){
 7         printf("Invalid input");
 8     }
 9     else  */
10     if(n%5==0||n%5==4){
11         printf("他在曬網");
12     }
13     else{
14         printf("他在打魚");
15     }
16  
17     return 0;
18 }
19 int inputdate()
20 {
21     int year,month,day;
22     int n = scanf("%4d-%2d-%2d",&year,&month,&day);
23     /* if(3 != n||year<1990||month>12||month<=0||day>31||day<=0){
24         return -1;
25     } */
26  
27     int date[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
28                      {31,29,31,30,31,30,31,31,30,31,30,31}};
29  
30     int sum = 0, flag =(year%4==0&&year%100!=0)||(year%400==0);
31     for(int i=0;i<month-1;++i){
32         sum +=date[flag][i];
33     }
34  
35     return sum+day;
36 }

6、時間換算

題目內容:編寫程序,實現輸入的時間,屏幕顯示一秒后的時間。顯示格式為HH:MM:SS。

 程序需要處理以下三種特殊情況:

(1)若秒數加1后為60,則秒數恢復到0,分鍾數增加1;

(2)若分鍾數加1后為60,則分鍾數恢復到0,小時數增加1;

(3)若小時數加1后為24,則小時數恢復到0。

輸入樣例:18:1:2

輸出樣例:18:01:03

輸入樣例:20:12:59

輸出樣例:20:13:00

輸入樣例:23:59:59

輸出樣例:00:00:00

 1 #include<stdio.h>
 2 void run();
 3 int main()
 4 {
 5     run();
 6  
 7     return 0;
 8 }
 9 void run()
10 {
11     int h,m,s;
12     scanf("%d:%d:%d",&h,&m,&s);
13  
14     s++;
15     if(s==60){
16         s=0;
17         m++;
18     }
19     if(m==60){
20         m=0;
21         h++;
22     }
23     if(h==24){
24         h=0;
25     }
26     printf("%02d:%02d:%02d\n",h,m,s);
27 }

7、正向建立單鏈表

題目內容:輸入若干個正整數(輸入-1為結束標志),要求按輸入數據的逆序建立單鏈表並輸出。

輸入格式:一行內輸入若干個正整數,之間用空格隔開,並以-1結束。

輸出格式:一行內輸出建立的單鏈表數據結果,之間用兩個分隔符 -- 隔開,結束標志-1不輸出。

輸入樣例:2 4 6 8 10 -1

輸出樣例:--2--4--6--8--10

 1 #include <stdio.h>
 2 #include <malloc.h>
 3 typedef struct node NODE;
 4 typedef struct node* Linklist;
 5 struct node{
 6     int data;
 7     Linklist next;
 8 };
 9 /* 
10 //頭插法(輸入與輸出是逆序)
11 Linklist create(){
12     Linklist L,p;
13     L = p = (Linklist)malloc(sizeof(NODE));
14     L->next = NULL;
15     int x;
16     scanf("%d",&x);
17     while(x!=-1)
18     {
19         p = (Linklist)malloc(sizeof(NODE));
20         p->data = x;
21         p->next = L->next;
22         L->next = p;
23         scanf("%d",&x);
24     }
25     return L;
26 } */
27 // 尾插法(輸入與輸出正序) 
28 Linklist create(){
29     Linklist L,p,r;
30     L = r = (Linklist)malloc(sizeof(NODE));
31     L->next = NULL;
32     int x;
33     scanf("%d",&x);
34     while(x!=-1)
35     {
36         p = (Linklist)malloc(sizeof(NODE));
37         p->data = x;
38         p->next = NULL;
39         r->next = p;
40         r = p;    
41         scanf("%d",&x);
42     }
43     return L;
44 }
45 void print(Linklist L){
46     while(L->next)
47     {
48         L = L->next;
49         printf("--%d",L->data);
50     }
51 }
52 int main()
53 {
54     Linklist L = create();
55     print(L);
56     return 0;
57 }

8、逆置單鏈表

題目內容:一個單鏈表L=(a1 , a2 , … , an-1 , an),其逆單鏈表定義為L’=( an , an-1 , … , a2 , a1),編寫算法將單鏈表L逆置,要求逆單鏈表仍占用原單鏈表的空間。

輸入格式:一行內輸入原單鏈表中的數據(若干個正整數,之間用空格隔開,並以-1結束

輸出格式:一行內輸出建立的原單鏈表數據結果,之間用兩個分隔符 -- 隔開,結束標志-1不輸出。

                  下一行內輸出逆置后的單鏈表數據結果,之間用兩個分隔符 -- 隔開。

輸入樣例:2 4 6 8 10 -1

輸出樣例:--2--4--6--8--10

                  --10--8--6--4--2

 1 #include <stdio.h>
 2 #include <malloc.h>
 3 typedef struct node NODE;
 4 typedef struct node* Linklist;
 5 struct node{
 6     int data;
 7     Linklist next;
 8 };
 9 void reverse(Linklist L)
10 {
11     Linklist NEW = NULL, CUR = L->next, TEMP;
12     while(CUR)//是否空
13     {
14         TEMP = CUR->next;//保存新表的當前指針的前驅指針(無前驅為NULL)
15         CUR->next = NEW;// 逆序(NEW為新表當前CUR的后繼指針)
16         NEW = CUR;//更新新表后繼指針
17         CUR = TEMP;//更新新表當前指針      
18     }    
19     L->next = NEW;//逆序的頭指針插入頭結點
20 }  
21 Linklist create()
22 {
23     Linklist L,p,r;
24     L = r = (Linklist)malloc(sizeof(NODE));
25     L->next = NULL;
26     int x;
27     scanf("%d",&x);
28     while(x!=-1)
29     {
30         p = (Linklist)malloc(sizeof(NODE));
31         p->data = x;
32         p->next = NULL;
33         /* ---------- */
34         r->next = p;//尾插
35         r = p;    //更新尾指針
36         scanf("%d",&x);
37     }
38     return L;
39 }
40 void print(Linklist L)
41 {
42     while(L->next)
43     {
44         L = L->next;
45         printf("--%d",L->data);
46     }
47     printf("\n");
48 }
49 int main()
50 {
51     Linklist L = create();
52     print(L);
53     reverse(L);//逆序
54     print(L);
55     return 0;
56 }

9、鏈表合並

題目內容:已知單鏈表LA=(a1,a2,…,am)和LB=(b1,b2,…,bn),編寫程序按以下規則將它們合並成一個單鏈表LC,要求新表 LC利用原表的存儲空間。

          LC=(a1b1,…,ambmbm+1,…,bn)m<=n

或者

      LC=(a1b1,…,an,bn,an+1,…,am),m>n

輸入格式:一行內輸入單鏈表LA中的數據(若干個正整數,之間用空格隔開,並以-1結束

                  一行內輸入單鏈表LB的數據(若干個正整數,之間用空格隔開,並以-1結束

輸出格式:一行內輸出合並后單鏈表LC的數據結果,之間用兩個分隔符 -- 隔開,結束標志-1不輸出。 

輸入樣例:1 3 5 7 -1

                  2 4 6 -1

輸出樣例:--1--2--3--4--5--6--7

 1 #include <stdio.h>
 2 #include <malloc.h>
 3 typedef struct node NODE;
 4 typedef struct node* Linklist;
 5 struct node{
 6     int data;
 7     Linklist next;
 8 };
 9 Linklist mergelist(Linklist LA,Linklist LB)
10 {
11     /* 
12         LA的頭指針為新鏈表的頭、尾指針
13         LB的頭結點釋放
14     */
15     Linklist head = LA,rear = LA,temp = LB; 
16     LA = LA->next;
17     LB = LB->next;
18     free(temp);
19     /* 逐個結點鏈接到新鏈表 */
20     while(LA && LB)
21     {
22         /* 先插LA再插LB */
23         if(LA)
24         {    
25             rear->next = LA;//尾插結點
26             rear = rear->next;//更新尾結點
27             LA = LA->next;    
28         }
29         if(LB)
30         {        
31             rear->next = LB;//尾插結點
32             rear = rear->next;//更新尾結點
33             LB = LB->next;
34         }
35     }
36     if(LA)
37         rear->next = LA;//尾插剩余結點
38     if(LB)
39         rear->next = LB; 
40     return head;
41 }
42 Linklist create(){
43     Linklist L,p,r;
44     L = r = (Linklist)malloc(sizeof(NODE));
45     L->next = NULL;
46     int x;
47     scanf("%d",&x);
48     while(x!=-1)
49     {
50         p = (Linklist)malloc(sizeof(NODE));
51         p->data = x;
52         p->next = NULL;
53         r->next = p;
54         r = p;    
55         scanf("%d",&x);
56     }
57     return L;
58 }
59 void print(Linklist L){
60     while(L->next)
61     {
62         L = L->next;
63         printf("--%d",L->data);
64     }
65     printf("\n");
66 }
67 int main()
68 {
69     Linklist L1 = create();
70     Linklist L2 = create();
71     Linklist L3 = mergelist(L1,L2);
72     print(L3);
73     return 0;
74 }

10、求2~2000的所有素數.有足夠的內存,要求盡量快

 1 #include <stdio.h>
 2 #include <assert.h>
 3 /* 素數集合 */
 4 int findvalue[2000];
 5 static int find;/*素數集合下標從0開始*/
 6 bool adjust(int value)
 7 {    
 8     assert(value>=2);/* value>=2 */
 9     if(value==2) return true; 
10     /* 被素數整除的不是素數 */
11     for(int i=0;i<=find;i++)
12     {
13         if(value%findvalue[i]==0)
14             return false;
15     }
16     findvalue[find++];
17     return true;
18 }
19 int main()
20 {    
21     /* 測試, 2~100中的素數集合 */
22     for(int i=2; i<100; ++i)
23         if(adjust(i))
24             findvalue[find] = i;
25     /* 打印素數集合 */
26     for(int j=0;findvalue[j]; ++j)
27         printf("%d ",findvalue[j]);
28     return 0;
29 }

11、計算器

輸入一個四則運算算式,運算符為+、-、*、/,操作數為實型數據,對於除法算式,除數不為0,輸出算式中的操作數要求保留小數點后兩位。

[測試輸入1]1.2+3.4

[測試輸出1]1.20+3.40=4.60

[測試輸入2]2.5*2

[測試輸出2]2.50*2.00=5.00

 1 #include <stdio.h>
 2 #define N 100
 3 int main()
 4 {    
 5     double a,b;    
 6     char op;
 7     scanf("%lf %c%lf",&a,&op,&b);
 8     double result=0;
 9     switch(op){
10         case '+':result = a+b;break;
11         case '-':result = a-b;break;
12         case '*':result = a*b;break;
13         case '/':result = a/b;break;
14     }
15     printf("%.2f%c%.2f=%.2f\n",a,op,b,result);       
16     return 0;
17 }

12、日期合法性判定

 從鍵盤輸入一個8位整數表示的日期(即yyyymmdd,表示yyyy年mm月dd日),判斷該日期是否為合法日期。

[輸入樣例] 19990101

[輸出樣例] 1999年1月1日是合法日期

[輸入樣例] 19361321

[輸出樣例] 1936年13月21日是非法日期

[輸入樣例] 19780431

[輸出樣例] 1978年4月31日是非法日期

[輸入樣例] 20000229

[輸出樣例] 2000年2月29日是合法日期

[輸入樣例] 21000229

[輸出樣例] 2100年2月29日是非法日期

[輸入樣例] 19980229

[輸出樣例] 1998年2月29日是非法日期

 1 #include <stdio.h>
 2 #define N 100
 3 int main()
 4 {    
 5     int arr[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
 6     int y,m,d,flag =0;
 7     scanf("%4d%2d%2d",&y,&m,&d);
 8     int leapyear = y%4==0&&y%100!=0||y%400==0;
 9     if(leapyear) arr[2]++;
10     if(m<=12&&m>=1&&d<=arr[m]&&d>=1)
11         flag = 1;
12     printf("%d年%d月%d日",y,m,d);  
13     if(flag)
14         printf("是合法日期");
15     else
16         printf("是非法日期");         
17     return 0;
18 }

13、求完數

一個正整數如果恰好等於它的因子之和,這個數就稱為“完數”,編程找出X之內(包含X)的所有完數並從小到大依次輸出各個完數,以空格間隔。例如:6=1+2+3,所以6就是完數。

[樣例輸入] 100

[樣例輸出] 6 28

[樣例輸入] 1000

[樣例輸出] 6 28 496

 1 #include<stdio.h>
 2 int fun(int i)
 3 {
 4     int sum=0,t = i-1;  
 5     while(t)
 6     {        
 7         if(i%t==0)     
 8             sum += t;
 9         t--;
10     }
11     if(sum==i) return 1;
12     return 0;        
13 }
14 
15 int main()
16 {
17     int n,flag=0;
18     scanf("%d",&n);
19    
20     for(int i=6;i<=n;++i)
21     {
22         if(fun(i)){
23             if(flag) printf(" ");
24             printf("%d",i);
25             flag = 1;
26         }
27     }    
28     return 0;
29 }

14、生日蠟燭

[題目描述] 某君從某年開始每年都舉辦一次生日party,並且每次都要吹熄與年齡相同根數的蠟燭。現在算起來,他一共吹熄了N根蠟燭。

請問,他從多少歲開始辦生日party的,現在他多少歲?

例如:若N=236,因為236=26+27+28+29+30+31+32+33,所以,他從26歲開始辦生日party,他現在33歲。

[輸入說明] 輸入一共吹熄的蠟燭數N。

[輸出說明] 輸出他辦生日party的起止年齡數,以空格隔開;若有多種情況,輸出起始年齡最小的一種情況。

[樣例輸入] 236

[樣例輸出] 26 33

[樣例輸入] 186

[樣例輸出] 10 21

 1 #include<stdio.h>
 2 void fun(int n, int *s,int *e){
 3     for(int i=1;i<=100;++i)
 4     {
 5         int sum=0;
 6         for(int j=i; j<=100; ++j){
 7             sum += j;
 8             if(sum==n){
 9                 *s=i;
10                 *e=j;
11                 return;
12             }            
13         }            
14     }    
15 }
16 int main()
17 {
18     int n,s,e;
19     scanf("%d",&n);
20     fun(n,&s,&e);
21     printf("%d %d",s,e);
22     return 0;
23 }

15、整數排序

[題目描述] 從鍵盤輸入n個整數,將其從小到大進行排序,並將排序后的結果輸出。特別說明:待排序的整數的個數不超過20個。

[輸入說明] 第一行為n值,第二行輸入n個整數,以空格間隔。

[輸出說明] 輸出的數據用空格隔開。

[樣例輸入] 

5

5 6 1 2 3

[樣例輸出]

1 2 3 5 6

 1 /*快速排序*/
 2 #include <stdio.h>
 3 void QuickSort(int *a,int n, int left, int right)
 4 {
 5     int i,j,t; 
 6         /*左指針left指向數組頭 右指針right指向數組尾*/
 7     if(left<right){
 8         i=left,j=right+1;/*左右指針*/
 9         while(i<j){
10             while(i+1<n && a[++i]<a[left]);/*左指針右移 指向大於基數的數據停止*/
11             while(j-1>-1 && a[--j]>a[left]);/*右指針左移 指向小於基數的數據停止*/
12             if(i<j)/*滿足左指針小於右指針的條件 兩指針指向數據交換*/
13                 t=a[i],a[i]=a[j],a[j]=t;         
14         }
15         t=a[left],a[left]=a[j],a[j]=t;/*右指針指向數據與基數交換*/
16         QuickSort(a,n,left,j-1);/*左邊數據遞歸*/
17         QuickSort(a,n,j+1,right);/*右邊數據遞歸*/
18     }
19 }
20 
21 int main()
22 {
23     int i,n, arr[20]={0};
24     scanf("%d",&n);
25     for(i=0; i<n; ++i)
26         scanf("%d",&arr[i]);
27     QuickSort(arr,n,0,n-1);
28     for(i=0; i<n; ++i)        
29         printf("%d ",arr[i]);         
30     return 0;
31 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM