揚州大學(xué)C語言上機作業(yè)1-9整理_第1頁
揚州大學(xué)C語言上機作業(yè)1-9整理_第2頁
揚州大學(xué)C語言上機作業(yè)1-9整理_第3頁
揚州大學(xué)C語言上機作業(yè)1-9整理_第4頁
揚州大學(xué)C語言上機作業(yè)1-9整理_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、答案僅供參考實驗一4設(shè)計main函數(shù)實現(xiàn)從鍵盤上輸入兩個整型變量a、b的值,交換它們的值并輸出。#include<stdio.h>void main()int a,b,c;printf("enter first integer :  ");scanf("%d",&a);printf("enter second integer :   ");scanf("%d",&b);c=a;a=b;b=c;printf("%d,%dn",a,b);ge

2、tch();5設(shè)計main函數(shù)實現(xiàn)從鍵盤上輸入兩個整型變量a、b的值,并將b的值加入到a中,輸出a的值。  #include<stdio.h>void main()int a,b,c;printf("enter first integer :  ");scanf("%d",&a);printf("enter second integer :   ");scanf("%d",&b);c=a+b;a=c;printf("a=%d,b=%dn&

3、quot;,a,b);getch();6從鍵盤輸入整數(shù)x的值,根據(jù)公式y(tǒng)=x3+3x2+x-10求y的值,輸出x和y的值。#include<stdio.h>void main()int x,y;printf("enter first integer :  ");scanf("%d",&x);y=x*x*x+3*x*x+x-10;printf("x=%d,y=%dn",x,y);getch();實驗二1 編寫程序,從鍵盤上輸入一個整數(shù)(例如560)表示分鐘,將其換算成用小時和分鐘表示,然后輸出至屏幕。#in

4、clude <stdio.h>void main()  int a,b,hour,min;  printf("enter first integer :  ");  scanf("%d",&a);  b=60;  hour=a/b;  min=a%b;  printf("hour=%d,min=%dn",hour,min);     getch();    

5、 2 編寫程序,輸入兩個整數(shù)(例如1500和350),求出它們的商和余數(shù)并進行輸出。#include <stdio.h>void main()   int a,b,c,d;   a=1500,b=350;   c=a/b;   d=a%b;   printf("%d,%d",c,d);   getch();  3.編寫程序,讀入3個整數(shù)給分別變量a,b,c,然后將a,b,c的值輸出到屏幕,再交換它們中的數(shù)值,把a中原來的值給

6、b,把b中原來的值賦給c,把c中原來的值賦給a,然后再次輸出a,b,c的值到屏幕。#include <stdio.h>void main() int a,b,c,d;printf("enter first integer : ");scanf("%d",&a);printf("enter second integer : ");scanf("%d",&b);printf("enter third integer : ");scanf("%d",&a

7、mp;c); printf("a=%d,b=%d,c=%dn",a,b,c);d=c;c=b;b=a;a=d;printf("a=%d,b=%d,c=%d",a,b,c);getch();4編寫程序,讀入3個雙精度數(shù),求它們的平均值輸出到屏幕。#include <stdio.h>void main()    double sum=0;    double a,b,c,d; printf("enter first integer :  ");&#

