C语言编程题

11、编写子函数:(1)用冒泡法将一个数组排成升序的函数---SUB1;(2)在升序数组中插入一个数,并且保持该数组仍为升序数组的函数---SUB2。
主函数:①输入任意10个正整数给数组;②调用SUB1对数组进行排序;③从键盘输入一个正整数,调用SUB2将其插入该数组。
#include<conio.h>
#include<stdio.h>
void main()
{
int i,k,a[12]={0}; //a[0] for no use
void sub1(int b[]),sub2(int b[],int k);
clrscr();
printf("Please input 10 numbers:");
for(i=1;i<=10;i++)
scanf("%d",&a[i]);
getchar();
sub1(a);
for(i=1;i<=10;i++)
printf("\na[%d]=%d\n",i,a[i]);
printf("\n\nplease input a number to be inserted into the array:");
scanf("%d",&k);
sub2(a,k);
for(i=1;i<=11;i++)
printf("\na[%d]=%d\n",i,a[i]);
puts("\nAny key to exit!");
getch();
}
void sub1(b)
int b[];
{
int i,j,t;
for (i=1;i<10;i++) //the first one is always the smallest
for(j=i;j<=10;j++)
if (b[i]>b[j])
{
t=b[i];
b[i]=b[j];
b[j]=t;
}
}
void sub2(int b[],int k)
{
int i;
for(i=10;i>=1;i--)
{
if(k<b[i])
b[i+1]=b[i];
else
{
b[i+1]=k;
break;
}
}
}

12、编写函数:(1)用选择法将数组排成降序的函数----SUB1;(2)用折半查找法查找某数是否在给定的数组当中的函数----SUB2。
主函数:输入任意10个正整数给数组,调用SUB1对数组进行排序,从键盘输入一个正整数,调用SUB2在数组中进行查找,找到后输出“OK”,没有找到则输出“NO FOUND!”。
#include<conio.h>
#include<stdio.h>
void main()
{
int i,key,a[11]={0},sub1(),sub2();
printf("please input 10 number: ");
for(i=1;i<=10;i++)
scanf("%d",&a[i]);
getchar();
sub1(a);
for(i=0;i<=10;i++)
printf("a[%d]=%d ,",i,a[i]);
printf("\n please input a key number: ");
scanf("%d",&key);
sub2(a,key,1,10);
getch();
}
int sub1(int b[])
{
int t,i,j,post;
for (i=1;i<10;i++)
{
post=i;
for(j=i+1;j<=10;j++)
if (b[post]>b[j])
post=j;
if(post!=i)
{
t=b[i];
b[i]=b[post];
b[post]=t;}
}
return 0;
}
int sub2(int c[],int k,int n0,int n1)
{
int i=n0,j=n1,m;
m=(i+j)/2;
while(i<=j)
{
if(k<c[m])
j=m-1;
if(k>c[m])
i=m+1;
if(k==c[m])
break;
m=(i+j)/2;
}
if(k==c[m])
printf("OK!\n") ;
else
printf("NO FOUND!\n");
return 0;
}

13、编写一个程序,输入两个包含5个元素的数组,先将两个数组升序排列,然后将这两个数组合并成一个升序数组。
#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,k,a[6]={0},b[6]={0},c[11]={0},sub1();

clrscr();
printf("\nplease input 5 int numbers to array1: ");
for(i=1;i<=5;i++) //a[0] for no use
scanf("%d",&a[i]);
getchar();
sub1(a,5);

printf("\nplease input 5 int numbers to array2: ");
for(i=1;i<=5;i++) //b[0] for no use
scanf("%d",&b[i]);
getchar();
sub1(b,5);

printf("\nthe sorted array a is:\n\n");
for(i=1;i<=5;i++)
printf("a[%d]=%d ",i,a[i]);
printf("\n");

printf("\nthe sorted array b is:\n\n");
for(i=1;i<=5;i++)
printf("b[%d]=%d ",i,b[i]);

