寒假作业3

一.杨辉三角形

设计过程:

规律:1.各行第一个数都是1

      2.各行最后一个数都是1

     3.从第三行起,除第一个数和最后一个数外,其余各数是上一行同列和前一列两个数之和;

代码:

#include <stdio.h>

#define N 100

main()

{

int a[N][N];

int n=0,i=0,j=0,k=0;

printf("please input n:");

scanf("%d",&n);

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

{

a[i][0]=1;

a[i][i]=1;

if(i>0)

{

for(j=1;j<i;j++)

{

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

}

}

}

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

{

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

{

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

}

printf("\n");

}

getch();

}

结果:

 

二.奇数阶幻方

设计过程:

  上网找到相关的算法过程,然后编写相应代码

   最经典的填法是罗伯特法(楼梯法),填写方法是这样: 

   把1(或最小的数)放在第一行正中;按以下规律排列剩下的n×n-1个数:  

   (1)每一个数放在前一个数的右上一格; 

   (2)如果这个数所要放的格已经超出了顶行那么就把它放在底行,仍然要放在右一列; 

   (3)如果这个数所要放的格已经超出了最右列那么就把它放在最左列,仍然要放在上一行; 

   (4)如果这个数所要放的格已经超出了顶行且超出了最右列,那么就把它放在前一个数的下一行同一列的格内; 

   (5)如果这个数所要放的格已经有数填入,处理方法同(4)

代码:

#include <stdio.h>

#define N 20

main()

{

int n=0,i=0,j=0,k=0,t=1;

int a[N][N];

printf("please input n:");

scanf("%d",&n);

if(n>0 && n%2!=0 && n<N)

{

for (k=1;k<=n;k++)

{

for (j=1;j<=n;j++)

{

a[k][j]=0;

}

}

j=n/2+1;

k=1;

a[k][j]=1;

for(i=2;i<=n*n;i++)

{  

k=k-1;

j=j+1;

if(k<1 && j>n)

{

k=k+2;

j=j-1;

}

else

{

if(k<1) {k=n;}

if(j>n) {j=1;}

}

if(a[k][j]==0) 

{a[k][j]=i;}

else

{

k=k+2;

j=j-1;

a[k][j]=i;

}

}

for(k=1;k<=n;k++)

{

for(j=1;j<=n;j++)

{

printf("%4d",a[k][j]);

}

printf("\n");

}

}

}

结果:

 

三.4n阶幻方的一般构造方法

设计过程:

上网找到相关的算法过程,然后编写相应代码

以四阶幻方为例加以说明:4×4正方形的对角线,按下述方法填入各数:

(1)    从第一行起,按从左到右的次序依次填入,只在没有被(当然也可以是全被)对角线割开的方格里记上该方格的位置序号.

(2)    再从最后一行起,按从右向左,自下而上的的顺序依次在剩下的方格里填入该位置方格的位置序号.

在构作8,12,16,…幻方时,只要先把它分成4×4方块,再画出所有4×4正方形的对角线,然后用上述方法填入各数即可

代码:

#include <stdio.h>

#define N 20

main()

{

int n=0,i=0,j=0,k=0,t=0,m=0,q=0;

int a[N][N],b[N*N];

printf("please input n:");

scanf("%d",&n);

if(n%4==0)

{

k=1,j=0,t=1;

m=n/4;

for(i=1;i<=n*n;i++)

{

j++;

if(j==k || j==n-k+1)

{

a[k][j]=0;

b[t]=i;

t++;

}

else

{

if(m>1)

{

for(q=1;q<m;q++)

{

    if(j==n-k+1-(4*q) || j==k-(4*q) || j==n-k+1+(4*q) || j==k+(4*q))

{

a[k][j]=0;

    b[t]=i;

    t++;

}  

}

if(a[k][j]!=0)

{

a[k][j]=i;

}

}

else

{

a[k][j]=i;

}

}

if(j==n) 

{

k=k+1;

j=0;

}

}

i=1;

for(k=n;k>0;k--)

{

for(j=n;j>0;j--)

{

if(a[k][j]==0)

{

a[k][j]=b[i];

i++;

}

if(i>=t)

{

break;

}

}

}

for(k=1;k<=n;k++)

{

for(j=1;j<=n;j++)

{

printf("%4d",a[k][j]);

}

printf("\n");

}

}

getch();

}

