实验三 函数应用编程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
1.任务1#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define N 80
 
void print_text(int line,int col,char text[]);
void print_spaces(int n);
void print_blank_lines(int n);
 
int main(){
    int line,col,i;
    char text[N]="hi,November~";
     
    srand(time(0));
    for(i=1;i<=10;i++){
        line =rand()%25;
        col=rand()%80;
        print_text(line,col,text);
        Sleep(1000);//暂停1000ms
    }
    return 0;
}
//打印n个空格
void print_spaces(int n){
    int i;
    for(i=1;i<=n;i++)
        printf("  ");
}
//打印n行空白行
void print_blank_lines(int n) {
    int i;
    for(i = 1; i <= n; ++i)
        printf("\n");
}
// 在第line行第col列打印一段文本
void print_text(int line, int col, char text[]) {
    print_blank_lines(line-1); // 打印(line-1)行空行
    print_spaces(col-1); // 打印(col-1)列空格
    printf("%s", text); // 在第line行、col列输出text中字符串
}功能:主函数将随机数传递给定义的函数,产生对应的空格和空行,第三个函数指的是在打印时在第line行第col列打印一段文本hi,November 2.任务二Task2.1c#include <stdio.h>
long long fac(int n); // 函数声明
int main() {
    int i, n;
    printf("Enter n: ");
    scanf("%d", &n);
    for (i = 1; i <= n; ++i)
        printf("%d! = %lld\n", i, fac(i));
         
    return 0;
}
long long fac(int n){
    static long long p=1;
    p=p*n;
    return p;
}#include <stdio.h>
int func(int, int); // 函数声明
int main() {
  int k = 4, m = 1, p1, p2;
  p1 = func(k, m); // 函数调用
  p2 = func(k, m); // 函数调用
  printf("%d, %d\n", p1, p2);
  return 0;
}
// 函数定义
int func(int a, int b) {
  static int m = 0, i = 2;
  i += m + 1;
  m = i + a + b;
  return m;
}long long func(int n); // 函数声明
int main() {
  int n;
  long long f;
  while (scanf("%d", &n) != EOF) {
         f = func(n); // 函数调用
         printf("n = %d, f = %lld\n", n, f);
 }
return 0;
}
long long func(int n){
    if(n==1){
        return 1;
    }else{
        return 2*func(n-1)+1;
    }
}4.任务四:#include <stdio.h>
int func(int n,int m);
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
        printf("n=%d,m=%d,ans=%d\n",n,m,func(n,m));
     
    return 0;
     
}
int func(int n,int m){
    if(n==m||m==0){
        return 1;
    }if(m==1){
        return n;
    }if(n<m){
        return 0;
    }
    else{
        return func(n-1,m)+func(n-1,m-1);
    }
}注意:递推时的起始值#include <stdio.h>
int mul(int n,int m);
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    printf("%d*%d=%d\n",n,m,mul(n,m));
    return 0;
}
int mul(int n,int m){
    if(m==0){
        return 0;
    }else{
        return n+mul(n,m-1);
    }
}6.任务六#include <stdio.h>
#include<stdlib.h>
void hanoi(unsigned int n,char from,char temp,char to);
void moveplate(unsigned int n,char from,char to);
int count=0;
int main(){
    unsigned int n;
    while(scanf("%u",&n)!=EOF){
        hanoi(n,'A','B','C');
        printf("一共移动了%lld次\n",count);
        count=0;
    }
     
     
     
    return 0;
}
void hanoi(unsigned int n,char from,char temp,char to){
 
    if(n==1){
    moveplate(n,from,to);   
    }else{
        hanoi(n-1,from,to,temp);
        moveplate(n,from,to);
        hanoi(n-1,temp,from,to);
    }
}
void moveplate(unsigned int n,char from,char to){
    printf("%u:%c-->%c\n",n,from,to);
    count++;
     
}7.任务七#include<stdio.h>
#include<math.h>
int is_prime(int n){
    int i;
    for(i=2;i<=sqrt(n);i++){
        if(n%i==0)
        break;
    }
    if(i<=sqrt(n)||i==1){
        return 0;
    }else{
        return 1;
    }
}
int main(){
    int i=1;
    int m,t;
    for(i=2;i<=10;i++){
        t=2*i;
        for(m=2;m<=i;m++){
            if((is_prime(m)==1)&&(is_prime(2*i-m)==1)){
                break;   
            }
        }
        printf("%d=%d+%d\n",t,m,2*i-m);
    }
} 8.任务8#include <stdio.h>
#include<math.h>
long fun(long s); // 函数声明
int main() {
    long s, t;
    printf("Enter a number: ");
    while (scanf("%ld", &s) != EOF) {
        t = fun(s); // 函数调用
        printf("new number is: %ld\n\n", t);
        printf("Enter a number: ");
    }
return 0;
}
long fun(long s){
    int m[100];
    int n=0;
    int cout=0;
    int i;
    while(s>0){   
        if((s%10)%2==1){
            m[cout++]=s%10;
        }
        s=s/10;   
    }
    for(i=cout-1;i>=0;i--){
        n+=pow(10,i)*m[i];
    }
    return n;
         
}  
复制代码
复制代码

分析:static变量为静态变量,此处为静态局部变量,即只有在第一次调用的时候被分配,之后再次进入该函数时使用上次的运行结果,不会再重新定义

复制代码
复制代码

第一次进入函数运行后,此时m=8,i=3,则第二次函数继续运行时以m=8,i=3继续结果为17

3.任务三:

复制代码
复制代码

复制代码

5.任务五

复制代码
复制代码

复制代码
复制代码
复制代码

 

 

复制代码

注意:在自定义的函数中使用数组输出时用for循环不可直接传输到主函数中

复制代码

 

复制代码

 

 

复制代码

 

posted @   王译曼  阅读(88)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示