8、160;scanf("%lf",&a); printf("enter second integer :   "); scanf("%lf",&b); printf("enter third integer :  "); scanf("%lf",&c); sum=a+b+c;    d=sum/3.0;    printf("

9、d=%lf",d);    getch();    5下列程序中,要求main函數(shù)實現(xiàn)如下功能:從鍵盤上輸入3個正整數(shù),求出它們中的最大值。請完善程序,并在程序最后用注釋的方式給出你的測試數(shù)據(jù)及在這組測試數(shù)據(jù)下的運行結(jié)果。#include <stdio.h>void main() int a ,b ,c ,d ,max; printf(“Enter three integers:”); scanf(“%d%d%d”,&a,&b,&c); if(a>b) max=a; else max

10、=b; if(c>max) max=c; printf(“max of the three numbers is %d”,max);grtch();6. 請編程序:對從鍵盤上輸入的x值,根據(jù)以下函數(shù)關(guān)系計算出相應(yīng)的y值(設(shè)x,y均為整型量)。x值的范圍計算y的公式x<000<=x<10x10<=x<201020<=x<40-5x+20 #include <stdio.h>void main()    int x,y;  printf("x=");  scanf(&q

11、uot;%d" ,&x);  if (x<0)  y=0;   else if (x<10&&x>=0)  y=x;   else if (x>=10&&x<20)  y=10;   else if (x>=20&&x<40)  y=(-5)*x+20;  printf("%d" ,y);  getch();  實驗三1. 下列程

12、序想求出滿足如下條件的三位數(shù)n:(1)n除以11(整數(shù)相除)所得到的商等于n的各位數(shù)字的平方和;(2)n中至少有二位數(shù)字相同。如:131除以11的商為11,131各位數(shù)字的平方和為11,131中有二位數(shù)字相同,故131是所要求出的三位數(shù)中的一個;又如550,也是滿足條件的三位數(shù)。源程序中有些錯誤,請你改正并最終使程序得到如下的運行結(jié)果:131 550 900#include <stdio.h>void main() int n , a , b , c;for(n=1; n<1000; n+) a=n/100; b=n/10%10; c=n%10;if(n/11=a*a+b*b

13、+c*c&&(a=b|a=c|b=c)printf("%dn",n); getch(); 2. 請編程序,實現(xiàn)從鍵盤上輸入任意一個整數(shù)n,求出n的各位數(shù)字之和。例如,當(dāng)n為263時,各位數(shù)字之和為11。下面是一個可以實現(xiàn)逐位數(shù)字累加功能的程序段,試理解后應(yīng)用到自己的程序中。k=0;n=263;do k+=n%10;n/=10; while(n);#include <stdio.h>void main()    int n,k;  scanf("%d",&n); 

14、; k=0;  do k+=n%10;  n/=10;  while(n);  printf("%d",k);  getch();   3. 試找出符合下列條件的正整數(shù):(1)該數(shù)是一個三位數(shù);(2)該數(shù)是37的倍數(shù);(3)該數(shù)循環(huán)左移后得到的另兩個數(shù)也是37的倍數(shù)。例如148是37的倍數(shù),481和814也是37的倍數(shù)。#include<stdio.h>void main()int n,a,b;for(n=100;n<1000;n+)if(n%37=0)a=n/10+n%10*100;b

15、=n/100+n%100*10;if(a%37=0&&b%37=0)printf("%dn",n);getch();4請編程序利用下列公式求的近似值。公式為:要求:先求出前2n項的值,再求出2n+2項的值,直至二者之差小于105為止。#include<stdio.h>void main()int n; float a,b; a=1,b=4.0/3; n=1; while(b-a>=1e-5) a=b; n+; b=b*4*n*n/(2*n-1)/(2*n+1); 

16、; printf("%f,%f",2*a,2*b); getch(); 5. 請編程序求出滿足如下條件的四位數(shù)n:(1)n的范圍為5000,8000;(2)n千位上的數(shù)減百位上的數(shù)減十位上的數(shù)減個位上的數(shù)大于零。編程要求:以每行5個輸出滿足條件的數(shù)及該類數(shù)的個數(shù)。#include<stdio.h>void main()int n,a,b,c,d,k=0; for(n=5000;n<=8000;n+) a=n/1000; b=n/100%10; c=n/10%10; d=n%10; if(a-b-c-d)>0) print

17、f("%5d",n);k+; if(k%5=0) printf("n"); printf("k=%d",k); getch(); 6請編程序求出滿足如下條件的一個四位整數(shù),它的9倍恰好是其反序數(shù)(例如,1234與4321互為反序數(shù))。#include<stdio.h>void main()int n,e,a,b,c,d;  for(n=1000;n<=9999;n+)  a=n/1000; b=n/100%10; c=n/10%10; d=n%10; e=d

18、*1000+c*100+b*10+a; if(9*n=e) printf("%d",n); getch(); 7請編程序求出從鍵盤上輸入的兩個正整數(shù)a和b的最大公約數(shù)和最小公倍數(shù)。#include <stdio.h>void main() int m,n,r,x; scanf("%d,%d",&m,&n); x=m*n;while(n!=0) r=m%n; m=n; n=r; printf("%d %d",m,x/m);getch();8請編程序計算1!+2!+3!+

19、n!的前10項之和。#include <stdio.h>void main()    long sum,k;    int i,j;   sum=0;    for(i=1;i<=10;i+)              for(j=1,k=1;j<=i;j+)              k*=j;        

20、              sum+=k;          printf("%ldn",sum);     getch();     實驗四1. 反序數(shù)。例如,123的反序數(shù)是321。請設(shè)計程序,從鍵盤上輸入一個整數(shù),求其反序數(shù)并輸出。#include "stdio.h"main() int n,k;printf("n=");scanf

21、("%d",&n);k=0; while(n!=0) k=k*10+n%10; n/=10; printf("k=%d",k); getch(); 2. 完備數(shù)。完備數(shù)是一些特殊的自然數(shù),它所有的真因子(即除了自身以外的約數(shù))之和恰好等于該數(shù)本身。例如,6=1+2+3,故6是一個完備數(shù)。請設(shè)計程序,找出500以內(nèi)的所有完備數(shù)。#include "stdio.h"void main() int n,i,k;for(n=1;n<=500;n+)  

22、; k=0;for(i=1;i<=n-1;i+)    if(n%i=0)      k+=i;   if(n=k)   printf("%dn",n);  getch(); 3. 回文數(shù)。一個數(shù)如果正讀和倒讀(即反序數(shù))相同,稱該數(shù)是回文數(shù)。例如,121、1331均為回文數(shù)。請設(shè)計程序,找出200300之間的所有回文數(shù),并以每行8個數(shù)輸出至屏幕。#include "stdio.h"void main

23、() int n,k,i,m=0; for(n=200;n<=300;n+) k=0; i=n; while(i!=0) k=k*10+i%10; i/=10; if(n=k) m+; printf("%8d",n); if(m%8=0) printf("n"); getch(); 4. 回文質(zhì)數(shù)。一個數(shù)如果是回文數(shù),同時又是質(zhì)數(shù),則該數(shù)被稱為回文質(zhì)數(shù)。請設(shè)計程序,找出100500之間的所有回文質(zhì)數(shù),并以每行5個輸出至屏幕。#include "stdio.h"#include "math.h"void mai

24、n() int n,k,i,m; for(n=100;n<=500;n+)   k=0;    i=n;    while(i!=0)    k=k*10+i%10;      i/=10;    if(n=k)      for(m=2;m<=sqrt(n);m+)      

25、;  if(n%m=0)            break;                if(m>sqrt(n)           printf("%4d",n);account+;if(account%5=

26、0)printf(“n”);           getch();5. 絕對質(zhì)數(shù)。如果一個數(shù)和其反序數(shù)均為質(zhì)數(shù),則該數(shù)被稱為絕對質(zhì)數(shù)。如,13是質(zhì)數(shù),其反序數(shù)31也為質(zhì)數(shù),則13和31是一對絕對質(zhì)數(shù)。請設(shè)計程序,找出200以內(nèi)的絕對質(zhì)數(shù)。#include "stdio.h"#include "math.h"void main() int n,k,i,m,a; for(n=1;n<=200;n+) for(a=2;a<=sqrt(n);a+) if(n%a

27、=0) break; if(a>sqrt(n) i=n,k=0; while(i!=0) k=k*10+i%10; i/=10; for(m=2;m<=sqrt(k);m+) if(k%m=0) break; if(m>sqrt(k) printf("%5d",n); getch(); 6. 平方鏡反數(shù)。如果一個數(shù)的平方數(shù),是其反序數(shù)的平方數(shù)的反序數(shù),則稱該數(shù)和其反序數(shù)是一對平方鏡反數(shù)。例如,12的平方數(shù)是144,其反序數(shù)為21,21的平方數(shù)是441,其反序數(shù)是12的平方數(shù)144,故12和21是一對平方鏡反數(shù)。請設(shè)計程序找出200以內(nèi)的所有平方鏡反數(shù)對。#

28、include "stdio.h"#include "math.h"void main() int n,k,i,a,b;  for(n=1;n<=200;n+)     k=0;      i=n;      while(i!=0)      k=k*10+i%10;       i/=10; 

29、;      a=k*k;       b=0;       while(a!=0)       b=b*10+a%10;       a/=10;       if(b=(n*n)     

30、0; printf("%dn",n);     getch();   7黑洞數(shù)。黑洞數(shù)又稱陷阱數(shù),是類具有奇特轉(zhuǎn)換特性的整數(shù)。任何一個數(shù)字不全相同整數(shù),經(jīng)有限“重排求差”操作,總會得到某個數(shù),這些數(shù)即為黑洞數(shù)?!爸嘏徘蟛睢辈僮骷窗呀M成該數(shù)的數(shù)字重排后得到的最大數(shù)減去重排后得到的最小數(shù)。隨便造一個四位數(shù),如a1=1231,先把組成部分1231的四個數(shù)字由大到小排列得到a2=3211,再把1231的四個數(shù)字由小到大排列得a3=1123,用大的減去小的a2-a1=3211-1123=2088。把2088按上面的方法再作一遍,由大到

31、小排列得8820,由小到大排列得288,相減8820-288=8532。把8532按上面的方法再作一遍,由大到小排列得8532,由 小到大排列得2358,相減8532-2358=6174。如果再往下做,奇跡就出現(xiàn)了!7641-1467=6174,又回到6174。6174便是一個4位黑洞數(shù)。請編寫程序,用上述方法找出3位黑洞數(shù)。暫無答案實驗五1. 有一張足夠大的紙,厚0.09毫米,問將它對折多少次后可以達到珠穆朗瑪峰的高度(8848米)?#include "stdio.h"void main()double a=9e-2; int n=0;while(a<=8848)a

32、=a*2;n+; printf("a=%d",n);getch();2. 請設(shè)計程序采用遞推法計算下列級數(shù)的近似值,當(dāng)n取某一值使得|x(2n+1)/n!|<0.000001時停止計算(x從鍵盤輸入)。級數(shù)如下:#include "stdio.h"#include "math.h"void main()double sum,x,p,n,q,m,k; printf("x="); scanf("%lf",&x); sum=x;p=x;m=1;q=1;n=1

33、;k=1; while(fabs(p/n)>=1.0e-6)   m=-m;    p*=(x*x);    q+=2;    k+;    n*=k;    sum+=(m*p)/(q*n);        printf("%lf",sum);    getch();   

34、; 3. 請設(shè)計程序,根據(jù)下列公式計算cos(x)的近似值。精度要求:當(dāng)通項的絕對值小于等于10-6為止。cos(x)=1-x2/2!+x4/4!-x6/6!+(-1)nx2n/(2n)!#include "stdio.h"#include "math.h"void main() double x,k,sum,term,a,b,m;  printf("x=");  scanf("%lf",&x);  sum=1;  m=1;  a=0;  k=1;

35、  b=1;  term=1;  while(fabs(term)>1.0e-6)    k*=(x*x);     m=-m;     a+=2;     b*=(a*(a-1);     sum+=(m*k)/b);     term=(m*k)/b;       printf(

36、"cos(x)=%lf",sum);   getch();   4. 請設(shè)計程序采用遞推法計算的值。即求-+的值。其中x為鍵盤輸入的一個任意的單精度實數(shù)。#include "stdio.h"void main()int i,p; float x,m,q,sum; printf("x="); scanf("%f",&x); sum=x; m=x; p=1; q=1; for(i=1;i<

37、;=10;i+) m*=(x*x);  p=-p;  q*=i;  sum+=(p*m/q);    printf("%f",sum);  getch();  5. 請設(shè)計程序,根據(jù)下列公式計算表達式的值。計算公式如下: 當(dāng)通項絕對值小于等于10-6時停止累加。#include "stdio.h"#include "math.h"void main() double x,k,sum,term,a,b,m;  printf("x=&quo

38、t;);  scanf("%lf",&x);  sum=1;  m=1;  a=0;  k=1;  b=1;  term=1;  while(fabs(term)>1.0e-6)    k*=(x*x);     m=-m;     a+=2;     b*=(a*(a-1);     sum+

39、=(m*k)/b);     term=(m*k)/b;        sum=(-1)*sum*cos(x)/x;    sum+=3.14/2;    printf("%lf",sum);    getch();    實驗六3. 水仙花數(shù)是指一個3位數(shù),其各位數(shù)字的立方和等于該數(shù)本身。請完善下列程序中的函數(shù)int daffodil(int n),其功能

40、是判斷整數(shù)n是否為水仙花數(shù),如是,則該函數(shù)返回值,否則返回值。注意,不能更改main函數(shù)。#include <stdio.h>int daffodil(int n) int k=0,m; m=n; do k+=(m%10)*(m%10)*(m%10); m/=10;while(m); if(n=k) return 1; else return 0;main() int m,i=0; for(m=100;m<1000;m+) if(daffodil(m)=1) printf("%5d",m); i+; if(i%5=0)printf("n"

41、;); getch();4. 請編寫程序,找出滿足如下條件的整數(shù)m:(1)該數(shù)在11,999之內(nèi);(2) m、m2、m3均為回文數(shù)。例如m=11,m2=121,m3=1331,11、121、1331皆為回文數(shù),故m=11是滿足條件的一個數(shù)。請設(shè)計函數(shù)int value(long m),其功能是判斷m是否是回文數(shù),如是,該函數(shù)返回值1,否則返回值0。編寫main函數(shù),求出11,999內(nèi)滿足條件的所有整數(shù)。#include <stdio.h>int value(long m) long s,n; s=0; n=m; while(n) s=s*10+n%10; n/=10; if(s=m

42、) return 1; else return 0;main() long m; for(m=11;m<1000;m+) if(value(m)=1&&value(m*m)=1&&value(m*m*m)=1) printf("%5ld",m); getch();5. 設(shè)n0是一個給定的正整數(shù)。對于i=0,1,2,定義:若ni是偶數(shù),則ni+1=ni/2;若ni是奇數(shù),則ni+1=3ni+1;若ni是1,則序列結(jié)束。用這種方法產(chǎn)生的數(shù)稱為冰雹數(shù)。請編寫一個函數(shù)void hailstones(int n),其功能是顯示由n產(chǎn)生的所要求的序

