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");