1 输入199个数字并用 函数和指针循环的方式 将这组数据从大到小排列 并输出中位数。
2写三个字符串函数,分别实现:求字符串长度,截取字符串片段,判断a串是否等于b串的子集。
求字符串长度的代码如下:
int strlen(char *p)
{
int i;
for(i=0;p[i];i++);
return i;
}
字符串复制函数的代码如下:
void strcpy(char *p1,char *p2)
{
int i;
for(i=0;p1[i];i++)
p2[i]=p1[i];
p2[i]='\0';
}
字符串比较函数会根据ASCII码依次比较p1和p2的每一个字符,直到出现不了字符,或者到达字符串末尾(’\0’)。
如果返回值<0,说明p1小于p2;
如果返回值>0,说明p1大于p2;
如果返回值=0,说明p1=p2;
3 求一个方程的实数解,x三次方 +2x平方+1=0.
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
double a, b, c, d;
double fun(double x)
{
double res = x*(x * (a * x + b) + c) + d;
return res;
}
double daofun(double x)//导数
{
double res = 3*a*x*x + 2 * b * x + c;
return res;
}
double solve(double x)//牛顿迭代
{
double x0;
double x1 = x;
do
{
x0 = x1;
x1 = x0 - fun(x0)/daofun(x0);
}while(fabs(x1 - x0) >= 1e-6);
return x1;
}
int main()
{
cin>>a>>b>>c>>d;
for(int i = -100; i <= 100; i++)
{
double left = i;
double right = i + 1;//lef和right选择长度为1的区间
if(fun (left ) == 0)
{
printf("%.2f ", left);
}
else if(fun(left ) * fun( right) < 0)
{
double jie = solve(right);//迭代的初始点从右端开始
printf("%.2f ", jie);
}
}
//system("pause");
}
综合题
用函数和指针实现 斐波那契数列
1建立一个链表 来储存该数据。
2 在链表中插入一个数 保持升序不变。
#include "stdio.h"
void fun(int n,int *p)//
{
int p1,p2;
if (n==1||n==2)
{
*p=1;
}
else
{
fun(n-1,&p1);
fun(n-2,&p2);
*p=p1+p2;
}
}
int main()//
{
int n,s;
scanf("%d",&n);
fun(n,&s);
printf("%d",s);
return 0;//
}
这是最初的可以运行,
但是要存进数组,需要改版,
#include "stdio.h"
void fun(int n,int *p)//
{
int p1,p2;
if (n==1||n==2)
{
*p=1;
}
else
{
fun(n-1,&p1);
fun(n-2,&p2);
*p=p1+p2;
}
}
int main()//
{
int n,s;
scanf("%d",&n);
int a[100];
for(int i=1;i<=n;i++)
{fun(i,&a[i]);
}
for( i=1;i<=n;i++)
printf(" %d",a[i]);
return 0;//
}
改完之后完美运行