C基础(21——25)

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

结果:

wKiom1eZoDHTPeojAAAH7mOl49k193.png


wKiom1eZoF2jMJ8XAAAoObo211g563.png

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

结果:

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值