南昌大學C程序設計(雙語)作業(yè)與答案_第1頁
南昌大學C程序設計(雙語)作業(yè)與答案_第2頁
南昌大學C程序設計(雙語)作業(yè)與答案_第3頁
南昌大學C程序設計(雙語)作業(yè)與答案_第4頁
南昌大學C程序設計(雙語)作業(yè)與答案_第5頁
已閱讀5頁,還剩21頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

Chapter1Introduction

1、1.Whichofthefollowingareillegalidentifiers?Circletheillegal

identifiers.

A.3idB._yesC.starlitD.int

教師批改:ACD

2、1.Whichofthefollowingareillegalidentifiers?Circletheillegal

identifiers.

A.xyshouldlB.me_to_2C.one_i_aren,'tD.2_i_am

教師批改:BCD

Chapter2LearningbyExample

1、Whatiscorrectaboutthefollowingprogram?

ttinclude<stdio.h>

#definecount6

voidmain(void)

{

count=count+1;

printf(〃%d〃,count);

}

A.’7'isoutputandcountbecomes7B'7'isoutputandcount

becomes7

C.RuntimeErrorD.’7'isoutputbutcountremains6

教師批改:C

2、IndicatewhichofthefollowingarelegalvariablenamesinC:

A、XB、formulalC、average_rainfallD、%correct

教師批改:ABC

3、IndicatewhichofthefollowingarelegalvariablenamesinC:

A、shortB、tinyC、totalrainfallD、aReasonab1yLongVariab1eName

教師批改:BD

4、IndicatewhichofthefollowingarelegalvariablenamesinC:

A、12MonthTotalB、marginal-costC、b4handD、_stk_depth

教師批改:CD

5、IndicatewhichofthefollowingarelegalvariablenamesinC:

A、shortB、4formulaC、average_rainfallD、%correct

教師批改:c

6、IndicatewhichofthefollowingarelegalvariablenamesinC:

A、shortB、formula_5C、average_rainfallD、4correct

教師批改:BC

7、IndicatewhichofthefollowingarelegalvariablenamesinC:

A、shortB、formala6C.floatD.printf

教師批改:BD

8、“Considerthefollowingcodefragment:

intdblarray[10],*dblPointer;

Whichofthefollowingstatementsarevalid(i.e.whichoneswillcompile)?

Circleallthecorrectanswers(theremaybemorethanonecorrect

answer)

A.dblPointer=db1Array;B.dblPointer=dblArray[4];

C.dblPointer二&(dblArray[2]);D.dblPointer=*db1Array;

教師批改:AC

9、Indicatethevaluesandtypesofthefollowingexpressions:2+3

value:type:

教師批改:5integer

10、Indicatethevaluesandtypesofthefollowingexpressions:19/5

value:type:

教師批改:3integer

11>Indicatethevaluesandtypesofthefollowingexpressions:19.0/5

value:type:

教師批改:3.8double

12、Indicatethevaluesandtypesofthefollowingexpressions:3*6.0

value:type:

教師批改:18.0double

13、Indicatethevaluesandtypesofthefollowingexpressions:19%5

value:type:

教師批改:4integer

14、Indicatethevaluesandtypesofthefollowingexpressions:2%7

value:type:

教師批改:4integer

15、Byapplyingtheappropriateprecedencerules,calculatetheresultof

thefollowingexpression:6+5/4-3,resultis

教師批改:4

16、Byapplyingtheappropriateprecedencerules,calculatetheresultof

thefollowingexpression:10+9*((8+7)%6)+5*4%3*2+l,resultis

教師批改:42

17、Byapplyingtheappropriateprecedencerules,calculatetheresultof

thefollowingexpression:1+2+(3+4)*((5*6%7*8)-9)-10,resultis

教師批改:42

18、Rewritethefollowingfloating-pointconstantsinC'sformfor

scientificnotation:29979250000.0

教師批改:29979250000

19、Rewritethefollowingfloating-pointconstantsinC'sformfor

scientificnotation:0.00000000529167

教師批改:0.00000000529

20、Evaluatethefollowingexpression:(2Points)

inti=l,j=2,k=3,m=4;

i+=j+k;//i=______________

j*=k=m+5;//j=

教師批改:618

21、Thevariablesareinitializedasfollows,

charc='A';

inti=7,j=7;

doublex=0.0,y=2.3;

Evaluatethefollowingexpressions:(5Points)

!c_______

!(i-j)____________

!i-j______________

!!(x+y)____________

!x*!!y______________

