程序设计18
- 问题18_1
- 代码18_1
- 结果18_1
 
- 问题18_2
- 代码18_2
- 结果18_2
 
- 问题18_3
- 代码18_3
- 结果18_3
 
问题18_1
        函数  
     
      
       
       
         f 
        
       
         u 
        
       
         n 
        
       
      
        fun 
       
      
    fun 的功能是:有  
     
      
       
       
         N 
        
       
         × 
        
       
         N 
        
       
      
        N\times N 
       
      
    N×N 的矩阵,根据给定的  
     
      
       
       
         m 
        
       
         ( 
        
       
         m 
        
       
         < 
        
       
         = 
        
       
         N 
        
       
         ) 
        
       
      
        m(m<=N) 
       
      
    m(m<=N) 值,将每行元素中的值均右移动  
     
      
       
       
         m 
        
       
      
        m 
       
      
    m 个位置,左位置为  
     
      
       
       
         0 
        
       
      
        0 
       
      
    0 。
         例如, 
     
      
       
       
         N 
        
       
         = 
        
       
         3 
        
       
         , 
        
       
         m 
        
       
         = 
        
       
         2 
        
       
      
        N=3,m=2 
       
      
    N=3,m=2 所有下列矩阵
  
      
       
        
         
          
          
           
            
            
              [ 
             
             
              
               
                
                
                  1 
                 
                
               
               
                
                
                  2 
                 
                
               
               
                
                
                  3 
                 
                
               
              
              
               
                
                
                  4 
                 
                
               
               
                
                
                  5 
                 
                
               
               
                
                
                  6 
                 
                
               
              
              
               
                
                
                  7 
                 
                
               
               
                
                
                  8 
                 
                
               
               
                
                
                  9 
                 
                
               
              
             
            
              ] 
             
            
           
          
          
          
         
        
       
         \begin{equation} \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5& 6 \\ 7 & 8 & 9 \end{bmatrix} \end{equation} 
        
       
      
                     147258369 
                     
         执行程序结果为
  
      
       
        
         
          
          
           
            
            
              [ 
             
             
              
               
                
                
                  0 
                 
                
               
               
                
                
                  0 
                 
                
               
               
                
                
                  1 
                 
                
               
              
              
               
                
                
                  0 
                 
                
               
               
                
                
                  0 
                 
                
               
               
                
                
                  4 
                 
                
               
              
              
               
                
                
                  0 
                 
                
               
               
                
                
                  0 
                 
                
               
               
                
                
                  7 
                 
                
               
              
             
            
              ] 
             
            
           
          
          
          
         
        
       
         \begin{equation} \begin{bmatrix} 0 & 0 &1 \\ 0 & 0& 4 \\ 0 & 0 & 7 \end{bmatrix} \end{equation} 
        
       
      
                     000000147 
                     
代码18_1
#include<stdio.h>
#define N 4
void fun(int (*t)[N], int m){
	int i, j;
	for(i=0; i<N; i++){
		for(j=N-1-m; j>=0; j--)
			t[i][j+m] = t[i][j];
		for(j=0; j<m; j++)
			t[i][j] = 0;
	}
} 
void main(void){
	int t[][N] = {21, 12, 13, 24, 
				  25, 16, 47, 38,
				  29, 11, 32, 54,
				  42, 21, 33, 10}, i, j, m;
	printf("\nThe original array:\n");
	for(i=0; i<N; i++){
		for(j=0; j<N; j++)
			printf("%4d", t[i][j]);
		printf("\n");
	}
	printf("Input m(m<=%d): ", N);
	scanf("%d", &m);
	fun(t, m);
	printf("\nThe result is:\n");
	for(i=0; i<N; i++){
		for(j=0; j<N; j++)
			printf("%4d", t[i][j]);
		printf("\n");
	}
}
结果18_1

问题18_2
        函数 
     
      
       
       
         f 
        
       
         u 
        
       
         n 
        
       
      
        fun 
       
      
    fun的功能是:计算并输出  
     
      
       
       
         h 
        
       
         i 
        
       
         g 
        
       
         h 
        
       
      
        high 
       
      
    high 以内最大  
     
      
       
       
         10 
        
       
      
        10 
       
      
    10 个素数的和。  
     
      
       
       
         h 
        
       
         i 
        
       
         g 
        
       
         h 
        
       
      
        high 
       
      
    high 的值由主函数传给  
     
      
       
       
         f 
        
       
         u 
        
       
         n 
        
       
      
        fun 
       
      
    fun 函数。
         例如,若  
     
      
       
       
         h 
        
       
         i 
        
       
         g 
        
       
         h 
        
       
         = 
        
       
         100 
        
       
      
        high=100 
       
      
    high=100,则输出的数值为  
     
      
       
       
         732 
        
       
      
        732 
       
      
    732。
代码18_2
#include<stdio.h>
#include<conio.h>
#include<math.h>
int fun(int high){
	int sum=0, n=0, j, yes;
	while((high>=2)&&(n<10)){
		yes = 1;
		for(j=2; j<=high/2; j++){
			if(high%j==0){
				yes = 0;
				break;
			}
		}
		if(yes){
			sum += high;
			n++;
		}
		high--;
	}
	return sum;
}
void main(void){
	printf("%d\n", fun(100));
}
结果18_2

