C基础(21——25)_C/C++

C基础(21——25)_C/C++_02

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

结果:

C基础(21——25)_C/C++_03


C基础(21——25)_C/C++_04

#include <stdio.h>
#include <stdlib.h>

#define N 5
void 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;
}

结果:

C基础(21——25)_C/C++_05


C基础(21——25)_C/C++_06

//5个数冒泡4次
#include <stdio.h>
#include <stdlib.h>
#define N 8
void 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;
}

结果:

C基础(21——25)_C_07


C基础(21——25)_C_08

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

结果:

C基础(21——25)_C/C++_09


C基础(21——25)_C/C++_10

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

结果:

C基础(21——25)_C_11