结果:

 

四.n个人围成一圈,顺序排序。从第一个人开始报数(从13报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位(用指针)

设计过程:

1)定义一个一维数组a[N],再定义一个指针变量p,输入人数n

2)将指针p指向数组开头p=a

3)给数组元素按1~n顺序排序;

4)按游戏规则,从第一个人开始从13报数,报到3的人出局(*p=0);出局人数m加一,当到最后一个人后又回到第一个人,直到最后只剩下一个人时,游戏结束;

5)找到那唯一一个没有出局(*p!=0)的人的序号;

代码:

#include <stdio.h>

#define N 100

main()

{

int i=0,k=0,n=0,m=0,*p;

int a[N];

printf("please input n:");

scanf("%d",&n);

p=a;

for(i=1;i<=n;i++)

{

*p=i;

p++;

}

p=a;

i=0;

while(m<n-1)

{

if(*p>0) 

{

k++;

}

if(k==3)

{

*p=0;

      k=0;

    m++;

}

    p++;

    i++;

if(i==n)

{

p=a;

i=0;

}

}

p=a;

while(*p==0)

{

p++;

}

printf("The last one is NO.%d\n",*p);

getch();

}

结果:

 

五.将一个5*5的矩阵中最大的元素放在中心,4个角分别放4个最小的元素(顺序为存放),写一个函数实现。用main函数调用。

设计过程:

1)定义一个a[5][5]的二维数组,并输入相应值

2)定义一个整型指针变量,并将指针指向二维数组开头

3)写一个函数,在函数内定义整型指针变量*pmin*pmax,找到最大值放到中心,在依次找到4个最小的数,从左到右,从上到下依次从小到大放到4个角

代码:

#include <stdio.h>

#define N 5

void change(int* p);

main()

{

int i=0,j=0;

int a[N][N];

int* p;

printf("please input a[%d][%d]:\n",N,N);

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

{

for (j=0;j<N;j++)

{

scanf("%d",&a[i][j]);

}

}

p=a;

change(p);

printf("after change:\n");

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

{

for (j=0;j<N;j++)

{

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

}

printf("\n");

}

fflush(stdin);

getch();

}

 

void change(int *p)

{

int tmp=0,i=0,j=0;

int *pmin,*pmax;

pmin=p;

pmax=p;

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

{

for(j=0;j<N;j++)

{

if(*pmax<*(p+i*N+j)) pmax=(p+i*N+j);

if(*pmin>*(p+i*N+j)) pmin=(p+i*N+j);

}

}

tmp=*(p+(N*N-1)/2);

*(p+(N*N-1)/2)=*pmax;

*pmax=tmp;

 

tmp=*p;

*p=*pmin;

*pmin=tmp;

 

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

{

for (j=0;j<N;j++)

{

if(*pmin>*(p+i*N+j) && *(p+i*N+j)>*p) pmin=(p+i*N+j);

}

}

tmp=*(p+N-1);

*(p+N-1)=*pmin;

*pmin=tmp;

 

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

{

for (j=0;j<N;j++)

{

if(*pmin>*(p+i*N+j) && *(p+i*N+j)>*(p+N-1)) pmin=(p+i*N+j);

}

}

tmp=*(p+(N-1)*N);

*(p+(N-1)*N)=*pmin;

*pmin=tmp;

 

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

{

for (j=0;j<N;j++)

{

if(*pmin>*(p+i*N+j) && *(p+i*N+j)>*(p+(N-1)*N)) pmin=(p+i*N+j);

}

}

tmp=*(p+N*N-1);

*(p+N*N-1)=*pmin;

*pmin=tmp;

}

结果:

 

六.输入年、月、日,计算该日在本年中是第几天,注意闰年问题。

设计过程:

1)定义一个结构体变量(包括年、月、日);

2)写一个函数,从主函数中获取年月日,计算出天数,再将天数返回到主函数;

代码:

#include <stdio.h>

struct date

{

int year;

int month;

int day;

};

int days(struct date adate);

main()