教師批改:01-711

22>Indicatethevaluesandtypesofthefollowingexpressions:2%7

value:type:

教師批改:2integer

23、Byapplyingtheappropriateprecedencerules,calculatetheresultof

thefollowingexpression:2+2*(2*2-2)%2/2,resultis

教師批改:2

24、ThefollowingCprogramiscompiledandrunssuccessfully.Writethe

outputthefollowingprogramproduces.

ttinclude<stdio.h>

voidmain()

intk=42;

printf(//z,%d\nz///,k++);

printf(〃〃%d\n〃〃,++k);

return0;

}

教師批改:4244

25、Writeaprogramthatreadsinalistofintegersuntiltheuserenters

thevalue-1asasentinel.Atthatpoint,theprogramshoulddisplaythe

averageofthevaluesenteredsofar.

Chapter3ProblemSolving

1、〃Whatistheoutputofthefollowingcode?

ttinclude<stdio.h>

voidmain(void)

{

inta;

a=1;

printf(〃〃%i〃〃,++a);

}

A.Compile-timeErrorB.0C.1D.2

教師批改:D

2、Use#definetointroduceaconstantnamedpiwiththevalue

3.14159________________

教師批改:ttdefinepi3.14159

3、Writeaprintfstatementtodisplaythefloating-pointvaluestored

inthevariabledistancesothatexactlythreedigitsappeartotheright

ofthedecimalpoint_____________________________

教師批改:printf(〃%.3f/z,distance);

4、Useforcontrollinetocountfrom15to25

教師批改:for(i=15;i<=25;i++)

5、WriteaprogramthatdisplaysthemessageZ/Hello,world.〃10timeson

separatelines._________________________________

教師批改:main(){intk;for(k=l;k<=10;k++)printf(z,Hello,world.\n/z);}

6、Writeaprogramthatprintsatabularversionofboththesquaresand

cubesofthenumberfrom1to10.

Chapter4StatementForms

1、Considerthefollowingprogram:

ttinclude<stdio.h>

voidmain()

{inta=l,b=2,c=3,d=0;

if(a==l&&b++==2)

if(b!=21|c--!=3)

printf(%d,%d\n”,a,b,c);

elseprintf(a%d,%d,%d\n”,a,b,c);

elseprintf(%d,%d\n”,a,b,c);

)

Theoutputoftheprogramis:

A)1,2,3B)1,3,2

C)1,3,3D)3,2,1

教師批改:C

2、Considertheprogramfragmentandgivetheoutput:

For(i=0;i<4;i++,i++)

for(k=l;k<3;k++);printf("*");

A)********B)****

C)**D)*

教師批改:D

3、“Variablesi,j,andkareintegerswherei=2,j=3andk=5

respectively.DothefollowingCexpressionsevaluatetotrueorfalse?

Youanswersshouldbeeither1or0.

a)(i>k)&&((i+j)==k)

b)((i+j)!=k)||(((j*j)>k)&&((i*i)<k))

c)k<(j<i)"

教師批改:010

4、“ThefollowingCprogramiscompiledandrunssuccessfully.Writethe

outputthatthisprogramproduces.

ttinclude<stdio.h>

main()

