• 【无标题】


    1、n项多项式求和

    s=1/1x2/1…1/(2n-1)x(2n/(2n-1))

    #include
    #include
    #include
    #include
    
    #define N 5
    
    
    
    int main() {
    
    
       
        int n;
    	float term=1,sum=0.0;//初始化term和s的值
    	
    	for(n=1;term>1e-6;n++){ //精度控制在le-6 
    		 term=(1.0/(2*n-1))*(2*n/(2*n-1));
    		 sum+=term; 
    	} 
    	
    	printf("%f",sum); 
    
    
    
    	return 0;
    }
    
    • 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

    2、nn魔方矩阵实现每行、每列、每一对角线上的元素之和相等 (将1~nn每一个元素放到合适位置)

    #include
    #include
    #include
    #include
    
    #define N 15
    
    
    
    
    
    int main() {
    
    
    
    	int a[N][N]= {0};
    	int k;
    	printf("请输入魔方矩阵k=");
    	scanf("%d",&k);
    
    	while(1) {
    		if(k%2==0) {
    			printf("魔方阶数只能为奇数,请重新输入\n");
    			scanf("%d",&k);
    		} else {
    			break;
    		}
    	}
    
    	int i,j,m,n;//m、n用于暂时存放上一次i、j
    	i=0,j=k/2;
    	a[i][j]=1;//初始位置为第一行、中间列
    	for(int x=2; x<=k*k; x++) {
    		m=i,n=j;
    		i--,j++;//向45°右上方移动也就是行减1,列加1
    		if(i<0)i=k-1;//如果已经在第一行,那么下一次移动到最后一行
    		if(j>=k)j=0; //如果已经在第n列,下一次移动到第一列
    		if(a[i][j]!=0) {
    			//如果该位置已经有元素占领,则存放在下个位置
    			i=m+1;
    			j=n;
    		}
    		a[i][j]=x;
    	}
    
    	for(int i=0; i<k; i++) {
    		for(int j=0; j<k; j++) {
    			printf("%d ",a[i][j]);
    		}
    		printf("\n");
    	}
    
    
    	return 0;
    }
    
    • 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

    3、 求前20项和 2/1+3/2+5/3…13/8+21/13

    #include
    #include
    #include
    #include
    
    #define N 15
    
    
    
    
    
    int main() {
    
    
    
        int i,n=20;
    	float a=2,b=1,s=0,t;//a是分子,b是分母
    	
    	for(int i=0;i<n;i++){
    		s+=(a/b);
    		t=a;
    		a+=b;//分子为前一项分子和分母之和 
    		b=t; //分母为前一项分子 
    	} 
    	printf("%f",s);//32.660263
    
    
    	return 0;
    }
    
    • 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

    4、第一项为2 第二项为3
    序列最后两项乘积<10,则最后项为两项乘积
    如果最后两项乘积>10,则拆分十位和个位,分别位新的两项

    #include
    #include
    #include
    #include
    
    #define N 15
    
    
    
    
    
    int main() {
    
    
    
    	int n,cnt,i=2;
    	int a[100];
    	a[0]=2,a[1]=3;
    	while(cnt!=100) {
    		int t=a[i-1]*a[i-2];
    		if(t<10) {
    			a[i++]=t;
    			cnt+=1; 
    		} else {
    			a[i++]=t/10;
    			a[i++]=t%10;
    			cnt+=2; 
    		}
    	}
    	
    	for(int i=0;i<100;i++){
    		printf("%d  ",a[i]); 
    	} 
    
    
    	return 0;
    }
    
    • 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

    5、调和级数 1/1+1/2+1/3+…+1/n 的前n项和,要求用分数

    #include
    #include
    #include
    #include
    
    #define N 15
    
    int gcb(int x,int y) {
    	return y==0?x:gcb(y,x%y);
    }
    
    
    
    int main() {
    
    
    	int n,u=1,v=1;//n为级数项数、u为分子、v为分母
    
        scanf("%d",&n); 
    
    	for(int i=2; i<=n; i++) {
    		//通分求和
    		u=u*i+1*v;
    		v*=i;
    		int g=gcb(u,v);//用最大公约数,约分
    		u/=g;
    		v/=g;
    		printf("%d/%d  ",u,v);
    	}
    
    	return 0;
    }
    
    • 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

    6、求出sinx的近似值 sinx用级数展开

    在这里插入图片描述

    #include
    #include
    #include
    #include
    
    #define N 15
    
    int  func(int n) {
    	int k=1;
    	for(int i=1; i<=n; i++) {
    		k=k*i;
    	}
    	return k;
    }
    
    
    
    int main() {
    
    
    	int x,n,sign=1;
    	double  term,s=0;
    
    	scanf("%d",&x);
    
    	term=x;
    
    	for(n=1; term>5e-6; n+=2) { //每一项为奇数项
    		term=pow(x,n)/func(n); 
    		s=s+sign*term;
    		sign=-sign;
    	}
    	
    	printf("sin%d的近似值为: %.8lf",x,s); 
    
    
    
    
    
    
    	return 0;
    }
    
    • 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

    求级数 1/1-1/2+1/3+…+1/n

    #include
    #include
    #include
    #include
    
    #define N 15
    
    
    
    
    
    int main() {
    
        
        int  n,sign=1;//设置负号变量
    	scanf("%d",&n);
    	
    	float s=0; 
    	for(int i=1;i<=n;i++){
    		s+=(float)sign/i;
    		sign=-sign; 
    	} 
    	
    	printf("%f",s); 
    
    
    
    
    
    	return 0;
    }
    
    • 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

    7、输出分母小于或者等于n的最简真分数(不能约分),并且从小到大排序输出

    n=5 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5

    #include
    #include
    #include
    #include
    
    #define  maxn 999
    
    
    struct  struct_result {
    	int  numerator;//分子
    	int  denominator;//分母
    	float result;//分数
    
    	bool operator <(const struct_result x) {
    		return result<=x.result;
    	}
    } list[maxn];
    
    int length;
    
    
    int  gcb(int x,int y) {
    	return y==0?x:gcb(y,x%y);
    }
    
    
    //快排
    int  partiton(struct_result list[],int low,int high) {
    	struct_result pivot=list[low];
    	while(low<high){	
    	while(low<high&&list[high].result>=pivot.result)high--;
    	list[low]=list[high];
    	while(low<high&&list[low].result<=pivot.result)low++;
    	list[high]=list[low];
       }
    	list[low]=pivot;
    	return low;
    }
    
    void quick_sort(struct_result list[],int low,int high) {
    	if(low<high) {
    		int pivotPos=partiton(list,low,high);
    		quick_sort(list,low,pivotPos-1);
    		quick_sort(list,pivotPos+1,high);
    	}
    }
    
    
    
    
    
    int main() {
    
    
    	int n;
    	scanf("%d",&n);
    
    	for(int i=1; i<=n; i++) { //枚举分子和分母
    		for(int j=i+1; j<=n; j++) {
    			if(gcb(i,j)==1) {
    				//如果为最简真分数则添加进入数组
    				list[length].numerator=i;
    				list[length].denominator=j;
    				list[length++].result=(float)i/j;
    			}
    		}
    	}
    	quick_sort(list,0,length-1);
    
    	for(int i=0; i<length; i++) {
    		printf("%d/%d  ",list[i].numerator,list[i].denominator);
    
    		if(i+1%10==0)printf("\n");//每十个换行
    	}
    
    
    
    
    
    
    
    	return 0;
    }
    
    • 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

    8、对100个点相互组合组成的线段求出距离最小的长度

    #include
    #include
    #include
    #include
    
    #define  maxn 999
    
    struct point{
    	 int x,y; 
    }point[maxn];
    
    
    
    int dis(struct point a,struct point b){
    	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    } 
    
    int main() {
    	
    
        for(int i=0;i<100;i++){
        	scanf("%d%d",&point[i].x,&point[i].y); 
    	} 
    	int mind=0x7f7f7f7f;
    	for(int i=0;i<100;i++)
    	  for(int j=i+1;j<100;j++){
    	  	   if(dis(point[i],point[j])<mind){
    	  	   	      mind=dis(point[i],point[j]);
    			 }
    	  } 
       
         printf("maxdis=",sqrt(mind));
      
    
    
    
    
    
    	return 0;
    }
    
    • 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

    9、给出100个点,任意三个点组成三角形,求这些三角形的最大面积是多少

    给出三个点,可以用向量来做,利用叉积=|a|x|b|xsina 等于平行四边面积为对于三角型面积二倍来求出三角形

    也可以利用海伦公式,求出周长为2p 利用海伦公式s=根号下px(p-x)x(p-y)x(p-z)

    #include
    #include
    #include
    #include
    
    #define  maxn 999
    
    typedef struct Point{
    	 double x,y; 
    }Point;
    
    
    Point decPoint(Point p1,Point p2){ //将坐标转换为向量 
    	Point ret;
    	ret.x=p1.x-p2.x;
    	ret.y=p1.y-p2.y;
    	return ret;
    }
    
    double multiPoint(Point p1,Point p2){//叉乘=x2y1-x1y2 
    	 return p1.x*p2.y-p1.y*p2.x; 
    }
    
    double  areaByThreePoint(Point A,Point B,Point C){ //三角形面积 
    	return fabs(multiPoint(decPoint(A,B),decPoint(B,C))); 
    } 
    
    
    
    
    int main() {
    	
    	int i,j,k;
    	double maxarea; 
    	Point pts[110];
    	
    	for(int i=0;i<100;i++){
    		scanf("%lf%lf",&pts[i].x,&pts[i].y); 
    	} 
    	
    	maxarea=areaByThreePoint(pts[0],pts[1],pts[2]);
    	
    	for(i=0;i<100;i++){
    		for(j=i+1;j<100;j++){
    			for(k=j+1;k<100;k++){
    				double t=areaByThreePoint(pts[i],pts[j],pts[k]);
    				if(maxarea<t)maxarea=t; 
    			} 
    		} 
    	} 
    	
    	printf("面积最大为:%.2lf",maxarea/2.0); 
    	 
    	
    
        
    
    
    
    	return 0;
    }
    
    • 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

    10、给出100个圆的圆心坐标和半径,判断哪些圆相切,将相切的圆用一个链表相连接,链表中存放圆的编号

    #include
    #include
    #include
    #include
    
    #define  maxn 999
    
    typedef struct node{
    	int m,n;
    	struct node* next; 
    }Node;
    
    typedef struct circle{
    	 int x,y;
    	 double r; 
    }Circle; 
    
    bool  isQie(circle a,circle b){
    	  double dis=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);//圆心直接距离的平方 
    	  if(pow(a.r+b.r,2)==dis)return true;
    	  
    	  return false; 
    } 
    
    
    
    
    int main() {
    	
         Node* head,p;
    	 Circle  cir[110];
    	 for(int i=0;i<100;i++){
    	 	scanf("%d%d%f",cir[i].x,cir[i].y,cir[i].r); 
    	 } 
    	 for(int i=0;i<100;i++){
    	 	for(int j=i+1;j<100;j++){
    	 		 if(isQie(cir[i],cir[j])){
    	 		 	 p=(Node*)malloc(sizeof(Node));//头插法 
    				   p->m=i;
    				   p->n=j;
    				   p->next=head;
    				   head=p; 
    			  } 
    		 } 
    	 } 
    	 
        
      
    
    
    
    
    
    	return 0;
    }
    
    • 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

    11、求解二元一次方程 ax^2+bx+c=0

    #include
    #include
    #include
    #include
    
    #define  maxn 999
    
    
    
    
    
    
    int main() {
    	
         
         double a,b,c,disc,x1,x2;
    	 
    	 scanf("%lf%lf%lf",&a,&b,&c);
    	 if(fabs(a)<1e-6)printf("不是二元一次方程");//a=0
    	 else{
    	 	 disc=b*b-4*a*c;//判断几个实根
    		 if(fabs(disc)<=1e-6)printf("%f",-b/(2*a));//只有一个根
    		 else if(fabs(disc)>1e-6){
    		 	 x1=(-b+sqrt(disc))/(2*a);
    		 	 x2=(-b-sqrt(disc))/(2*a);
    			 
    			 printf("两个解分别为%lf,%lf",x1,x2); 
    		 }else{
    		 	printf("无解");//disc<0 
    		 } 
    	 } 
        
      
    
    
    
    
    
    	return 0;
    }
    
    • 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

    12、利用二分法找到 2x^3- 4x^3+3x-6=0 位于-10~10直接的一个跟 x=3

    #include
    #include
    #include
    #include
    
    #define  eps 1e-6 
    
    
    double  f(double x){
    	return  2*x*x*x-4*x*x+3*x-6; 
    } 
    
    
    
    int main() {
    	
         
        double m=-10,n=10;
    	double i,s,k;
    	if(f(m)*f(n)<0){
    		 while(fabs(m-n)>eps){//两个端点不相同 
    		 	 i=(m+n)/2;
    			 s=f(i);
    			 if(fabs(s)<eps)break;
    			 else if(f(i)*f(m)<0)n=i;
    			 else if(f(i)*f(n)<0)m=i; 
    		 } 
    	} 
    	
    	printf("%lf%lf",m,n);
    	k=(m+n)/2;
    	printf("%lf",k); 
      
    
    
    
    
    
    	return 0;
    }
    
    • 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

    13、利用牛顿迭代法求12题根,x1=1.5

    牛顿迭代法

    #include
    #include
    #include
    #include
    
    #define  eps 1e-6 
    
    
    float fun(float x){
    	return 2*x*x*x-4*x*x+3*x-6; 
    } 
    
    float fun1(float x){
    	return 6*x*x-8*x+3; 
    } 
    
    
    int main() {
    	
         
        float x1=1.5,x2=x1-fun(x1)/fun1(x1); //根据斜率公式表示新的与x轴交点x2 
    	while(fabs(x1-x2)>1e-8){
    		x1=x2;
    		x2=x1-fun(x1)/fun1(x1); 
    	} 
    	
    	printf("%f",x2); 
      
    
    
    
    
    
    	return 0;
    }
    
    • 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

    14、判断某点是否在三角形内部

    利用叉乘,判断D点是否在AB BC CA 的左侧, 分别CA 与AD做叉乘,AB 与BD做叉乘,BC与CD做叉乘,判断符号是否相同

    #include
    #include
    #include
    #include
    
    #define  eps 1e-6 
    
    typedef struct Point{
    	double x,y; 
    }Point;
    
    Point decPoint(Point p1,Point p2){ //定义向量 
    	Point ret;
    	ret.x=p1.x-p2.x;
    	ret.y=p1.y-p2.y;
    	return ret; 
    }
    
    
    double multiPoint(Point  ver1,Point ver2){ //叉乘 
    	return (ver1.x*ver2.y-ver1.y*ver2.x); 
    } 
    
    
    int sign(double x){ //判断符号 
    	if(fabs(x)<eps)return 0;//等于0
    	else if(x>0)return 1;
    	return 0; 
    } 
    
    
    
    int main() {
    	
          Point points[4];// A B C D
    	  
    	  for(int i=0;i<4;i++){
    	     scanf("%lf%lf",&points[i].x,&points[i].y); 
    	  } 
    	  
    	  int sgn[3];//存放三个叉乘后向量的符号
    	  
    	  
    	  //叉乘向量 CA 和 AD
    	  Point CA=decPoint(points[0],points[2]);
    	  Point AD=decPoint(points[3],points[0]);
    	  double verA=multiPoint(CA,AD);
    	  sgn[0]=sign(verA);
    	  printf("%lf\n",verA); 
    	  
    	  //叉乘 AB BD
    	  Point AB=decPoint(points[1],points[0]);
    	  Point BD=decPoint(points[3],points[1]);
    	  double verB=multiPoint(AB,BD);
    	  sgn[1]=sign(verB); 
    	  	  printf("%lf\n",verB); 
    
    	  
    	  //叉乘 BC  CD
    	  Point BC=decPoint(points[2],points[1]);
    	  Point CD=decPoint(points[3],points[2]);
    	  double verC=multiPoint(BC,CD);
    	  sgn[2]=sign(verC); 
    	
    		  printf("%lf\n",verC); 
    
    	   
          if(sgn[0]==0||sgn[1]==0||sgn[2]==0)printf("Point D is on the triangle!"); //在边上或者延长线上 
    	  else if(sgn[0]==sgn[1]&&sgn[1]==sgn[2]) printf("Point D is in the triangle!");
    	  else printf("Point D is out of the triangle!"); 
    
    
    
    
    
    	return 0;
    }
    
    • 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

    15、递归编写函数,求出数组中所有数的和

    #include
    #include
    #include
    #include
    
    #define  eps 1e-6 
    
    int  sum(int n,int a[]){
    	if(n==1)return a[0];
    	
    	return sum(n-1,a)+a[n-1];
    }
    
    
    int main() {
    	 
    	 
    	 int a[10]={1,2,3,4,5,6,7,8,9,10};
    	 printf("%d",sum(10,a));
    	
    
    
    
    
    	return 0;
    }
    
    • 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

    16、利用递归求出一个整数个个位数之和

    #include
    #include
    #include
    #include
    
    #define  eps 1e-6 
    
    int  sum(int n){
    	
    	if(n<10)return n;
    	
    	return n%10+sum(n/10);
    }
    
    
    int main() {
    	 
    	 
        int n=1031;
        printf("%d",sum(n));
    	
    
    
    
    
    	return 0;
    }
    
    • 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

    17、一根木条在五分之二处截断, 将剩余木条也在五分至二处截断,知道所有木条长度均小于5,求出一个折成多少木条

    #include
    #include
    #include
    #include
    
    #define  eps 1e-6 
     
    
    int  f(double n){
    	if(n<=5)return 1;
    	
    	return f(2*n/5)+f(3*n/5);
    }
    
    int main() {
    	 
         
         printf("%d",f(6));
    
    
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    18、读入文件中学生成绩,将学生成绩用一个链表有高分到低分排列起来,一边读入,一边排序

    #include
    #include //malloc
    
    
    typedef struct  Grade{
    	 int num;
    	 char name[20];
    	 int disscore;
    	 int repscore;
    	 int testscore;
    	 int totscore;
    	 struct Grade* next; 
    }GradeNode,*GradeList; 
     
     
     
    
    int main() {
    	
    	FILE* fpin=fopen("2018Exp.txt","r");//打开文件 
    	 
    	GradeList  head;
    	head->next=NULL; 
        GradeNode*  gNode; 
    	while(!feof(fpin)){ //如果文件不为空 
    		 gNode=(GradeNode*)malloc(sizeof(GradeNode)); 
    		 fscanf(fpin,"%d",&gNode->num);
    		 fscanf(fpin,"%s",&gNode->name);
    		 fscanf(fpin,"%d",&gNode->disscore);
    		 fscanf(fpin,"%d",&gNode->repscore);
    		 fscanf(fpin,"%d",&gNode->testscore);
    		 
    		 gNode->totscore=(int)(0.2*gNode->disscore+0.2*gNode->repscore+0.6*gNode->testscore);
    		 
    		 GradeNode* p=head->next;//p为当前节点 
    		 GradeNode* q=head;//q为p的前一个节点 
    		 while(p&&p->totscore>gNode->totscore){//找到gNode所在合适的位置 
    		 	  p=p->next;
    		 	  q=q->next;
    		 }
    		 q->next=gNode;
    		 gNode->next=p;
    	} 
    	
    	fclose(fpin);//关闭文件 
    	 
         
        
    
    
    	return 0;
    }
    
    • 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

    19、从键盘接收学生个数、学生数据返回一个学生链表,学号由小到大排名,将链表写入文件

    #include
    #include //malloc
    
    typedef struct Student{
    	int stuNum;
    	int score; 
    	char sex;
    	char name[20];
    	struct  Student* next; 
    }Student,*StuList;
    
     StuList head=NULL;//设置头节点
     
     StuList CreateList(int n){
     	  Student* stu;
    	  while(n){
    	  	stu=(Student*)malloc(sizeof(Student));
    		scanf("%d%d",&stu->stuNum,&stu->score);
    		scanf("%c",&stu->sex);
    		scanf("%s",&stu->name); 
    		
    		Student *p=head->next;
    		Student *q=head;
    		while(p&&p->stuNum<stu->stuNum){
    			p=p->next;
    			q=q->next; 
    		} 
    		q->next=stu;
    		stu->next=p; 
    		n--; 	
    	  }
    	  return head; 
     } 
    
    bool WriteFile(StuList stuList){
    	FILE* fpin;
    	
    	if((fpin=fopen("output.txt","w"))==NULL){
    		printf("FILE Open Error!");
    		return false; 
    	} 
    	
    	while(stuList){
    		fprintf(fpin,"%d  ",stuList->stuNum); 
    		fprintf(fpin,"%d  ",stuList->score);
    		fprintf(fpin,"%c  ",stuList->sex);
    		fprintf(fpin,"%s\n  ",stuList->name); 
    		stuList=stuList->next; 
    	} 
    	
    	fclose(fpin);
    	return true; 	 
    } 
     
    
    int main() {
    	 
    	 
    	 int n;
    	 printf("请输入学生个数: ");
    	 scanf("%d",&n);
    	  
    	 
    	 head=CreateList(n); 
    	 WriteFile(head); 
    	
    	
         
    
    
    	return 0;
    }
    
    • 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

    20、将小写字母转换为大写字母,并且写入文件中

    #include
    #include //malloc
    
    
    
    int main() {
    	 
    	 
    	 FILE *fpin;
    	 char str[100];
    	 printf("请输入字符串: \n");
    	 gets(str); 
    	 int i=0; 
    	 while(str[i]!='\0'){
    	     if(str[i]>='a'&&str[i]<='z'){
    	     	str[i]=str[i]-'a'+'A'; 
    		 } 
    		 i++; 
    	 } 
    	 
    	 
    	if((fpin=fopen("test.txt","w"))==NULL){
    		printf("无法打开文件\n"); 
    	} else{
    		fputs(str,fpin); 
    		fclose(fpin); 
    	} 
    	 
    	  
    	
         
        
    
    
    	return 0;
    }
    
    • 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

    21、创建学生链表,将指定年龄的学生删除,将删除后的链表写入文件

    #include
    #include //malloc
    
    
    typedef struct Student{
    	int num; 
    	int age;
    	char name[20];
    	
    	struct  Student * next; 
    	 
    }Student,*StuList;
    
    StuList head; 
    
    StuList  CreateList(int n){ //创建带头结点的单链表 
    	  Student *p,*r=head;
    	  
    	  while(n){ //尾插法 
    	  	  p=(Student*)malloc(sizeof(Student));
    		  scanf("%d%d%s",&p->num,&p->age,&p->name);
    		  r->next=p;
    		  r=p; 
    		  n--; 
    	  } 
    	  r->next=NULL;
    	  return head; 
    } 
    
    
    void  save(StuList list,int age){
    	FILE *file;
    	if((file=fopen("output.txt","w"))==NULL)printf("File Open Error!");
    	
    	Student *p=list->next;
    	Student *q=list; 
    	
    	while(p){
    		if(p->age==age){ //删除节点 
    		    q=p->next; 
    			free(p); 
    			p=q->next; 
    		} 
    	} 
    	p=head->next; 
    	while(p){
    		fprintf(file,"%5d\n",p->num);
    		fprintf(file,"%s\n",p->name);
    		fprintf(file,"5d\n",p->age); 
    		p=p->next; 
    	} 
    	
    	fclose(file); 
    } 
    
    
    int main() {
    	
    	  StuList list=CreateList(10);
    	  save(list,20); 
    	  
    	 
    	 
    	
    	  
    	
         
        
    
    
    	return 0;
    }
    
    • 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

    22、从键盘读入学生链表,进行冒泡排序,在从文件读入一个学生链表进行冒泡排序,最后将两个链表归并排序

    #include
    #include 
    
    
    typedef  struct  Student{
    	int num;
    	char name[10];
    	int grade;
    	struct  Student* next; 
    }Student,*StuList; 
    
    
    void BubbleSort(StuList list){ //传入带头节点的链表 
       	  
       	   bool flag=true;
       	   Student *p,*q,*r; 
    	   while(flag){ //  q p r  相对关系 
    	   	    q=list;
    			p=list->next;
    			r=p->next; 
    			flag=false; 
    			 while(r){
    			 	 if(p->grade>r->grade){
    			 	 	  p->next=r->next;//交换节点 
    						r->next=p;
    						q->next=r; 
    						p=r;//调回相对关系 
    						r=p->next; 
    						flag=true; 
    				  }
    				  	  q=p;
    					  p=r; 
    					  r=r->next;	  
    			 }
    	   }
    } 
    
    
    StuList  CreateList(int n){
    	StuList  head=(Student*)malloc(sizeof(Student));
    	head->next=NULL; 
    	Student *p;
    	
    	while(n){
    		p=(Student*)malloc(sizeof(Student));
    		printf("请输入学生学号: "); 
    		scanf("%d",&p->num);
    		printf("请输入学生姓名: ");
    		scanf("%s",&p->name);
    		printf("请输入学生成绩: ");
    		scanf("%d",&p->grade); 
    		p->next=head->next;
    		head->next=p;//头插法 
    		n--; 
    	} 
    	return head; 
    } 
    
    //按照学生成绩打印信息 
    void PrintList(StuList list){
    	 StuList p=list->next;
    	 while(p){
    	 	printf("%d-->",p->grade);
    		 p=p->next; 
    	 } 
    	 printf("\n"); 
    } 
    
    
    StuList  ReadList(int n){
    	 FILE *fpin;
    	 if((fpin=fopen("in.txt","r"))==NULL){
    	 	 printf("File Open  Error!");
    		 exit(0); 
    	 } 
    	 
    	 StuList head;
    	 Student *p;
    	 while(n--){
    	 	p=(Student*)malloc(sizeof(Student));
    		fscanf(fpin,"%d%s%d",&(p->num),&(p->name),&(p->grade));
    		p->next=head; //尾插法 
    		head=p; 
    	 } 
    	 
    	 fclose(fpin);
    	 return head; 
    } 
    
    //递归的Merge 
    StuList Merge(Student *pHead1,Student *pHead2){
    	  if(pHead1==NULL){
    	  	return pHead2; 
    	  } 
    	  if(pHead2==NULL){
    	  	return pHead1; 
    	  }
    	  Student*  head=(Student*)malloc(sizeof(Student));
    
    	  if(pHead1->grade<pHead2->grade){
    	  	   head=pHead1;
    		   head->next=Merge(pHead1->next,pHead2); 
    	  }else{
    	  	   head=pHead2;
    		   head->next=Merge(pHead1,pHead2->next);  
    	  }  
    	  return head; 
    } 
    
     
    int main() {
    	   int n;//学生个数 
    	   printf("请输入学生个数: "); 
    	   scanf("%d",&n); 
    	   StuList list1=CreateList(n);
    	   PrintList(list1); 
    	   BubbleSort(list1);
    	   printf("请输入学生个数: "); 
    	   scanf("%d",&n); 
    	   StuList list2=CreateList(n); 
    	   PrintList(list2); 
    	   BubbleSort(list2);//冒泡排序 
    	   printf("Merge两个链表\n"); 
           StuList list=(Student*)malloc(sizeof(Student));
    	   list->next=Merge(list1->next,list2->next); 
           PrintList(list); 
      
    	   
    	  
    	 
    	 
    	
    	  
    	
         
        
    
    
    	return 0;
    }
    
    • 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
  • 相关阅读:
    杂多酸离子液体[BMIM]3 PW12O40负载三乙烯四胺(TETA)功能化Fe3O4复合材料([BMIM]3 PW12O40/Fe3O4@TETA)
    Cadence Allegro PCB设计88问解析(十一) 之 Allegro中文件自动保存时间设置
    【黑马头条】-day11热点文章实时计算-kafka-kafkaStream-Redis
    计算机毕设(附源码)JAVA-SSM加油站安全事故案例库管理系统
    Mybatis 返回值配置理解 - 返回值是浮点数 BigDecimal 或整数 Integer的配置 - 返回指定实体类格式的 List 数组
    基于JavaWeb+SSM+Vue微信小程序校园兼职任务平台系统的设计和实现
    iOS封装FrameWork
    在原生APP中集成Unity容器
    Elasticsearch 深入理解search After 处理深度分页问题
    前端时间格式传入后端负载里面没有东西
  • 原文地址:https://blog.csdn.net/weixin_46503238/article/details/126532741