C基础(26——30)

wKioL1ecY1vSj4iSAAApgPi_vqs982.png

如果n越大,则递归计算比较慢
//#include <stdio.h>
//#include <stdlib.h>
//
//int FibNoRecursion(int n)  //斐波那契非递归
//{
//             int a=0;
//             int b=1;
//             int c=a+b;
//
//             if(n==0)
//                             return 0;
//             if(n==1)
//                             return b;
//             if(n==2)
//                             return a+b;
//             for(int i=3;i<=n;i++)
//             {
//                             a=b;
//                             b=c;
//                             c=a+b;
//             }
//             return c;
//}
//void test()
//{
//             int n=0;
//             printf("Please input n: ");
//             scanf("%d",&n);
//             int ret=FibNoRecursion(n);
//             printf("Fibonacci(%d) = %d\n",n,ret);
//}
//int main()
//{
//             test();
//             system("pause");
//             return 0;
//}


//数组实现
#include <stdio.h>
#include <stdlib.h>

int FibNoRecursion(int n)  //斐波那契非递归
{
                 int fib_arr[100]={0};    //数组元素全部初始化为0
                fib_arr[0]=1;
                fib_arr[1]=1;
                 int count=0;

                 while(count<n-1)
                {
                                fib_arr[count+2]=fib_arr[count+1]+fib_arr[count];
                                count++;
                }
                 return fib_arr[n-1];
}
void test()
{
                 int n=0;
                printf( "Please input n: ");
                scanf( "%d",&n);
                 int ret=FibNoRecursion(n);
                printf( "Fibonacci(%d) = %d\n",n,ret);
}
int main()
{
                test();
                system( "pause");
                 return 0;
}

结果:

wKioL1ecY5WhlV-QAAAL5ZPdT-0318.png



wKioL1ecY6-hvlisAAAyKEXfiwk720.png

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

void test()
{
                 char* str="abcdefghi123" ;
                 int count=0;

                 while(*str)
                {
                                count++;
                                str++;
                }

                printf( "%d\n",count);
}
int main()
{
                test();
                system( "pause");
                 return 0;
}

结果:

wKiom1ecY9TDw60wAAAMPmTa9dk683.png



wKioL1ecY_Di7ewFAAAoI6cH1u8491.png

//不用临时变量,即可以用递归来求

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

int Strlen(char * str)
{
                 if(*str =='\0')
                                 return 0;
                 if(*str )
                {
                                 str++;
                                 return Strlen(str )+1;
                }
}
void test()
{
                 char* str="0abc123def" ;
                 int ret=Strlen(str);

                printf( "strlen=%d\n",ret);
}
int main()
{
                test();
                system( "pause");
                 return 0;
}

结果:

wKioL1ecZBSTsi_HAAATPXXs1Gk734.png



wKiom1ecZDPysJG7AAAnDZv-RWU401.png

#include <stdio.h>
#include <stdlib.h>
#define N 8
void test()
{
                 int arr[N ]={23,88,12,8,37,99,25,0};

                 for(int i=0;i<N-1;i++)
                {
                                 int j=i+1;

                                 for(j;j<N ;j++)
                                {
                                                 if(arr[i]>arr[j])
                                                {
                                                                 int tmp=arr[i];
                                                                arr[i]=arr[j];
                                                                arr[j]=tmp;
                                                }
                                }
                }
                 //输出
                 for(int m=0;m<N;m++)
                {
                                printf( "%d ",arr[m]);
                }
                printf( "\n");
}
int main()
{
                test();
                system( "pause");
                 return 0;
}

结果:

wKiom1ecZFDxZAvyAAAckDkfzng129.png



wKiom1ecZbThIvDCAABSLumEAzU864.png

wKioL1ecZG2y3D1LAAAPFBjQ2A4169.png

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

void test()
{
                 int a=15;
                 int count=0;

                 while(a)
                {
                                 if(a & 1)  //按位与
                                                count++;

                                a=a>>1;
                }

                printf( "%d\n",count);
}
int main()
{
                test();
                system( "pause");
                 return 0;
}

结果:

wKiom1ecZJPxIraDAAAKExK7XAQ796.png


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值