华科历年上机真题以及解答part4.docx

上传人:b****6 文档编号:15960730 上传时间:2023-07-09 格式:DOCX 页数:29 大小:21.71KB
下载 相关 举报
华科历年上机真题以及解答part4.docx_第1页
第1页 / 共29页
华科历年上机真题以及解答part4.docx_第2页
第2页 / 共29页
华科历年上机真题以及解答part4.docx_第3页
第3页 / 共29页
华科历年上机真题以及解答part4.docx_第4页
第4页 / 共29页
华科历年上机真题以及解答part4.docx_第5页
第5页 / 共29页
华科历年上机真题以及解答part4.docx_第6页
第6页 / 共29页
华科历年上机真题以及解答part4.docx_第7页
第7页 / 共29页
华科历年上机真题以及解答part4.docx_第8页
第8页 / 共29页
华科历年上机真题以及解答part4.docx_第9页
第9页 / 共29页
华科历年上机真题以及解答part4.docx_第10页
第10页 / 共29页
华科历年上机真题以及解答part4.docx_第11页
第11页 / 共29页
华科历年上机真题以及解答part4.docx_第12页
第12页 / 共29页
华科历年上机真题以及解答part4.docx_第13页
第13页 / 共29页
华科历年上机真题以及解答part4.docx_第14页
第14页 / 共29页
华科历年上机真题以及解答part4.docx_第15页
第15页 / 共29页
华科历年上机真题以及解答part4.docx_第16页
第16页 / 共29页
华科历年上机真题以及解答part4.docx_第17页
第17页 / 共29页
华科历年上机真题以及解答part4.docx_第18页
第18页 / 共29页
华科历年上机真题以及解答part4.docx_第19页
第19页 / 共29页
华科历年上机真题以及解答part4.docx_第20页
第20页 / 共29页
亲,该文档总共29页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

华科历年上机真题以及解答part4.docx

《华科历年上机真题以及解答part4.docx》由会员分享,可在线阅读,更多相关《华科历年上机真题以及解答part4.docx(29页珍藏版)》请在冰点文库上搜索。

华科历年上机真题以及解答part4.docx

华科历年上机真题以及解答part4

07年的华科招收研究生上机试题

一、编写一个程序输入一个5X6的矩阵存储并输出,并且求出每行的最大值和每行的总和要求把每行总和放入每行最大值的位置,最后把结果矩阵,每行最大值及其原下标及其总和输出。

#include

#include

intgetMat(inta[5][6],intn,intm)