{

intx=4;

inty=4;

if((x<=y)&&(x〉二y))

printf("Bingo");

else

printf("Nogo");

教師批改:Bingo

5、Whatwillbeprintedtothescreenifweexecutethecode?

ttinclude<stdio.h>

voidmain()

{

intindex=0;

if(index=0)

printf(〃A〃);

if(index=1)

printf(〃B〃);

)

教師批改:B

6、Whatarethevaluesofthefollowingstatement,assumingthati,j,and

karedeclaredasintegervariables:i=(j=4)(k=16)

i=J=,k=

教師批改:64,4,16

7、Writeaforloopcontrollinetocountfrom1to100

教師批改:for(k=l;k<=100;k++)

8、Writeaforloopcontrollinetocountbackwardbytwosfrom100to

0

教師批改:for(k=100;k>=0;k=k-2)

9、ThefollowingCprogramcompilesandrunswithouterrors.Writethe

outputofthisprogram.

ttinclude<stdio.h>

voidmain(){

inti=-1;

intj=1;

if(0<i||i<10)

if(j>i)printf(〃A〃);

elseprintf(〃B〃);

else

printf(〃C〃);

教師批改:A

10、Whatistheoutputofthefollowingcode?

ttinclude<stdio.h>

voidmain(){

intday_of_week=4;

switch(day_of_week)

case0:printf("todayisSunday.\n〃);

case1:printf(/ztodayisMonday.\n〃);

case2:printf("todayisTuesday.\n〃);

case3:printf("todayisWednesday.\n〃);

case4:printf("todayisThursday.\n〃);

case5:printf(/ztodayisFriday.\n〃);

case6:printf("todayisSaturday.\n〃);

default:

printf(^Somethingismissing.z/);

break;

)

)

教師批改:todayisThursday

todayisFriday.

TodayisSaturday.

Somethingismissing.

11>ThefollowingCprogramiscompiledandrunssuccessfully.Writethe

outputthefollowingprogramproduces.

#include<stdio.h>

#defineNRows5

voidmain()

{

inti,j;

for(i=1;i<=NRows;i++){

for(j=i;j<NRows;j++){

printf(〃〃);

)

for(j=0;j〈2*i—1;j++){

printf(〃*〃);

}

printf(〃\n〃);

}

)

教師批改:'*

***

12、ThefollowingCprogramiscompiledandrunssuccessfully.Writethe

outputthefollowingprogramproduces.

ttinclude<stdio.h>

voidmain()

inta[6][6],i,j;

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

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

a[i][j]=(i/j)*(j/i);

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

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

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

printf(〃〃\n〃〃);

教師批改:10000

01000

00100

00010

00001

13、Thefollowingcodefragmentisplacedinacompleteprogram,compiled

andrunwithouterrors.Writetheoutputofthefollowingcode:

inti;

for(i=10;i>=0;i-=3)

printf(/z%d/z,i-l);/**/

教師批改:9630

14、Howmanylinesofoutputdoesthefollowingprogramprint?

ttinclude<stdio.h>

voidmain(){

inti,j;

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

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

printf(Z/Helloworld!\nz/);

教師批改:1

15、Completetheprogrambelowtoproducethefollowingoutput:

1x12=12

2x12=24

3x12=36

10x12=120

11x12=132

12x12=144

^include<stdio.h>

voidmain(void){

inti;

for(;;)

printf(//z/%dx12=%d\n〃〃,,);

)

教師批改:i=li<12i++i*12\n

16、ThefollowingCprogramiscompiledandrunssuccessfully.Writethe

outputthefollowingprogramproduces.

#defineLowerLimit1

#defineUpperLimit4

#include<stdio.h>

voidmain()

{

inti;

printf("NumberSquareCube\n〃);

for(i=LowerLimit;i<=UpperLimit;i++){

printf(/z%2d%3d%4d\n/z,i,i*i,i*i*i);}

)

教師批改:111、248、3927

17、Whilewe'reonthesubjectofsillysongs,anotheroldstandbyis

“ThisOldMan"forwhichthefirstverseisThisoldman,heplayed1.He

playedknick-knackonmythumb.Withaknick-knack,paddy-whack,Giveyour

dogabone.Thisoldmancomerollinghome.Eachsubsequentverseisthe

same,exceptforthenumberandtherhymingwordattheendofthesecond

line,whichgetsreplacedasfollows:2-shoe3-knee4-door5-hive

6-sticks7-heaven8-pate9-spine10-shin

Chapter5Function

1、Considerthefollowingprogram:

ttinclude<stdio.h>

intf(intx,inty)

{return((y-x)*x);}

voidmain()

{inta=3,b=4,c=5,d;

d=f(f(a,b),f(a,c));

printf(〃%d\n〃,d);

A)10B)9C)8D)7

教師批改:B

2、Thefollowingprogramcompilesandexecuteswithouterrors.Writethe

outputofthisprogram.(8Points)

ttinclude<stdio.h>

intfoo(intx){

x++;

returnx;

}

intbar(inty){

y-;

returny;

}

voidmain(){

intx=1;

inty=3;

foo(x);

bar(y);

printf(〃%d%d\n/z,x,y);

y=foo(x);

x=bar(y);

printf(〃%d%d\nz/,x,y);

)

教師批改:13

12

3、Thefollowingprogramcompilesandexecuteswithouterrors.Writethe

outputofthisprogram.

ttinclude<stdio.h>

intcountl(intk){

intx=1;

x=x+k;

printf(/z%d〃,x);

returnx;

}

intcount2(intk){

staticintx=1;

x=x+k;

printf(〃/d〃,x);

returnx;

voidmain(){

count1(count1(1));

count2(count2(2));

}

教師批改:23

36

4、ThefollowingCprogramiscompiledandrunssuccessfully.Writethe

outputthefollowingprogramproduces.

intfun(intx,inty,intz)

{z=x*x+y*y;}

voidmain()

{

inta=31;

fun(5,2,a);

printf(〃%d〃,a);

}/**/

教師批改:31

5、Thefollowingprogramcompilesandexecuteswithouterrors.Writethe

outputofthisprogram.

ttinclude<stdio.h>

intpfft(intx)

{

if(x==0)

return0;

/*Hint:%givestheremainder,example5%2is1*/

if((x%2)!=0)

return(1+pfft(x-1));

else

return(3+pfft(x-1));

}

voidmain()

{

printf(/z%d\n,z,pfft(7));

}

教師批改:13

6、ThefollowingCprogramiscompiledandrunssuccessfully.Writethe

outputthefollowingprogramproduces.

ttinclude<stdio.h>

voidmain()

{

intw=2;

voidfun(int);

fun(w);

)

voidfun(intk)

{if(k>0)fun(k-l);

printf("%d",k);

}

教師批改:012

7、Whatoutputwillthefollowingpieceofcodeproduce?

voidfoo(intc)

{

c=20;

}

voidbar(intd[])

d[l]=30;

)

voidmain(void)

{

intx=10;

inty=15;

inta[2]={1,2};

intb[2]={3,4};

foo(a[0]);

printf(/z%d%d%d%d%d%d\n〃,x,y,a[0],a[l],b[0],b[l])

bar(b);

printf(/z%d%d%d%d%d%d\n/z,x,y,a[0],a[l],b[0],b⑴)

foo(y);

printf(/z%d%d%d%d%d%d\n/z,x,y,a[0],a:l],b[0],b⑴)

bar(a);

printf(zz%d%d%d%d%d%d\n〃,x,y,a[0],a[l],b[0],b⑴)

/*__________________________*/

)

教師批改:10151234

101512330

101512330

1015130330

8、ThefollowingCprogramiscompiledandrunssuccessfully.Writethe

outputthatthisprogramproduces.

#include<stdio.h>

voidmain()

{inta=l,b=2,c=3;

a+=b+=++c;

printf(/z%5d%5d%5d\n/z,a,b,c);//(1)

{

floatb=4.0;

intc;

a+=c=5*b;

printf(〃%5d%5.lf%5d\n,z,a,b,c);//(2)

)

printf(/z%5d%5d%5d\n/z,a,b,c);(3)

)

教師批改:(1)764(2)274.020(3)2764

9、WriteafunctionFib(n)tocalculatethenthFibonaccinumber.

Chapter11Arrays

1、Itisanarraydeclaration:

inta[3];

Whichoneisinvalidtorefertoparticularmembersofarrays:

A\a[0]B\a[U2]C\a[4-2]D\a[3]

教師批改:D

2、AssumewehavedeclaredthetwodimensionalarrayAintheClanguage,

intA[3][2]={0,1,2,3,4,5);

WritethevalueofA[2][1].

教師批改:5

3、Thisprogramcomputestheaverageofallthevaluesofanarraynamed

‘list'.Assumethatthecountofthenumberofvaluesis100orfewer.

ttinclude<stdio.h>

ttdefineARRAY_SIZE100

main()

{

intlist[ARRAY_SIZE];

intcount=0;

doubletotal=0.0;

while(scanf(/z%d/z,&list[count])==1)

total=;

count=;

printf(/zaverage=%lf\n〃,);

}

教師批改:total=total+list[count];

count=count+l;

total/count

4、Thisprogramreadsinanarrayofintegers,reversestheelementsof

thearray,andthendisplaystheelementsintheirreversedorder.Please

completetheprogram.

#defineMaxElement5

ttinclude<stdio.h>

voidinput(inta[],intn);

voidreverse(inta[],intn);

voidoutput(inta[],intn);

voidmain()

{inti,array[MaxElement];

input(array,MaxElement);

reverse(array,MaxElement);

output(array,MaxElement);

}

voidinput(inta[],intn){

inti;

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

{

printf(zzarray[%d]=?\n/z,i);

scanf(zz%d//,&a[i]);

}

)

/*Thisfunctionreversestheelementsofarray,whichhasnasits

effectivesize.*/

voidreverse(inta[],intn)

{

inti,temp;

for(i=0;;i++)

}

)

voidoutput(inta[],intn)

inti;

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

printf(zzarray[%d]=%d\n/z,i,a[i]);

)

教師批改:i〈n/2temp=a[i];a[i]=a[n-i-l];a[n-i-l]=temp;

5、Thecodefragment(below)hasanerror.Changeonecharactertoremove

theerror.

inttotal[15];

intfactorl[15];

intfactor2[15];

inti;

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

total[i]=factorl[i]+factor2[i]+factor2[i/2];

教師批改:for(i=0;i<15;i++)

即去掉〃二〃,引用數(shù)組元素越界

6、ThefollowingCprogramiscompiledandrunssuccessfully.Writethe

outputthefollowingprogramproduces.

main()

{

intaa[4][4]={{1,2,3,4},{5,6,7,8},{3,9,10,2},{4,2,9,6}};

inti,s=0;

for(i=0;i<4;i++)s+=aa[i][1];

printf("%d\n",s);

)

教師批改:19

7、ThefollowingCprogramiscompiledandrunssuccessfully.Writethe

outputthefollowingprogramproduces.

voidf(intarr[],intlen);

voidmain()

{

intj;

inta[3]={1,2,3};

f(a,3);

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

printf(/z%d\nz/,a[j]);

}

voidf(intarr[],intlen)

{

inti;

for(i=0;i<len;i++){

arr[i]++;

教師批改:234

8、Thisfunctiongoesthroughthefirstnelementsofthearray,andremoves

anyelementswhosevalueiszero.Theremainingelementsareshiftedto

fillupthecellsformerlyoccupiedbythezerovalues,sothatthefinal

resultisthatthearraycontainsonlythenon-zerovaluesinitsinitial

elements.

intRemoveZeroElements(intarray[],intn)

{

intIh,rh;

Ih=0;

for(rh=0;rh<n;rh++){

if(array[rh]){

array[Ih]=array[];

教師批改:!=0rhlh++

10

Writeaprogramtoalculatethevalueofthefirsttwentyitems(N=20)of

Fibonaccilistfollowtheformul:

a(l)=a(2)=l

a(n)=a(n-l)+a(n-2)

Chapter13Pointers

1、Considerthefollowingprogram:

ttinclude<stdio.h>

voidfun(int*a,int*b)

{

int*c;

c=a;a=b;b=c;

}

voidmain()

{

intx=3,y=5,*p=&x,*q二&y;

fun(p,q);printf(〃%d,%d,z,,*p,*q);

fun(&x,&y);printf(〃%d,%d\n〃,*p,*q);

}

Theoutputoftheprogramis

A)3,5,5,3B)3,5,3,5C)5,3,3,5D)5,3,5,

教師批改:B

2、1.What?stheresultwiththefollowingprogram?(

#include<string.h>

voidmain(){

char*p="abcdefgh”;

printf("%d\n",strlen(strcpy(p+3,ABCD")));

puts(p);

}

A)8B)12C)4D)7

教師批改:

3、1.What?stheresultwiththefollowingprogram?(

sub(intx,inty,int*z)

{*z=y-x;}

voidmain()

{inta,b,c;

sub(10,5,&a);

sub(7,a,&b);

sub(a,b,&c);

printf("%4d,%4d,%4d\n”,a,b,c);

}

A)5,2,3B)-5,-12,-7C)-5,-12,-17D)5,-2,-7

教師批改:B

4、ttinclude<stdio.h>

voidf(int*p,int*q);

voidmain()

{

intm=l,n=2,^r=&m;

f(r,&n);printf(/z%d,%d〃,m,n);

}

voidf(int^p,int*q)

{

p=p+1;

*q=*q+1;

)

A)1,3B)2,3C)l,4D)l,2

