C简单程序

6.递归求阶乘

#include<stdio.h>

long f(x)

{ long a;

   if(x==0||x==1)

    {

       a=1;

    }

   else

      {

       a=x*f(x-1);

      }

       return a;

}

main()

{

         intb;

   scanf("%d",&b);

   printf("%d",f(b));

}

 

7.输出连续数字之和

#include<stdio.h>

int main()

{  

    int i=0;

    int count=0;

    int sum=0;

 

    char str[100];

 

    scanf("%s",str);

    while(str[i]!='\0')

    {

         if(str[i]>='0'&&str[i]<='9')

         {

             sum=sum*10+str[i]-48;

         }

         else

         {

             count=count+sum;

             sum=0;

         }

         i++;

    }

    if(str[i-1]>='0'&&str[i-1]<='9')

    {

         count=count+sum;

    }

    printf("%d\n",count);

}

 

8.输出最长连续数字长度

#include<stdio.h>

int main()

{

    int i=0;

    int count=0;

    int result=0;

    char str[100];

 

    scanf("%s",str);

    while(str[i]!='\0')

    {

         if(str[i]>='0'&&str[i]<='9')

         {

         count++;

         }

         else

         {

            if(count>result)

            {

                result=count;

            }

            count=0;

        }

        i++;

    }

    if(str[i-1]>='0'&&str[i-1]<='9'//末尾判断是否为数字

    {

          if(count>result)

         {

             result=count;

         }

    }

    printf("%d\n",result);

}

9.冒泡排序

#include<stdio.h>

#define MAX_SIZE 5

main()

{

    inti;

    intj;

    inttemp;

    inta[MAX_SIZE] = { 6, 8, 4, 9, 1 };

    for(i = 4; i>0; i--)

    {

         for(j = 0; j<i; j++)         //两个for反向循环

         {

             if(a[j]>a[j + 1])

             {

                  temp= a[j + 1];

                  a[j+ 1] = a[j];

                  a[j]= temp;

             }

         }

    }

    for(i = 0; i<5; i++)

    {

         printf("%d",a[i]);

         printf("\t");

    }

    system("pause");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值