问题18_3
         函数 
     
      
       
       
         f 
        
       
         u 
        
       
         n 
        
       
      
        fun 
       
      
    fun的功能是:利用下面的简单迭代方法求方程  
     
      
       
       
         c 
        
       
         o 
        
       
         s 
        
       
         ( 
        
       
         x 
        
       
         ) 
        
       
           
        
       
         − 
        
       
           
        
       
         x 
        
       
           
        
       
         = 
        
       
           
        
       
         0 
        
       
      
        cos(x)\ -\ x\ =\ 0 
       
      
    cos(x) − x = 0 的一个实根。
          
     
      
       
        
        
          x 
         
         
         
           n 
          
         
           + 
          
         
           1 
          
         
        
       
           
        
       
         = 
        
       
           
        
       
         c 
        
       
         o 
        
       
         s 
        
       
         ( 
        
        
        
          x 
         
        
          n 
         
        
       
         ) 
        
       
      
        x_{n+1}\ =\ cos(x_{n}) 
       
      
    xn+1 = cos(xn)
         迭代步骤如下:
           
     
      
       
       
         ( 
        
       
         1 
        
       
         ) 
        
       
      
        (1) 
       
      
    (1) 取  
     
      
       
        
        
          x 
         
        
          1 
         
        
       
      
        x_{1} 
       
      
    x1 初值为  
     
      
       
       
         0.0 
        
       
      
        0.0 
       
      
    0.0;
           
     
      
       
       
         ( 
        
       
         2 
        
       
         ) 
        
       
      
        (2) 
       
      
    (2)  
     
      
       
        
        
          x 
         
        
          0 
         
        
       
           
        
       
         = 
        
       
           
        
        
        
          x 
         
        
          1 
         
        
       
      
        x_{0}\ =\ x_{1} 
       
      
    x0 = x1 ,将  
     
      
       
        
        
          x 
         
        
          1 
         
        
       
      
        x_{1} 
       
      
    x1 的值赋给  
     
      
       
        
        
          x 
         
        
          0 
         
        
       
      
        x_{0} 
       
      
    x0;
           
     
      
       
       
         ( 
        
       
         3 
        
       
         ) 
        
       
      
        (3) 
       
      
    (3)  
     
      
       
        
        
          x 
         
        
          1 
         
        
       
           
        
       
         = 
        
       
           
        
       
         c 
        
       
         o 
        
       
         s 
        
       
         ( 
        
        
        
          x 
         
        
          0 
         
        
       
         ) 
        
       
      
        x_{1}\ =\ cos(x_{0}) 
       
      
    x1 = cos(x0) ,求出一个新的  
     
      
       
        
        
          x 
         
        
          1 
         
        
       
      
        x_{1} 
       
      
    x1;
           
     
      
       
       
         ( 
        
       
         4 
        
       
         ) 
        
       
      
        (4) 
       
      
    (4) 若 
     
      
       
        
        
          x 
         
        
          0 
         
        
       
         − 
        
        
        
          x 
         
        
          1 
         
        
       
      
        x_{0}-x_{1} 
       
      
    x0−x1 的绝对值小于  
     
      
       
       
         0.000001 
        
       
      
        0.000001 
       
      
    0.000001 ,执行步骤  
     
      
       
       
         ( 
        
       
         5 
        
       
         ) 
        
       
      
        (5) 
       
      
    (5) ,否则执行步骤  
     
      
       
       
         ( 
        
       
         2 
        
       
         ) 
        
       
      
        (2) 
       
      
    (2) 。
           
     
      
       
       
         ( 
        
       
         5 
        
       
         ) 
        
       
      
        (5) 
       
      
    (5)所求  
     
      
       
        
        
          x 
         
        
          1 
         
        
       
      
        x_{1} 
       
      
    x1 就是方程  
     
      
       
       
         c 
        
       
         o 
        
       
         s 
        
       
         ( 
        
       
         x 
        
       
         ) 
        
       
           
        
       
         − 
        
       
           
        
       
         x 
        
       
           
        
       
         = 
        
       
           
        
       
         0 
        
       
      
        cos(x)\ -\ x\ =\ 0 
       
      
    cos(x) − x = 0 的一个实根,作为函数值返回。
         程序输出结果为  
     
      
       
       
         R 
        
       
         o 
        
       
         o 
        
       
         t 
        
       
         = 
        
       
         0.739086 
        
       
      
        Root = 0.739086 
       
      
    Root=0.739086 。
代码18_3
#include<conio.h>
#include<math.h>
#include<conio.h>
double fun(){
	double x1=0.0, x0;
	do{
		x0 = x1;
		x1 = cos(x0);
	}while(fabs(x0-x1)>=1e-6);
	return x1;
}
void main(void){
	double f = fun();
	printf("Root = %f\n", f);
}
结果18_3






![微信小程序-获取手机号:HttpClientErrorException: 412 Precondition Failed: [no body]](https://i-blog.csdnimg.cn/direct/7cf412295b124719bec9645204fad3e1.png)