{

int n=0,i=1;

struct date stud;

printf("please input year month day:");

    scanf("%d %d %d",&stud.year,&stud.month,&stud.day);

n=days(stud);

if(n>0)

{

printf("%d/%d is the %dth day in %d\n",stud.month,stud.day,n,stud.year);

}

fflush(stdin);

getch();

}

 

int days(struct date adate)

{

int n;

switch(adate.month)

{

case 1:

n=adate.day;

break;

case 2:

n=adate.day+31;

break;

case 3:

n=adate.day+59;

break;

case 4:

n=adate.day+90;

break;

case 5:

n=adate.day+120;

break;

case 6:

n=adate.day+151;

break;

case 7:

n=adate.day+181;

break;

case 8:

n=adate.day+212;

break;

case 9:

n=adate.day+243;

break;

case 10:

n=adate.day+273;

break;

case 11:

n=adate.day+304;

break;

case 12:

n=adate.day+334;

break;

default:

printf("error!\n");

n=-1;

break;

}

if ((adate.year%4==0 && adate.year%100!=0 || adate.year%400==0)&&(adate.month>2))

{

n+=1;

}

return n;

}

结果:

 

七.用牛顿迭代法求方程的根,系数abcd的值依次为1,2,3,4,求x1附近的一个实根。

设计过程:

1)牛顿迭代公式为,是上一次求出的近似根,有题可知开始时=1

2)写一个函数将上一次迭代后的x作为带入迭代公式中,求出x的下一个近似值,直到迭代到时结束;

代码:

#include <stdio.h>

#include <math.h>

double calculate(double a,double b,double c,double d,double x0);

main()

{

double a=0,b=0,c=0,d=0,x=0,x0=0;

printf("please input a,b,c,d:");

scanf("%lf,%lf,%lf,%lf",&a,&b,&c,&d);

printf("please input x0:");

scanf("%lf",&x0);

x=calculate(a,b,c,d,x0);

printf("x=%lf\n",x);

fflush(stdin);

getch();

}

 

double calculate(double a,double b,double c,double d,double x0)

{

double x=0,f=0,f1=0;

x=x0;

do

{

x0=x;

f=((a*x0+b)*x0+c)*x0+d;

f1=(3*a*x0+2*b)*x0+c;

x=x0-f/f1;

}while (fabs(x-x0)>=0.001);

return x;

}

结果:

 

八.用指向指针的指针的方法对n个整数排序并输出

设计过程:

1)定义一个一维数组(a[N]),一个整型指针数组(*b[N]),一个指针变量(**p);

2)输入要进行排序的数的个数n,并依次输入n个数保存的数组的地址中;

3)写一个函数对这些数进行比较、排序(用冒泡法);

代码:

#include <stdio.h>

#define N 100

void sort (int n,int **p);

main()

{

int n=0,i=0;

int a[N];

int **p,*b[N];

printf("please input n:");

scanf("%d",&n);

printf("please input %d num:",n);

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

{

b[i]=&a[i];

scanf("%d",b[i]);

}

p=b;

sort(n,p);

printf("now:");

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

{

printf("%4d",*b[i]);

}

printf("\n");

getch();

}

 

void sort(int n,int **p)

{

int i=0,j=0,*tmp;

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

{

for (j=i+1;j<n;j++)

{

if(**(p+i)>**(p+j))

{

tmp=*(p+i);

*(p+i)=*(p+j);

*(p+j)=tmp;

}

}

}

}

结果:

 

九.写一个用矩形法求定积分的通用函数,分别求

设计过程:

1)求定积分的通用函数integral有三个形参:下限a,上限b以及指向函数的指针变量fun

可写为:float integral(float a,float b,float (*fun)(float));

2)分别定义三个函数fsin,fcos,fexp;

3)先后调用integral函数三次,并将上、下限以及相应的函数作为实参;

代码:

#include <stdio.h>

#include <math.h>

#define N 20

float integral (float a,float b,float (*fun)(float));

float fsin(float x);

float fcos(float x);

float fexp(float x);

main()

