C基础(26——30)

C基础(26——30)

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站制作、网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的彭阳网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

如果n越大,则递归计算比较慢
//#include 
//#include 
//
//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 
#include 

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

                 while(count

结果:

C基础(26——30)

C基础(26——30)

#include 
#include 

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

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

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

结果:

C基础(26——30)

C基础(26——30)

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

#include 
#include 

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

结果:

C基础(26——30)

C基础(26——30)

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

                 for(int i=0;iarr[j])
                                                {
                                                                 int tmp=arr[i];
                                                                arr[i]=arr[j];
                                                                arr[j]=tmp;
                                                }
                                }
                }
                 //输出
                 for(int m=0;m

结果:

C基础(26——30)

C基础(26——30)

C基础(26——30)

#include 
#include 

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

结果:

C基础(26——30)


本文标题:C基础(26——30)
文章URL:http://azwzsj.com/article/jgpeih.html