1.递推法和动态分配数组内存结合求Fibonacci数列第n项
要求n由用户输出
代码:
#include <stdio.h>
#include <stdlib.h>
unsigned fun(int n);//Fibonacci数列都是正值,所以用unsigned类型保存Fibonacci项
int main()
{
int n;
printf("请输入项数:");
scanf("%d",&n);
printf("Fibonacci数列第%d项值是%u\n",n,fun(n));
return 0;
}
unsigned fun(int n)
{
unsigned *a,i;
a=malloc(n*sizeof(int));//分配内存
if(a==NULL) return -1;
a[1]=1;a[2]=1;
for(i=3;i<=n;i++)
a[i]=a[i-1]+a[i-2];
i=a[n];//将数组最后元素值保存到变量i中
free(a);//释放数组分配的内存
return i;
}
运行:
注意: unsigned类型的最大值也只有4294967295(2的23次方-1),所以最多也只能求到80项。
2.读入若干同学信息,根据成绩从高到低排序
代码:
#include <stdio.h>
#include <stdlib.h>
struct student
{
int num;//学号
char name[20];//姓名
float score;//成绩
};
void read(struct student *p,int n);//读入数据
void sort(struct student *p,int n);//排序数据
void write(struct student *p,int n);//输出处理结果
void swap(struct student *p1,struct student *p2);//数据交换
int main()
{
struct student *p;
int n;
printf("请输入学生数量:");
scanf("%d",&n);
p=malloc(n*sizeof(struct student));
read(p,n);
sort(p,n);
write(p,n);
free(p);
return 0;
}
void read(struct student *p,int n)
{
int i;
puts("请输入同学的学号、姓名、成绩:");
for(i=0;i<n;i++){
scanf("%d%s%f",&p[i].num,&p[i].name,&p[i].score);
}
}
void sort(struct student *p,int n)
{
int i,j;
for(i=0;i<n-1;i++){//冒泡排序
for(j=0;j<n-i-1;j++)
if(p[j].score<p[j+1].score)
swap(&p[j],&p[j+1]);//等价于swap(p+j,p+j+1)
}
}
void write(struct student *p,int n)
{
int i;
for(i=0;i<n;i++)
printf("学号:%d,姓名:%s,成绩:%.1f\n",p[i].num,p[i].name,p[i].score);
}
void swap(struct student *p1,struct student *p2)
{
struct student t;
t=*p1;
*p1=*p2;
*p2=t;
}
运行:
有一个小遗憾是read函数没有实现全部的输入功能,如果把用户数量的读入也放到read函数中,程序的功能划分就更合理了。
为了把用户数量的读入和学生信息的读入都放在read函数中,read函数就需要完成学生数组内存分配问题,如果在被调函数中进行内存分配,并且把分配的值返回到主调函数中,就要使用到二级指针。下面给出修改后的read函数和mai函数。
代码:
#include <stdio.h>
#include <stdlib.h>
struct student
{
int num;//学号
char name[20];//姓名
float score;//成绩
};
void read(struct student **p,int *n);//读入数据
void sort(struct student *p,int n);//排序数据
void write(struct student *p,int n);//输出处理结果
void swap(struct student *p1,struct student *p2);//数据交换
int main()
{
struct student *p;
int n;
read(&p,&n);
sort(p,n);
write(p,n);
free(p);
return 0;
}
void read(struct student **p,int *n)
{
int i;
printf("请输入学生数量:");
scanf("%d",n);
*p=malloc(*n*sizeof(struct student));
puts("请输入同学的学号、姓名、成绩:");
for(i=0;i<*n;i++){
scanf("%d%s%f",&(*p)[i].num,&(*p)[i].name,&(*p)[i].score);
}
}
void sort(struct student *p,int n)
{
int i,j;
for(i=0;i<n-1;i++){//冒泡排序
for(j=0;j<n-i-1;j++)
if(p[j].score<p[j+1].score)
swap(&p[j],&p[j+1]);//等价于swap(p+j,p+j+1)
}
}
void write(struct student *p,int n)
{
int i;
for(i=0;i<n;i++)
printf("学号:%d,姓名:%s,成绩:%.1f\n",p[i].num,p[i].name,p[i].score);
}
void swap(struct student *p1,struct student *p2)
{
struct student t;
t=*p1;
*p1=*p2;
*p2=t;
}
设有3个候选人,共十张选票,每次输入一个得票的候选人的名字,要求输出个人得票的结果。
代码:
#include<stdio.h>
#include<string.h>
#define N 10 //共有十张选票
struct person//定义一个结构体类型用于存放候选人及票数
{
char name[20];
int count;
}leader[3]={{"Li",0},{"Wang",0},{"Ma",0}};//定义结构体类型数组
void main()
{
int i,j;
char leader_name[20];
for(i=0;i<N;i++)
{
scanf("%s",&leader_name);
for(j=0;j<3;j++)
if(strcmp(leader_name,leader[j].name)==0)//寻找与选票姓名一致的候选人
leader[j].count++;
}
for(j=0;j<3;j++)
printf("%s:%d\n",leader[j].name,leader[j].count);
}
运行:
4.编写一些字符串复制函数
代码:
#include<stdio.h>
void copy_string1(char *a,char *b)
{
int i=0;
while(b[i]!='\0')
{
a[i]=b[i];
i++;
}
a[i]='\0';
}
void copy_string2(char *a,char *b)
{
for(;*b!='\0';a++,b++)
*a=*b;
*a='\0';
}
void copy_string3(char *a,char *b)
{
while((*a=*b)!='\0')
{
a++;b++;
}
}
void copy_string4(char *a,char *b)
{
while(*b!='\0')
*a++=*b++;
*a='\0';
}
void copy_string5(char *a,char *b)
{
while(*b)
*a++=*b++;
*a='\0';
}
void copy_string6(char *a,char *b)
{
while(*a++=*b++);
}
void copy_string7(char *a,char *b)
{
while((*a++=*b++)!='\0');
}
void main()
{
char a[]="Hi!Boy!";
char b[]="Welcome to China!";
copy_string7(a,b);
printf("copy string b to a:\nstring a=%s\nstring b=%s\n",a,b);
}
运行: