1、用C语言打印出杨辉三角
杨辉三角形是形如:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
的三角形,其实质是二项式(a+b)的n次方展开后各项的系数排成的三角形,它的特点是左右两边全是1,从第二行起,中间的每一个数是上一行里相邻两个数之和。
程序:
#include <stdio.h>
int main()
{
int n;
int c;
int d;
printf("please input hang shu :\n");
scanf("%d",&n);
int f[100][100] = {0};
f[0][0] = 1;
f[1][0] = 1;
f[1][1] = 1;
for(c = 2;c < n; c++)
{
for(d = 0;d < (c + 1);d++)
{
if(d == 0 || d == (n - 1))
f[c][d] = 1;
else
{
f[c][d] = f[c -1][d - 1] + f[c - 1][d];
}
}
}
for(c = 0;c < n;c++)
{
for(d = 0;d < (c + 1);d++)
{
printf("%5d",f[c][d]);
}
printf("\n");
}
return 0;
}
2.用C语言实现简单的计算器(加、减、乘、除)
#include <stdio.h>int main()
{
float a;
char c;
float b;
float z;
printf("please input your suan shi:\n");
scanf("%f %c %f",&a,&c,&b);
if(c =='+')
z = a + b;
else if(c == '-')
z= a - b;
else if(c == '/')
z = a / b;
else
z = a * b;
printf("your suan shi deng yu : %f\n",z);
return 0;
}
3.利用递归方法实现一个函数,该函数能够实现n的阶乘,即 n! = n*(n-1)*…*3*2*1;
#include <stdio.h>
int main()
{
int n;
printf("please input n:\n");
scanf("%d",&n);
int sum = 1;
for(; n > 0;n--)
{
sum *= n;
}
printf("the jie cheng shu is :%d\n",sum);
return 0;
}
这题好像没用递归函数。。。。。。。
4.输入一个字符串,计算字符串中子串出现的次数
“hellosdfdshellodsfdshello”
“hello”
#include <stdio.h>
#include<string.h>
#define B 80
int main()
{
char a[B];
char c[B];
char g[B];
int d;
int i = 0;
int h ;
int sum = 0;
int e;
printf("please input a type chaun:\n");
scanf("%s",a);
printf("please input you want ji suan de zi fu shu:\n");
scanf("%s",c);
d = strlen(c);
e = strlen(a);
for(i = 0;i < (e - d); i++)
{
if(a[i] == c[0])
{
for(h = 0; h < d;h++)
{
if(a[i + h] == c[h])
sum += 1;
else
sum += 0 ;
}
}
}
sum /= d;
if(sum == 0)
printf("no you input zi fu chaun!\n");
else
printf("you input zifu chuan you %d ge !",sum);
return 0;
}
第四题自我感觉写的过于繁琐了!! 内容还没看到!!!