C基础(21——25)

来源:互联网 发布:北外网络教育多少钱 编辑:程序博客网 时间:2024/05/22 01:52

wKioL1eZn4Wg5SgcAAAjWAeUY7o992.png

wKioL1eZn6rjFYvjAAAJg08-92w189.png

#include <stdio.h>#include <stdlib.h>void test(){                 int arr[3][3]={1,2,3,4,5,6,7,8,9};                 int i=0;                 int j=0;                 int sum=0;                 for(i=0;i<3;i++)                {                                 for(j=0;j<3;j++)                                {                                                 if(i==j||(i+j==2))                                                {                                                                sum+=arr[i][j];                                                }                                }                }                printf( "sum=%d\n",sum);}int main(){                test();                system( "pause");                 return 0;}//N*N矩阵//#include <stdio.h>//#include <stdlib.h>//#define N 3 //4////void test()//{//             int arr[N][N]={1,2,3,4,5,6,7,8,9/*,10,11,12,13,14,15,16*/};//             int i=0;//             int j=0;//             int sum=0;////             for(i=0;i<N;i++)//             {//                             for(j=0;j<N;j++)//                             {//                                             if(i==j||(i+j==N-1))//                                             {//                                                             sum+=arr[i][j];//                                             }//                             }//             }////             printf("sum=%d\n",sum);//}//int main()//{//             test();//             system("pause");//             return 0;//}

结果:

wKioL1eZn96ySRoQAAANd-YfukQ802.png


wKioL1eZn_3C9RayAABAxrPt2xk745.png

#include <stdio.h>#include <stdlib.h>#define N 5void test(){                 int a[N ]={1,23,3,48,51};                 int i=0;                 for(;i<N /2;i++)                {                                 int tmp=a[i];                                a[i]=a[ N-1-i];                                a[ N-1-i]=tmp;                }                 for(int j=0;j<N;j++)                {                                printf( "%d ",a[j]);                }                printf( "\n");}int main(){                test();                system( "pause");                 return 0;}

结果:

wKiom1eZoDHTPeojAAAH7mOl49k193.png


wKiom1eZoF2jMJ8XAAAoObo211g563.png

//5个数冒泡4次#include <stdio.h>#include <stdlib.h>#define N 8void test(){                 int arr[N ];                 for(int m=0;m<N;m++) //数组初始化                {                                arr[m]=0;                }                printf( "输入%d个数:" ,N);                 for(int n=0;n<N;n++)                {                                scanf( "%d",&arr[n]);                }                 for(int i=0;i<N-1;i++)                {                                 for(int j=0;j<N-1-i;j++)                                {                                                 if(arr[j]>arr[j+1])                                                {                                                                 int tmp=arr[j];                                                                arr[j]=arr[j+1];                                                                arr[j+1]=tmp;                                                }                                }                }                 for(int k=0;k<N;k++)                {                                printf( "%d ",arr[k]);                }                printf( "\n");}int main(){                test();                system( "pause");                 return 0;}

结果:

wKioL1eZoHywjWiqAAAOODG9s2M653.png


wKioL1eZoK-R9TKqAABR0XMbFBk255.png

#include <stdio.h>#include <stdlib.h>#include <string.h>void SwapStr(char ** s1,char** s2){                 char* tmp=*s1 ;                * s1=*s2 ;                * s2=tmp;}void test(){                 char* str1="hello" ;                 char* str2="bit-tech" ;                 char* str3="world" ;                 //交换指针                 if(strcmp(str1,str2)>0)                                 SwapStr(&str1,&str2);  //str2是大的                 if(strcmp(str2,str3)>0)                                SwapStr(&str2,&str3);  //str3是最大的,str1和str2是最小的和次小的                 if(strcmp(str1,str2)>0)                                SwapStr(&str1,&str2);                printf( "%s\n%s\n%s\n",str1,str2,str3);}int main(){                test();                system( "pause");                 return 0;}

结果:

wKiom1eZoNmhGL32AAAK9X0Gi1c993.png


wKiom1eZoPPR5oUjAAAoM5F_3Ls813.png

#include <stdio.h>#include <stdlib.h>int Fibonacci(int n){                 if(n ==0)                                 return 0;                 if(n ==1)                                 return 1;                                 return Fibonacci(n -2)+Fibonacci(n-1);}void test(){                 int i=0;                printf( "Please input n: ");                scanf( "%d",&i);                 int ret=Fibonacci(i);                printf( "Fibonacci(%d) = %d\n",i,ret);}int main(){                test();                system( "pause");                 return 0;}

结果:

wKiom1eZoQzwonCEAAANaf0c16w237.png


本文出自 “追寻内心的声音” 博客,请务必保留此出处http://ljy789.blog.51cto.com/10697684/1831174

0 0