{

inti;

intj;

for(i=0;i

{

for(j=0;j

{

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

}

}

return0;

}

intgetMax(inta[5][6],intn,intm,intb[5],intc[5],intd[5],intsum[5])

{

inti;

intj;

for(i=0;i

{

b[i]=0;

c[i]=0;

d[i]=0;

sum[i]=0;

}

for(i=0;i

{

intmax=a[i][0];

intcol=0;

intr=0;

for(j=0;j

{

sum[i]+=a[i][j];

if(max

max=a[i][j];

r=i;

col=j;

}

b[i]=max;

c[i]=r;

d[i]=col;

}

for(i=0;i

{

a[c[i]][d[i]]=sum[i];

}

return0;

}

voiddisplayMat(inta[5][6])

{

inti;

intj;

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

{

printf("\n");

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

{

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

}

}

printf("\n");

}

intmain()

{

inta[5][6];

getMat(a,5,6);

intc[5],d[5],b[5],sum[5];

getMax(a,5,6,b,c,d,sum);

printf("每行最大值:

\n");

for(inti=0;i<5;i++)

{

printf("第%d行:

\n最大值:

%d\n列:

%d\n和:

%d\n",i+1,b[i],d[i],sum[i]);

}

printf("结果矩阵:

\n");

displayMat(a);

return0;

}

二、编写程序判断输入的任意字符,任意长度的字符串是否回文(顺读,逆读均相同),最后输出原字符串和判断结果。

#include

#include

#include

voidmain()

{

inti;

intbIsSame=1;//是否相同的标志

inta=0,b;

charf[255];

scanf("%s",f);//读入字符串

b=strlen(f)-1;//获得最后一个字符的序号(字符串总长度减)

for(i=0;i

{

if(f[a++]!

=f[b--])//首尾比较是否相同字符

{

bIsSame=0;//有不同的就把标志置,并跳出循环

break;

}

}

if(bIsSame)//相同就输出Y

printf("Y");

else//不同就输出N

printf("N");

getchar();//按任意键退出

}

三、输入一个6X6的矩阵并存储,把该矩阵逆置并输出。

要求:

不得使用任何数组(就地逆置)。

#include

#include

voidTwoDto1D(inta[6][6],intb[36])

{

inti,j;

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

{

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

{

b[6*i+j]=a[i][j];

}

}

}

voidTransposeInplace(intb[36],int&width,int&height)

{

intfrom,to,offset,step;

inti,j;

for(i=to=offset=0,step=width;i

for(j=0,from=i+offset;j

inttemp;

temp=b[to];

b[to]=b[from];

b[from]=temp;

//b[to]=b[from];

}

}

inttemp;

temp=width;

width=height;

height=temp;

}

intgetMat(inta[6][6],intn,intm)

{

inti;

intj;

for(i=0;i

{

for(j=0;j

{

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

}

}

return0;

}

voiddisplay(intb[36])

{

for(inti=0;i<36;i++)

{

if(i%6==0)

{

printf("\n");

}

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

}

}

intmain()

{

inta[6][6];

intb[36];

getMat(a,6,6);

TwoDto1D(a,b);

intwidth=6;

intheight=6;

TransposeInplace(b,width,height);

display(b);

return0;

}

==》06年的华科研究生复试上机试题:

(1)采用命令行方式复制任意多个文件内容到一个文件中,如下所示:

命令行形式:

mycopy1.txt2.txt3.txt4.txt...

功能:

复制2.txt3.txt4.txt…的内容到1.txt中

#include

#include

#include

#defineBUFFERSIZE1000

intmain(intargc,char*argv[])

{

if(argc<3)

{

printf("参数不足\n");

return0;

}

//intcnt=argc-1;

inti;

FILE*fp;

if(NULL==(fp=fopen(argv[1],"a")))

{

printf("目标文件打开失败!

");

return0;

}

fprintf(fp,"%s","\n");

FILE*fpSRC;

for(i=2;i

{

if(NULL==(fpSRC=fopen(argv[i],"r")))

{

printf("源文件打开失败!

");

return0;

}

chartemp[BUFFERSIZE+1];

while(fgets(temp,BUFFERSIZE,fpSRC))

{

fprintf(fp,"%s\n",temp);

}

fclose(fpSRC);

}

printf("内容复制成功!

\n");

fclose(fp);

return0;

}

(2)判定一个C语言变量的命名是否正确

#include

intcheck(char*str)

{

char*temp=str;

if(!

(*temp=='_'||(*temp>='a'&&*temp<='z')||(*temp>='A'&&*temp<='Z')))

{

return0;

}

temp++;

while(*temp!

='\0')

{

if(!

(*temp=='_'||(*temp>='a'&&*temp<='z')||(*temp>='A'&&*temp<='Z')||(*temp>='0'&&*temp<='9')))

{

return0;

}

temp++;

}

return1;

}

intmain()

{

while

(1)

{

printf("inputvar:

");

charhaha[255];

scanf("%s",haha);

if(check(haha))

printf("CORRECT\n");

else

printf("WRONG\n");

}

return0;

}

(3)建立一种数据结构,可以存储任意个、任意长度的整数,利用这个数据结构,输入一串数,排序,求累加和

#include

#include

#include

typedefstructnode

{

inti;

structnode*next;

structnode*pre;

}bigIntNode,*bigInt;

bigIntgetBigInt(char*s)

{

bigIntbi;

bi=(bigInt)malloc(sizeof(bigIntNode));

bi->pre=NULL;

bigInttemp=bi;

char*p=s;

while(*p!

='\0')

{

bi->i=*p-48;

bi->next=(bigInt)malloc(sizeof(bigIntNode));

bigIntt=bi;

bi=bi->next;

bi->pre=t;

p++;

}

bi->next=NULL;

returntemp;

}

intdisplay(bigIntbi)

{

bigIntp=bi;

if(p->i)

{

printf("%d",p->i);

}

p=p->next;

while(p->next)

{

printf("%d",p->i);

p=p->next;

}

printf("\n");

return1;

}

bigIntadd(bigIntbi1,bigIntbi2)

{

bigIntp=bi1;

bigIntq=bi2;

while(p->next)

{

p=p->next;

}

while(q->next)

{

q=q->next;

}

bigIntret=(bigInt)malloc(sizeof(bigIntNode));

ret->next=NULL;

ret->i=0;

intfinali=0;

intadd=0;

while(p&&q)

{

inti=p->i+q->i;

intt=0;

intj=0;

if(i>9)

{

j=i/10;

t=i%10;

}

else

{

j=0;

t=i;

}

ret->i=t;

bigInttb=(bigInt)malloc(sizeof(bigIntNode));

tb->i=0;

tb->i+=j;

ret->pre=tb;

tb->next=ret;

ret=tb;

p=p->pre;

q=q->pre;

finali=j;

}

if(p)

{

intf=0;

while(p)

{

f=p->i+ret->i;

intt=0;

intj=0;

if(f>9)

{

j=f/10;

t=f%10;

}

else

{

j=0;

t=f;

}

ret->i=t;

bigInttb=(bigInt)malloc(sizeof(bigIntNode));

tb->i=0;

tb->i+=j;

ret->pre=tb;

tb->next=ret;

ret=tb;

p=p->pre;

}

}

if(q)

{

intf=0;

while(q)

{

f=q->i+ret->i;

intt=0;

intj=0;

if(f>9)

{

j=f/10;

t=f%10;

}

else

{

j=0;

t=f;

}

ret->i=t;

bigInttb=(bigInt)malloc(sizeof(bigIntNode));

tb->i=0;

tb->i+=j;

ret->pre=tb;

tb->next=ret;

ret=tb;

q=q->pre;

}

}

ret->pre=NULL;

returnret;

}

intgetlength(bigIntbi)

{

bigIntp=bi;

inti=0;

while(p)

{

p=p->next;

i++;

}

returni;

}

intcompare(bigIntbi1,bigIntbi2)

{

bigIntp=bi1;

bigIntq=bi2;

if(getlength(p)>getlength(q))

{

return1;

}

elseif(getlength(p)

{

return-1;

}

else

{

for(;p&&q;p=p->next,q=q->next)

{

if(p->i>q->i)

{

return1;

}

elseif(p->ii)

{

return-1;

}

}

return0;

}

}

intsort(bigInt*bi,intn)

{

inti;

intj;

for(i=0;i

{

bigInttemp;

for(j=i+1;j

{

if(compare(bi[i],bi[j]))

{

memcpy(temp,bi[i],sizeof(bigInt));

memcpy(bi[i],bi[j],sizeof(bigInt));

memcpy(bi[j],bi[i],sizeof(bigInt));

}

}

}

return0;

}

intmain()

{

bigInt*bia;

bia=(bigInt*)malloc(1000*sizeof(bigInt));

intcnt=0;

charstr[1000];

printf("输入大数\n");

for(inti=0;i<1000;i++)

{

scanf("%s",str);

if(strcmp(str,"0")==0)

break;

bia[i]=getBigInt(str);

cnt++;

}

bigIntsum=bia[0];

for(inti=1;i

{

sum=add(sum,bia[i]);

}

printf("原来大数:

\n");

for(inti=0;i

{

display(bia[i]);

}

sort(bia,cnt);

printf("排序后大数:

\n");

for(inti=0;i

{

display(bia[i]);

}

printf("和:

\n");

display(sum);

return0;

}

==》05-03年华科招收研究生复试上机试题汇总:

(1)输入一个数列以0为结束标志,建立链式线性表,查找其中最大的数并输出删除释放节点,然后对剩余的进行排序,并输出释放节点。

#include

#include

typedefstructnode

{

intvalue;

structnode*next;

}LNode,*SqList;

voidcreateList(SqList&L)

{

SqListp=L;

while

(1)

{

intvalue;

scanf("%d",&value);

if(value==0)

{

p=NULL;

return;

}

p->value=value;

p->next=(SqList)malloc(sizeof(LNode));

p=p->next;

}

}

voiddisplay(SqListL)

{

SqListp=L;

while(p->value!

=-1163005939)

{

printf("%d",p->value);

SqListq=p->next;

free(p);

p=q;

}

}

intgetMax(SqListL)

{

if(L)

{

SqListp=L;

inttemp=p->value;

p=p->next;

while(p&&p->value!

=-1163005939)

{

if(tempvalue)

{

temp=p->value;

}

p=p->next;

}

returntemp;

}

}

voidfreeMax(SqList&L)

{

if(L)

{

SqListp=L;

SqListq;

while(p)

{

if(p->value==getMax(L))

{

q=p->next;

free(p);

L=q;

break;

}

p=p->next;

}

}

}

voidSortSqList(SqList&L)

{

SqListp,q,small;

inttemp;

for(p=L;p->next;p=p->next)

{

small=p;

for(q=p->next;q;q=q->next)

{

if(q->valuevalue)

small=q;

if(small!

=p)

{

temp=p->value;

p->value=q->value;

q->value=temp;

}

}

}

}

intmain()

{

SqListL;

L=(SqList)malloc(sizeof(LNode));

createList(L);

intmax=getMax(L);

printf("最大值:

%d\n",max);

freeMax(L);

SortSqList(L);

printf("排序后:

\n");

display(L);

}

(2)输入一个数列以0为结束标志,建立二叉遍历树,并对其进行逆中序遍历,释放空间。

#include

#include

typedefstructtnode

{

intvalue;

structtnode*lchild;

structtnode*rchild;

}BTreeNode,*BTree;

BTreecreateBTree()

{

intvalue;

BTreet;

scanf("%d",&value);

if(value==0)/*判断当前子树是否创建完成*/

returnNULL;

else

{

t=(BTree)malloc(sizeof(BTreeNode));

t->value=value;

t->lchild=createBTree();

t->rchild=createBTree();

returnt;

}

}

voidReInorderBTree(BTreeB)

{

BTreep=B;

if(p)

{

InorderBTree(p->rchild);

printf("%d\n",p->value);

free(p);

InorderBTree(p->lchild);

}

}

intmain()

{

printf("输入整数,以结尾\n");

BTreeB=createBTree();

printf("中序遍历如下:

\n")

展开阅读全文
相关搜索
资源标签

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2