k=i=j=1;
while(i<=5&&j<=5)
if(a[i]<b[j])
c[k++]=a[i++]; //c[0] for no use
else
c[k++]=b[j++];
if(i<j) //appending the rest ones in array a
for(;i<=5;i++)
c[k++]=a[i];
else //appending the rest ones in array b
for(;j<=5;j++)
c[k++]=b[j];
printf("\n\n");

printf("\nthe merged array c is:\n\n");
for(k=1;k<=10;k++)
{
if(k==6)
printf("\n");
printf("c[%d]=%d ",k,c[k]);
}
while(!kbhit());
}

int sub1(int b[],int n)
{
int t,i,j,post;
for(i=1;i<n;i++)
{
post=i;
for(j=i+1;j<=n;j++)
if(b[post]>b[j])
post=j;
if(post!=i)
{
t=b[i];
b[i]=b[post];
b[post]=t;
}
}
return 0;
}

14、耶稣有13个门徒,其中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个开始报号:1,2,3,1,2,3……,凡是报到“3”就退出圈子,最后留在圈内的人就是出卖耶稣的叛徒,请找出它原来的序号。
/*
// approach one
#define N 13

#include<stdio.h>
#include<conio.h>

struct person
{
int number; //its order in the original circle
int nextp; //record its next person
};
struct person link[N+1]; //link[0] for no use

void main()
{
int i,count,next; //count for 12 persons,and
//next for the person not out of circle yet
clrscr();

for (i=1;i<=N;i++)
{
link[i].number=i; //numbering each person
if(i==N)
link[i].nextp=1;
else
link[i].nextp=i+1; //numbering each next person
}

printf("\nThe sequence out of the circle is:\n");
for(next=1,count=1;count<N;count++) //count until 12 persons
{
i=1;
while (i!=3) //i counts 1,2,3
{
do //skip the ones whose numbers are zero
next=link[next].nextp;
while(link[next].number==0); //end of do
i++;
}
printf("%3d ",link[next].number);
link[next].number=0; //indicate out of circle already
do //start from the ones whose numbers are not zero next time
next=link[next].nextp;
while(link[next].number==0);
}

printf("\n\nThe betrayer of them is:");
for(i=1;i<=N;i++)
if(link[i].number)
printf("%3d\n",link[i].number);
getch();
}
*/

// approach two using cyclic list
#define N 13
#define LEN sizeof(struct person)

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>

// struct person //permit struct placed here//
// {
// int number;
// struct person *next;
// };

void main()
{
int i,count;
struct person //or permit struct placed here also//
{
int number;
struct person *next;
};
struct person *head,*p1,*p2;

clrscr();

head=p2=NULL;
for(i=1;i<=N;i++)
{
p1=(struct person *)malloc(LEN);
p1->number=i;
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
}
p2->next=head;

printf("\nthe sequence out of the circle is:\n");
for (count=1;count<N;count++)
{
i=1;
while(i!=3)
{
p1=head;
head=head->next;
i++;
}
p2=head;
printf("%3d ",p2->number);
p1->next=head=p2->next;
free(p2);
}

printf("\nThe betrayer of them is:\n%3d",head->number);
getch();
}


15、编写一个程序,根据用户输入的不同边长,输出其菱形。例如,边长为3的菱形为:

16、按如下图形打印杨辉三角形的前10行。其特点是两个腰上的数都为1,其它位置上的每一个数是它上一行相邻两个整数之和。

#include<stdio.h>
#include<conio.h>
#define N 10

void main()
{
int i,j,k,a[N][N];

clrscr();
for(i=0;i<N;i++) //initialize a[N][N]
{
a[i][0]=1;
a[i][i]=1;
}

for(i=2;i<N;i++) //calculate
for(j=1;j<i;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];

for(i=0;i<N;i++) //output
{
for(k=0;k<=3*(N-i);k++)
printf(" ");
for(j=0;j<=i;j++)
printf("%6d",a[i][j]);
printf("\n\n");
}
getch();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值