{

float a1,a2,a3,b1,b2,b3,c;

float (*p)(float);

printf("please input a1 b1:");

scanf("%f,%f",&a1,&b1);

p=fsin;

c=integral(a1,b1,p);

printf("the integral of sin(x) is:%f\n",c);

getch();

printf("please input a2 b2:");

scanf("%f,%f",&a2,&b2);

p=fcos;

c=integral(a2,b2,p);

printf("the integral of cos(x) is:%f\n",c);

getch();

printf("please input a3,b3:");

scanf("%f,%f",&a3,&b3);

p=fexp;

c=integral(a3,b3,p);

printf("the integral of exp(x) is:%f\n",c);

getch();

}

float integral (float a,float b,float (*fun)(float))

{

int i=0,n=0;

float x=0,h=0,s=0;

n=N;

h=(b-a)/n;

x=a;

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

{

x=x+h;

s+=(*fun)(x)*h;

}

return s;

}

float fsin(float x)

{

return sin(x);

}

float fcos(float x)

{

return cos(x);

}

float fexp(float x)

{

return exp(x);

}

结果:

 

十.写一个程序实现链表的建立、输出、删除和插入

代码:

#include <stdio.h>

#include <stdlib.h>

#define LEN sizeof(struct student)

struct student

{

long num;

float score;

struct student *next;

};

int n;

struct student *creat(void);

struct student *del(struct student *head,long num);

struct student *insert(struct student *head,struct student *stu);

void print(struct student *head);

 

main()

{

struct student *head,*stu;

long num;

int i=1;

while (i>0)

{

system("cls");

    printf("=============================\n");

    printf(" 1.建立链表               ...\n");

    printf(" 2.删除结点               ...\n");

    printf(" 3.插入结点               ...\n");

    printf("=============================\n");

printf("请选择:");

scanf("%d",&i);

switch(i)

{

case 1:

printf("please input recoreds:\n");

head=creat();

print(head);

getch();

break;

case 2:

printf("\nplease input the deleted number:");

scanf("%ld",&num);

while(num!=0)

{

head=del(head,num);

print(head);

printf("\nplease input the deleted number:");

scanf("%ld",&num);

}

getch();

break;

case 3:

printf("\nplease input the inserted record:\n");

stu=(struct student*)malloc(LEN);

scanf("%ld,%f",&stu->num,&stu->score);

while(stu->num!=0)

{

head=insert(head,stu);

print(head);

printf("\nplease input the inserted record:\n");

scanf("%ld,%f",&stu->num,&stu->score);

}

getch();

break;

}

}

}

 

struct student *creat() //建立链表

{

struct student *head;

struct student *p1,*p2;

n=0;

p1=p2=(struct student *)malloc(LEN);

scanf("%ld,%f",&p1->num,&p1->score);

head=NULL;

while(p1->num!=0)

{

n+=1;

if(n==1) head=p1;

else p2->next=p1;

p2=p1;

p1=(struct student *)malloc(LEN);

scanf("%ld,%f",&p1->num,&p1->score);

}

p2->next=NULL;

return(head);

}

 

struct student *del(struct student *head,long num) //删除结点

{

struct student *p1,*p2;

if(head==NULL)

{

printf("\nlist null!\n");

return(head);

}

p1=head;

while(num!=p1->num && p1->next!=NULL)

{

p2=p1;

p1=p1->next;

}

if(num==p1->num)

{

if(p1==head) head=p1->next;

else p2->next=p1->next;

printf("delete:%ld\n",num);

n=n-1;

}

else

{

printf("%ld not been found!\n",num);

}

return(head);

}

 

struct student *insert(struct student *head,struct student *stu) //插入结点

{

struct student *p0,*p1,*p2;

p1=head;

p0=stu;

if(head==NULL)

{

head=p0;

p0->next=NULL;

}

else

{

while((p0->num>p1->num)&&(p1->next!=NULL))

{

p2=p1;

p1=p1->next;

}

if(p0->num<=p1->num)

{

if(head==p1)

{

head=p0;

}

else

{

p2->next=p0;

p0->next=p1;

}

}

else

{

p1->next=p0;

p0->next=NULL;

}

}

n+=1;

return head;

}

 

void print(struct student *head)

{

struct student *p;

printf("\nthese %d records are:\n",n);

p=head;

if(head!=NULL)

{

do 

{

printf("%ld %5.2f\n",p->num,p->score);

p=p->next;

}while(p!=NULL);

}

}

结果:

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值