C语言(函数,DEVFORGE学编程社区)

Posted on 2019-09-19 15:26  金色的省略号  阅读(502)  评论(0编辑  收藏  举报

1、A+B 2

 1 #include<stdio.h>
 2  
 3 int fun(int m, int n)
 4 {
 5     return (m+n)%100;
 6 }
 7  
 8 int main()
 9 {
10     int n,a,b;    
11     scanf("%d",&n);
12     while(n--)
13     {
14         scanf("%d%d",&a,&b);
15         printf("%d\n",fun(a,b));
16     }    
17     return 0;
18 }

2、最小整数

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

3、最大整数

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

4、ACKERMAN

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

5、发工资的日子

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

6、逆序整数

 1 #include <stdio.h>
 2 void fun(int n) 
 3 {    
 4     if(n>0){
 5         printf("%d",n%10);
 6         fun(n/10);
 7     }         
 8 }
 9 
10 int main()
11 {
12     int n;    
13     scanf("%d",&n);
14     fun(n);
15     return 0;
16 }

7、组合数

 1 #include <stdio.h>
 2 int c(int n,int m)
 3 {
 4     /*temp 为答案*/
 5     double temp = 1;
 6     /*保证m>=n-m*/
 7     if(m<n-m)
 8         return c(n,n-m);
 9     for(int i=0;i<n-m;++i){
10         temp*=m+i+1;
11         temp/=i+1;
12     }
13     return (int)temp;
14 }
15 
16 
17 int main()
18 {
19     int n,m;    
20     scanf("%d%d",&n,&m);
21     int ret = c(n,m);
22     if(ret)
23         printf("%d",ret);
24     else
25         printf("wrong");        
26     return 0;
27 }

8、高低交换

 1 #include <stdio.h>
 2 
 3 void fun(unsigned char a)
 4 {
 5     unsigned char height = (a & 0x0F)<<4;
 6     unsigned char low = a >> 4;
 7     printf("%d",low+height);
 8 }
 9 
10 int main()
11 {
12     int a;
13     scanf("%d",&a);
14     fun(a);
15     return 0;
16 }

9、数据加密

 1 #include <stdio.h>
 2 
 3 int fun1(int n);
 4 void fun2(int a);
 5 
 6 int main()
 7 {
 8     int a;    
 9     scanf("%d",&a);
10     fun2(a);    
11     return 0;
12 }
13 
14 int fun1(int n)
15 {
16     return (n+5)%10;
17 }
18 
19 void fun2(int a)
20 {
21     int arr[4] = {0}, i=0;
22     while(a)
23     {
24         arr[i++] = fun1(a%10);
25         a /= 10;
26     }
27     
28     for(int j=0; j<4; ++j)
29         printf("%d",arr[j]);
30 

10、不会吧,又是A+B

 1 #include <stdio.h>
 2 
 3 void fun(int hour, int minute, int second);
 4 
 5 int main()
 6 {
 7     int h,m,s,n=2;
 8     int second=0, minute=0, hour=0;
 9     
10     while(n--)
11     {
12         scanf("%d%d%d",&h,&m,&s);
13         second += s;
14         minute += m;
15         hour += h;
16     }
17     
18     fun(hour,minute,second);
19 
20     return 0;
21 }
22 
23 
24 void fun(int hour, int minute, int second)
25 {
26     if (second>59)
27     {
28         minute += second/60;
29         second %= 60;    
30     }
31 
32     if (minute>59)
33     {
34         hour += minute/60;
35         minute %= 60;        
36     }
37     
38     printf("%d %d %d", hour,minute,second);
39 }