43、列,按每行6個數(shù)輸出該數(shù)列中的所有數(shù)。編寫main函數(shù),在main函數(shù)中定義一個整型變量n,從鍵盤上輸入值77賦給n,用n作為實參調(diào)用函數(shù)hailstones。測試數(shù)據(jù):77輸出結(jié)果:Hailstones generated by 77:77 232 116 58 29 8844 22 11 34 17 5226 13 40 20 10 516 8 4 2 1Number of hailstones generated:23#include <stdio.h>void hailstones(int n);void main()int n; scanf("%d",

44、&n); hailstones(n); getch(); void hailstones(int n) int i; printf("Hailstones generated by 77:n"); i=0; while(n!=1) printf("%5d",n); i+; if(n%2=0) n=n/2; else n=3*n+1; if(i%6=0) printf("n"); printf("%5dn",n); i+; printf("Number of hailstones generated

45、:%5d",i); 6. 請按要求編寫程序。編程要求:(1) 編寫函數(shù)int twinborn(int m,int n),其功能是判斷整數(shù)m和n是否為孿生質(zhì)數(shù)對(相差為2的兩個質(zhì)數(shù)稱為孿生質(zhì)數(shù)),如是,則函數(shù)返回值,否則返回值。(2) 編寫main函數(shù),求出10,99內(nèi)的所有孿生質(zhì)數(shù)對,使得程序的運行結(jié)果為:11,1317,1929,3141,4359,6171,73#include <stdio.h>int prime(int m)int n; for(n=2;n<=(m/2);n+) if(m%n=0) return 0; return 1; int twinb

46、orn(int m,int n) if(prime(m)+prime(n)=2)return 1;elsereturn 0; void main()int n,m; for(n=10;n<=97;n+) m=n+2; if(twinborn(m,n)=1) printf("%d,%dn",n,m); getch(); 實驗七1用牛頓迭代法求方程3x3-3x2+x-1=0在x0=2附近的實根。要求:用函數(shù)float newtoon(float x)求方程在x附近的根;用函數(shù)float F(float x)求x處的函數(shù)值,用函數(shù)float F1(float x)求f(x)

47、在x處的導(dǎo)數(shù);在主函數(shù)中輸入x0,調(diào)用函數(shù)求得方程的近似根,并輸出結(jié)果。請完善下列程序,使之完成上述功能。并請以注釋的方式在程序的最后給出你在運行該程序時所選用的測試數(shù)據(jù)及在該測試數(shù)據(jù)下的運行結(jié)果。#include <stdio.h>#include <math.h>float F(float x)         return  x*(3*x*(x-1)+1)-1;    float F1(float x)     &#

48、160;     return 9*x*x-6*x+1;    float newtoon(float x)     float f,f1,x0;    do x0=x;         f=F(x0);         f1=F1(x0);     

49、;    x=x0-f/f1;      while(fabs(x-x0)>1e-5);    return x; void main()  float x0;   scanf("%f",&x0);   printf("The result =%.2fn",newtoon(x0);   getch(); 2請設(shè)計程序,用二分法求方程x3+1.1

50、x2+0.9x-1.4=0在(0,1)內(nèi)的近似根。直到|f(x)|<0.0001為止。#include <stdio.h>#include <math.h>float f(float x) return(x*x*x+1.1*x*x+0.9*x-1.4); void main() float m=0,n=1,r; r=(m+n)/2.0; while(1) if(fabs(f(r)<1e-4) break; if(f(m)*f(r)<0) n=r; else m=r; r=(m+n)/2.0; printf("x=%f",r); ge

51、tch(); 3以下程序的功能是從鍵盤上輸入10個整數(shù),并檢測整數(shù)3是否包含在這些數(shù)據(jù)中,若包含3,則顯示出第一個3出現(xiàn)的位置,程序有些錯誤,試改正之。#include <stdio.h>void main() int data10,j=0; while (j<10) scanf("%d",&dataj); j+; for(j=0;j<10;j+) if(dataj=3) printf("3 is in the position of %dn ",j+1); break; if(j=10) printf("not

52、 found!n "); getch();4請編輯調(diào)試下列程序,觀察其運行結(jié)果,理解數(shù)組名作為函數(shù)參數(shù)時的作用。該程序的功能是選出a數(shù)組中下標(biāo)為奇數(shù)的并大于10的項并且輸出滿足條件的個數(shù)。【源程序】#include <stdio.h>int fun(int a,int b) int i,j=0; for(i=0;ai;i+) if(i%2=0)continue ; if(ai>10) bj+=ai; return j;void main() int a10=3,15,32,23,11,4,5,9,b10; int i,x; x=fun(a,b) ; for(i=0;

53、i<x;i+) printf("%dt",bi); printf("n%d",x);實驗八1請編輯調(diào)試下列程序,觀察、理解其運行結(jié)果,并敘述該程序的功能。【源程序】#include <stdio.h>int fun(int a,int b) int i,j=0; for(i=0;ai;i+) if(i%2=0)continue ; if(ai>10) bj+=ai; return j;void main() int a10=3,15,32,23,11,4,5,9,b10; int i=0,x=0; x=fun(a,b) ; for

54、(i=0;i<x;i+) printf("%dt",bi); printf("n%d",x);該程序的功能:選出a數(shù)組中下標(biāo)為奇數(shù)的并大于10的項并且輸出滿足條件的個數(shù)。2以下程序在a數(shù)組中查找與x值相同的元素的所在位置。請完善程序?!驹闯绦颉?include <stdio.h>void main() int a11 ,x , i; printf("Enter 10 Integers:n"); for(i=1; i<=10; i+) scanf("%d",a+i); printf("

55、;Enter x:"); scanf("%d",&x); a0=x; i=10; while(x!=ai) i-; if(i>0)printf("%5d 's position is %4dn",x,i); else printf("%d is not found!n",x); getch();3下列程序是利用插入排序法將n個數(shù)從大到小進行排序,插入排序的算法思想如下:從一個空表開始,將待排序的數(shù)一個接一個插入到已排好序的有序表中(空表視為有序),從而得到一個新的、記錄數(shù)增1的有序表。例如:當(dāng)n=7時,

56、待排序的數(shù)及每一趟有序表的變化情況如下:趟數(shù)有序表剩余待排序數(shù)初始狀態(tài)空49386597761327第1趟49386597761327第2趟49386597761327第3趟65493897761327第4趟97654938761327第5趟97766549381327第6趟97766549381327第7趟97766549382713空請完善下列程序并進行調(diào)試。#include <stdio.h>void main() int a10=10,9,8,7,3,4,5,1,-3,0,i,j,t; sort(a,10); ; printf("nthere is 10 numb

57、er: "); for(i=0; i<=9; i+) printf("%4d",ai); getch();int sort(int a,int n) int i,j,t; for(i=1; i<n; i+) t=ai; j=i-1; while(j>=0)&&(t>aj) aj+1=aj; j-; aj+1=t ; 4請按下列要求設(shè)計程序:(1)請編寫void selsort(int a,int n)函數(shù),其功能是用選擇排序算法對a指向的長度為n的數(shù)組進行從小到大排序。(2)編寫main函數(shù),聲明一個一維數(shù)組并用下列測試數(shù)據(jù)

58、初始化,調(diào)用selsort函數(shù)實現(xiàn)a3到a8元素從小到大排序。測試數(shù)據(jù)為:6 8 9 12 16 -3 90 -9 10 1。#include<stdio.h>void selsort(int a,int n)int i,j,k,t;for(i=3;i<=n-2;i+)k=i;for(j=i+1;j<n;j+)if(ak>aj) k=j;t=ai;ai=ak;ak=t;void main()int a10=6,8,9,12,16,-3,90,-9,10,1,i;selsort(a,9);for(i=0;i<10;i+)printf("%5d&quo

59、t;,ai);getch();5冒泡排序算法的C語言描述如下:void bb_sort(int a,int n) int i,j,t; for(i=0;i<n-1;i+) for(j=0;j<n-i-1;j+) if(aj>aj+1) t=aj; aj=aj+1; aj+1=t; 但這個算法對問題規(guī)模為n的待排序數(shù)組來說要進行n-1趟排序。事實上,對一些測試數(shù)據(jù),如2 1 5 6 7 8 9等7個數(shù)據(jù)來說,利用上述冒泡排序算法,在第2趟排序過程中已沒有元素的交換,亦即排序進行2趟后就已經(jīng)有序。試修改上述冒泡算法,使之在已經(jīng)有序的情況下提前結(jié)束外循環(huán)以提高程序執(zhí)行效率。并利用題4的main函數(shù)調(diào)用改進的函數(shù)實現(xiàn)對待排序數(shù)據(jù)的排序。#include<stdio.h>void bbsort(int a,int n) int i,j,t,flag=1; for(i=0;i<n-1&&flag;i+) flag=0; for(j=0;j<n-i-1;j+) if(aj>aj+1) t=aj; aj=aj+1; aj+1=t; flag=1; void main()int a7=2,1,5,6,7,8

溫馨提示

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

評論

0/150

提交評論