# 实验任务1

 1 #include<stdio.h>
2 long long fac(int n);
3 int main(){
4     int i,n;
5     printf("Enter n: ");
6     scanf("%d",&n);
7
8     for (i=1;i<=n;i++){
9         printf("%d !=%lld\n",i,fac(i));
10
11     }
12         return 0;
13     }
14
15     long long fac(int n){
16     static long long p = 1;
17     p = p*n;18　　　printf("p = %lld\n", p);
19     return p;
20     }

#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;
}

局部static变量的特性：分配静态内存，当函数调用完成后，内存不回收。

# 实验任务2

//迭代
#include <stdio.h>
void printSymbol(int n, char symbol);
int main()
{
int n;
char symbol;
while( scanf("%d %c", &n, &symbol) != EOF )
{
printSymbol(n, symbol);
printf("\n");
}
return 0;
}
void printSymbol(int n,char symbol){
int i;
for (i=0;i<n;i++)
printf("%c",symbol);
}

//递归
#include <stdio.h>
void printSymbol(int n, char symbol);
int main()
{
int n;
char symbol;
while( scanf("%d %c", &n, &symbol) != EOF )
{
printSymbol(n, symbol);
printf("\n");
}
return 0;
}
void printSymbol(int n,char symbol){
if(n!=0){
printf("%c",symbol);
return printSymbol (n-1,symbol);
}

}

第一种好点，更直观，只调用了一次，但第二种解决复杂点的问题可能有奇效，各有优劣。

# 实验任务3

#include <stdio.h>
long long fun(int n);
int main()
{
int n;
long long f;
while (scanf("%d", &n) != EOF)
{
f = fun(n);
printf("n = %d, f = %lld\n", n, f);
}
return 0;
}
long long fun(int n){
if(n==1)
return 1;
else
return 2*(fun(n-1)+1)-1;
}

# 实验任务4

#include<stdio.h>
#include<math.h>
int isPrime(int n){
int i;
for(i = 2;i<=sqrt(n);i++)
if(n%i==0)
return 0;
return 1;

}
int main(){
int i;
int num  = 0 ;
int k =0;
for(i=101;i<=200;i++){

if (isPrime(i) == 0){

printf("%d ",i);
num++;
k++;}
if(k==10){
printf("\n");
k = 0;
}
}
printf("\n一共有%d个非素数",num);
}

# 实验任务5

#include <stdio.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 i=1;
int t;
int num=0;
while(s!=0){
t = s%10;
if (t % 2 ==1){
num = num + t*i;
i *=10;
}
s = s/10;
}
return num;
}

# 实验任务6

#include <stdio.h>
#include <math.h>
double fun(int n);
int main()
{
int n;
double s;
printf("Enter n(1~10): ");
while (scanf("%d", &n) != EOF)
{
s = fun(n);
printf("n = %d, s= %f\n\n", n, s);
printf("Enter n(1~10): ");
}
return 0;
}
double fun (int n){
double qwq=1;
int i;
double s =0;

for(i=1;i<=n;i++){
qwq = qwq * i;
s= s+pow(-1,i-1)*(1/qwq);}
return s;
}

# 万事收工

posted @ 2021-11-24 11:34  Konpaukuy  阅读(32)  评论(3编辑  收藏  举报