教師批改:A

5、What'stheoutputwiththefollowingprogram?

ttinclude<stdio.h>

voidf(int*p,int*q);

voidmain()

intm=l,n=2,^r=&m;

f(r,&n);printf(〃%d,%d〃,m,n);

voidf(int*p,int*q)

{

P=P+1;

*q=*q+1;

}

A)1,3B)2,3C)l,4D)l,2

教師批改:A

6、“Considerthefollowingcodefragment:

intx=5;

int*y=&x;

int*z;

(*y)++;

z=y;

printf(〃〃%d〃〃,*z);

Whatistheoutput?

教師批改:6

7、“Theprogrambelowcompileswithouterrorsandexecutescorrectly.

Writetheoutputofthisprogram.(6Points)

ttinclude<stdio.h>

voidfoo(int*a){

*a+二5;

)

voidbar(intb[]){

b[l]=15;

}

main(){

inta[3]={3,4,5);

intb[3]={3,4,5};

int*p2;

p2=&a[0];

(*p2)++;

bar(p2);

printf(〃〃%d%d%d\n〃〃,a[0],a[l],a[2]);

p2=&b[l];

foo(p2);

bar(p2);

printf(///z%d%d%d\n〃〃,b[0],b[l],b[2]);

教師批改:“4155\3915

8、Declarationsandinitializationsasfollowing:

inti=3,j=5,*p=&i,*q=&j,*r;

doublex;

Evaluatethefollowingexpressions:

p==&i//(1)

**&p//(2)

r=&x//(3)

7**p/*q+7//(4)

*(r=&j)*=*p//(5)

教師批改:(1)1

(2)3(3)illegal(4)11(5)15

9、#include<stdio.h>

voidtry_me(int[][3]);

intmain(void){

inta[3][3]={{2,5,7},{0,-1,-2},{7,9,3});

try_me(a);

return0;

)

voidtry_me(int(*a)[3]){

printf(zz%d%d%d%d...infinity\n/z,

a[l][0],-a[l][l],a[0][0],a[2][2]);

)

教師批改:0123…infinity

10、〃Theprogrambelowcompileswithouterrorsandexecutescorrectly.

Writetheoutputofthisprogram.(8Points)

ttinclude<stdio.h>

voidswapl(inta,intb){

inttmp=a;

a=b;

b=tmp;

}

voidswap2(inta[]){

inttmp=a[0];

a[0]=a[l];

a[l]=tmp;

}

voidswap3(int*a,int*b){

inttmp=*a;

*a=*b;

*b=tmp;

}

voidtripleswap(int*a,int*b,int*c){

inttmp=*a;

*a=*b;

*c=tmp;

*b二*c;

}

main(){

inta[2];

a[0]=0;

a[l]=1;

swapl(a[0],a[l]);

printf%d\n〃〃,a[0],a[l]);

swap2(a);

printf%d\n〃〃,a[0],a[l]);

swap3(&a[0],&a[l]);

printf%d\n〃〃,a[0],a[l]);

a[0]=0;

a[l]=1;

tripleswap(&a[0],&a[l],&a[0]);

printf%d\n〃〃,a[0],a[l]);

}

教師批改:〃01\10\01\10〃

11>Designafunctionprototypethatwouldallowasinglefunctiontofind

andreturnsimultaneouslyboththelowestandhighestvaluesinanarray

oftypedouble.

南昌大學賣膾報告

學生姓名:學號:專業(yè)班級:

實驗類型:口驗證口綜合口設計口創(chuàng)新

實驗日期:實驗成績:

一、實驗項目名稱

NQueenProblem

(MachinePractice)

二、實驗目的

[Objectives]

Themachineproblemistotrainthestudenttointegratealltheknowledge

fromthebookstosolvetheproblem.Toimprovetheabilityofprogrammingin

CandactivetheinterestingatCprogramminglanguagethroughthis

integratedexperiment.

三、實驗要求

[Contents]:

Asquarewhichiscomposedofnmultipliedbynblocksis

called11checkerboardofnelementsIftwoqueensarelocatedin

thesamelineorthesamecolumnoreventhesamediagonalline,

wesaytheyattackeachother.

Writea0programtodisplayalloftheoverallarrangements

whenallofthenqueensonthecheckerboarddon'tattackeach

other.

[Steps]:

1.Solutions

a.Thequeencouldn'tbeputinthesameroworcolumn,and

couldn'tinthediagonalline.

b.Howtomakesurethatnoqueencanbeputinthesamerowand

column?

Wecanuseanarrayx[n]torepresenttheNthrowandx[n]=kto

representthattheNthqueenbeputintheKthcolumn.

Wecanusealooptocomparethevalueofx[i](0<i<n)withthe

valueofx[j](O<j<i)andmakesurethatx[i]Wx[j].

c.Howtomakesurethatnoqueencanbeputinthesamediagonal

line?

Wefoundthatifx[i]andx[j]areinthesamediagonalline,

theirvaluefit|x[i]-x[j]|=|i-j|.ToavoidthisIusethesameloopin

solutionbtocomparethevalueof|x[i]-x[j]|withthevalueof|i-j|and

makesurethattheyaredifferent.

2.Algorithms

Firstly:WritethemainfunctiontodealwiththeproblemofNqueen.

(1).[placethefirstvalue]x[1]-0k-1

(2).[solvetheproblem]

Gotoloopwhenk>0,implement.

x[k]<-x[k]+1

gotoloopwhenx[k]<nandplace(k)=false,implement.

Ifx[k]<na

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論