當前位置:編程學習大全網 - 編程語言 - 急求:2010 南開100題 上機題 2級C語言

急求:2010 南開100題 上機題 2級C語言

南開二級C語言上機100題

1、 改錯題1

下列給定程序的功能是:讀入壹個整數(2<=k《=10000》,打印它的所有質因子(即所有為素數的因子)。例如,若輸入整數2310,則應輸出2、3、5、7、11。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序:

#include <conio.h>

#include <stdio.h>

*******************************

IsPrime(int n); ——————去掉分號

{int i,m;

m=1;

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

******************************

if !(n%i) ——————if (!(n%i))

{ m=0; break; }

return (m);

}

main()

{ int j,k;

clrscr();

printf("nPlease enter an integer number between 2 and 10000:");scanf("%d",&k);

printf("nnThe prime factor(s) of %d is (are):",k);

for (j=2;j<=k;j++)

if ((!(k%j))&&(IsPrime(j)) printf("n %4d",j);

printf("n");

)

2、 編程題1

m個人的成績存放在score數組中,請編寫函數fun,它的功能是:將低於平均分的人數作為函數值行會,將低於平均分的分數放在below所指的數組中。

例如,但score數組的數據為10、20、30、40、50、60、70、80、90時,函數返回的人數應該時4,below中的數據應為10、20、30、40。

註意:部分源程序給出如下。

請勿改動主函數main和其他函數中的任何內容僅在函數fun的花括號中填入所編寫的若幹語句。

試題程序:

#include <conio.h>

#include <stdio.h>

#include <string.h>

int fun (int score[],int m, int below[])

{

——————int i,k=0;float aver=0;

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

aver+=(score[i]);

aver/=m;

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

if (score[i]<aver)

{below[k]=score[i];

k++;

retern(k);}

}

main()

{int i, n, below[9];

int score [9]={10,20,30,40,50,60,70,80,90};

clrscr();

n=fun(score,9,below);

printf("nBelow the average score are:");

for (i=0;i<n;i++) printf ("%d",below[i]);

}

3、 改錯2

下列給定程序中,函數fun的功能是:逐個比較a、b兩個字符串對應位置中的字符,把ASCII值大或等於的字符壹次存放到c數組中,形成壹個新的字符串。例如,若a中的字符串為aBCDeFgH,b中的字符串為:ABcd,則c中的字符串為:aBcdeFgh。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序

#include <studio.h>

#include <string.h>

****************************************

void; fun(char *p,char *q,char *c)——去分號

***************************************

{int k=1; ——1改為0

***************************************

while(*p!=*q) —— != 改為 ||

***************************************

{if (*p<*q) c[k]=*q; ——小於號改為小於等於

else c[k]=*p;

if (*p) p++;

if (*q) q++;

k++;

}

}

main()

{char a[10]="aBCDeFgh",b[10]="ABcd",c[80]={''};

fun(a,b,c);

printf("The string a:"); puts(a);

printf("The string b:"); puts (b);

printf("The result:"); puts(c);

}

4、 改錯3

下列給定程序中,函數fun的功能是:依次取出字符串中所有數字字符,形成新的字符串,並取代原字符串。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序

#include <stdio.h>

#include <conio.h>

void fun (char *s)

{ int i,j;

for (i=0,j=0;s[i]!='\0';i++)

****************************************

if (s[i]>='0' && s[i]<='9')

s[j]=s[i]; ——————j改為j++

***************************************

s[j]="\0"; ——————s[j]='\0'

}

main()

{char item[80];

clrscr();

printf("\nEnter a string:");gets(item);

printf("\n\nThe string is :\%s\n",item);

fun (item);

printf("\n\nThe string of changing is :\%s\n",item);

}

5、 改錯4

下列給定程序中,函數fun的功能是:分別銅級字符串中大寫字母和小寫字母的個數。例如,給字符串s輸入:AAaaBBb123CCccccd,則應該輸出結果:upper=6,lower=8。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序

#include <conio.h>

#include <stdio.h>

**********************************

void fun (char *s,int a, int b) ————a應為*a,b應為*b

{ while(*s)

{ if (*s>='A' && *s<='Z')

***********************************

a++; ————(*a)++;

if (*s>='a' && *s<='z')

************************************

b++; ————(*b)++;

s++;

}

}

main()

{ char s[100];int upper=0,lower=0;

clrscr();

printf("nPlease a string:");gets(s);

fun(s,&upper, &lower);

printf("n upper=%d lower=%dn",upper,lower);

}

6、 改錯5

假定整數數列中的數不重復,並存放在數組中。下列給定程序中,函數fun的功能是:刪除數列中值為x的元素。n中存放的是數列中元素的個數。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序

#include <stdio.h>

#define N 20

fun (int *a,int n, int x)

{int p=0,i;

a[n]=x;

while (x!=a[p])

p=p+1;

if (p==n) return -1;

else

{for (i=p;i<n;i++)

******************************************

a[i+1]=a[i]; ————a[i]=a[i+1];

return n-1;

}

}

main()

{int w[N]={-3,0,1,5,7,99,10,15,30,90},x,n,i;

n=10;

printf("The original data:n");

for (i=0;i<n;i++) printf("%5d",w[i]);

printf("nInput x (to delete):");scanf("%d",&x);

printf("Delete:%dn",x);

n=fun(w,n,x);

if (n==-1) printf("***Nor be found!***nn");

else

{printf("The data after deleted:n");

for (i=0,i<n;i++) printf("%5d",w[i]);printf("nn");

}

}

7、 改錯6

下列給定程序中,函數fun的功能是:根據整型形參m的值,計算如下公式的值。t=1-1/2×2-1/3×3-…-1/m×m

例如,若m中的值為5,則應輸出:0.536389。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序:

#include <conio.h>

#include <stdio.h>

double fun (int m)

{ double y=1.0;

int i;

***************************************

for (i=2;i<m;i++) ———— “<”改為“<=”

***************************************

y-=1/(i*i); ————“1”改為“1.0”

return(y);

}

main ()

{int n=5;

clrscr();

printf("\nRhe result is %1f\n",fun(n));

}

8、 改錯7

下列給定程序中,函數fun的功能是:用選擇法對數組中的n個元素按從小到大的順序進行排序。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序:

#include <stdio.h>

#define N 20

void fun(int a[],int n)

{ int i,j,t,p;

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

***************************

{p=j ——加上“;”

for (i=j;i,n;i++) ——j改為j+1

if (a[i]<a[p])

*********************************

p=j; ——j改為i

t=a[p];a[p]=a[j];a[j]=t;

}

}

main()

{

int a[N]={9,6,8,3,-1},i,m=5;

printf("排序前的數據:");

for (i=0;i<m;i++) printf("%d",a[i]);printf("\n");

fun(a,m);

printf("排序後的數據:");

for (i=0;i<m;i++) printf("%d",a[i]);printf("\n");

}

9、 改錯8(2004.7.27)

下列給定程序中,函數fun的功能是:在字符串str中找出ASCII碼值最大的字符,將其放在第壹個位置上;並將該字符前的原字符向後順序移動。例如,調用fun函數之前給字符串輸入:ABCDeFGH,調用後字符串中的內容為eABCDFGH。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序:

#include <stdio.h>

fun(char *p)

{ char max,*q;int i=0;

max=p[i];

while(p[i]!=0)

{ if(max<p[i])

{max=p[i];

*******************************

p=q+i; ——改為q=p+i;

}

i++;

}

******************************

while(q<p) ——q<p改為q>p

{*q=*(q-1);

q--;

}

p[0]=max;

}

main()

{char str[80];

printf("Enter a string:");gets(Str);

printf("\nThe original string: ");puts(Str);

fun(str);

printf("\nThe string agter moving:");puts (str);ptintf("\n\n");

}

10、 改錯9(2004.7.27)

下列給定程序中,函數fun的功能是:從n個學生的成績中統計出低於平均分的學生人數,此人數由函數值返回,平均分存放在形參aver所指的存儲單元中。例如,若輸入8名學生的成績:

80.5 60 72 90.5 98 51.5 88 64

則低於平均分的學生人數為4(平均分為:75.5625)。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序:

#include <conio.h>

#include <stdio.h>

#define N 20

int fun (float *s,int n,float *aver)

{float ave,t=0.0;

int count=0,k,i;

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

*******************************

t=s[k]; ——t+=s[k];

ave=t/n;

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

if (s[i]<ave) count++;

******************************

*aver=&ave; ——去掉&

return count;

}

main()

{float s[30],aver;

int m,i;

clrscr();

printf("nPlease enter m:";scanf("%d",&m);

printf("nPlease enter %d mark:n",m);

for (i=0;i<m;i++) scanf("%f",s+i);

printf("nThe number of students:%dn",fun(s,m,&aver));

printf("Ave=%fn",aver);

)

11、 改錯10(2004.8.1)

下列給定程序中,函數fun的功能是:將s所指字符串中出現的t1所指子串全部替換成t2所指子字符串,所形成的新串放在w所指的數組中。在此處,要求t1和t2所指字符串的長度相同。例如,當s所指字符產中的內容為abcdabfab,t1所指子串中的內容為ab,t2所指子串中的內容為99時,結果,在2

所指的數組中內容應為99cd99f99。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序:

#include<conio.h>

#include<stdio.h>

#include<string.h>

int fun (char *s, char *t1,char*t2,char*w)

{

int i; char *p,*r,*a;

strcpy(w,s);

while(*w)

{p=w;r=t1;

********************************************

while(r) ——r改為*r

if(*r==*p) {r++;p++}

else break;

if(*r=='')

{a=w;r=t2;

*******************************************

while(*r){*a=*r;a++;r++} ——改為while(*r){*a=*r;a++;r++;}

w+=strlen(t2);

}

else w++;

}

}

main()

{

char s[100],t1[100],t2[100],w[100];

clrscr();

printf("nPlease enter string S:");scanf("%s",s);

printf("nPlease enter string t1:");scanf("%s",t1);

printf("nPlease enter string t2:");scanf("%s",t2);

if (strlen(t1)==strlen(t2))

{

printf("nThe result is :%sn",w);

}

else printf("Error:strlen(t1)!=strlen(t2)n");

}

12、 改錯11(2004.8.1)

給定程序MODI1.C中,fun函數的功能是:先從鍵盤上輸入壹個3行3列矩陣的各個元素的值,然後輸出主對角線元素之積。

請改正程序中的錯誤,或在橫線處填上適當的內容並把橫線刪除,使它能得出正確的結果。

註意:不要改動main函數,不得增行或刪行,也不得更改程序的結構!

試題程序:

#include <stdio.h>

int fun ()

{

int a [3] [3], sum;

int i, j;

***********************************

_____; ——sum = 1;

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

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

*****************************************

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

}

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

sum=sum*a[i] [i];

printf (〃Sum=%dn〃, sum);

}

main ()

{ fun (); }

13、 改錯12(8.1)

下列程序的功能是:讀入壹個整數 k(2≤k≤10000),打印它的所有質因子(即所有為素數的因子)。例如,若輸入整數:2310,則應輸出:2、3、5、7、11。

請改正程序中的語法錯誤,使程序能得出正確的結果。

註意:不要改動main函數,不得增行或刪行,也不得更改程序的結構!

試題程序:

#include 〃conio.h〃

#include 〃stdio.h〃

************************************************

IsPrime ( int n ); ——IsPrime(int n)

{ int i, m;

m = 1;

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

*********************************************

if !( n%i ) ——if(!(n%i))

{ m=0; break ; }

return ( m );

}

main ()

{ int j, k;

clrscr ();

printf (〃nPease enter an integer number between 2 and 10000:〃); scanf ( 〃%d〃,&k );

printf ( 〃nn The prime factor (s) of % d is ( are ):〃, k );

for ( j = 2; j <= k; j++ )

if ( ( ! k%j ) ) && ( IsPrime ( j ) ) } printf ( 〃n % 4d〃, j );

printf (〃n〃);

}

14、 改錯13(8.4)

下列程序中,fun函數的功能是:根據形參m,計算如下公式的值。

t=1+ 1/2 + 1/3 + 1/4 + … + 1/m

例如,若輸入5,則應輸出2.283333。

請改正程序中的語法錯誤,使程序能得出正確的結果。

註意:不要改動main函數,不得增行或刪行,也不得更改程序的結構!

試題程序:

#include<conio.h>

#include<stdio.h>

double fun(int m)

{

double t=1.0;

int i;

for(i=2;i<=m;i++)

*******************************************

t+=1.0/k; ——k改為i

*********************************************

____________; ——填return t;

}

main()

{

int m;

clrscr();

printf(“nPlease enter 1 integer number:”);

scanf(“%d”,&m);

printf(“nThe result is %1fn”,fun(m));

}

15、 改錯14(8.4)

下列程序中,fun和funx函數的功能是:

用二分法求方程2x×x×x-4x×x+3x-6=0的壹個根,並要求絕對誤差不超過0.001。例如,若給m輸入-100,給n輸入90,則函數求得的壹個根為2.000。

請改正程序中的語法錯誤,使程序能得出正確的結果。

註意:不要改動main函數,不得增行或刪行,也不得更改程序的結構!

試題程序:

#include<stdio.h>

#include<math.h>

double funx(double x)

{

return(2*x*x*x-4*x*x+3*x-6);

}

double fun(double m, double n)

{

**************************************

int r;——改為double r;

r=(m+n)/2;

**********************************

while(fabs(n-m)<0.001) —— "<" 改為" >= "

{if(funx(r)*funx(n)<0 m=r;

else n=r;

r=(m+n)/2;

)

return r;

)

main()

{

double m,n,root;

printf("Enter m n:n");scanf("%1f%1f",&m,&n);

root=fun(m,n);

printf("root=%6.3fn",root);

}

16、 改錯15(8.5)

下列給定程序中,函數fun的功能是:判斷字符ch是否與str所指串中的某個字符相同;若相同,則什麽也不做,若不同,則將器插在串的最後。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序

#include <conio.h>

#include <stdio.h>

#include <string.h>

**********************************

void fun(char str, char ch)------------------------1

{ while(*str && *str!=ch) str++;

**********************************

if (*str==ch)------------------------------------2

{ str[0]=ch;

*********************************

str[1]='0';------------------------------------3

}

}

main()

{char s[81],c;

clrscr();

printf("nPlease enter a string:n");gets(s)

printf("nPlease enter the character to search:");

c=getchar();

fun(s,c);

printf("nThe result is %sn",s);

}

15 題Answer:

1. void fun(char *str, char ch)

2. if (*str=='

17、 改錯16(8.5)

下列給定程序中,函數Creatlink的功能是:創建帶頭結點的單項鏈表,並為各結點數據域賦0到m-1的值。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序

#include <conio.h>

#include <stdio.h>

#include <stdlib.h>

typedef struct aa

{ int data;

struct aa *next;

}NODE;

NODE *Creatlink(int n, int m)

{NODE *h=Null, *p,*s;

int i;

s=(NODE *)malloc(sizeof(NODE));

********************************************

h=p;-------------------------------------1

p->next=NULL;

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

{s=(NODE *)malloc(sizeof(NODE));

********************************************

s->data=rand()%m;-----------------------2

s->next=p->next;

p->next=s;

p=p->next;

}

********************************************

return p;--------------------------------3

}

outlink(NODE *h)

{NODE *p;

p=h->next;

printf("nnTHE LIST:nn HEAD");

while(p)

{printf("->%d",p->data);

p=p->next;

}

printf("n");

}

main()

{ NODE *head;

clrscr();

head=Creatlink(8,22);

outlink(head);

}

16 題Answer:

1. h=p=s;

2. s->data=rand()%(m-1);

3. return h;

18、 改錯17(8.5)

下列給定程序中,函數fun的功能是:計算並輸出k以內最大的10個能被13或17整除的自然數之和。k的值由主函數傳入,若k的值為500,則函數值為4622。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序

#include <conio.h>

#include <stdio.h>

int fun(int k)

{int m=0, mc=0,j;

while ((k>=2)&&(mc<10))

*********************************

{if ((k%13=0)||(k%17=0))-------------------1

{m=m+k;mc++;}

k--;

}

return m;

**********************************

_____________-------------------------------2

main()

{clrscr();

printf("%dn",fun(500));

}

17 題Answer:

1. if ((k%13==0)||(k%17==0))

2. }

19、 改錯18(8.5)

下列給定程序中,函數fun的功能是:實現兩個整數的交換。例如給a和b分別輸入60和65,輸出為:a=65 b=60

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序

#include <conio.h>

#include <stdio.h>

********************************

void fun (int a,b)--------------------------1

{int t;

*******************************

t=b;b=a;a=t;-------------------------------2

}

main()

{int a,b;

clrscr();

printf("Enter a,b:");scanf("%d%d",&a,&b);

fun(&a,&b);

printf("a=%d b=%dn",a,b);

}

18 題Answer:

1. void fun (int *a,int *b)

2. t=*b;*b=*a;*a=t;

20、 改錯19(8.5)

下列給定程序中,函數fun的功能是:從低位開始取出長整型變量s中偶數位上的數,壹次構成壹個新數放在t中。例如,當s中的數為7654321時,t是的數為642。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序

#include <conio.h>

#include <stdio.h>

********************************

void fun (long s, long t)--------------------------1

{long sl=10;

s/=10;

*t=s%10;

****************************

while (s<0)--------------------------------------2

{s=s/100;

*t=s%10*sl+*t;

sl=sl*10;

}

}

main()

{long s,t;

clrscr();

printf("nPlease enter s:");scanf("%ld",&s);

fun(s,&t);

printf("The result is : %ldn",t);

}

19 題Answer:

1. void fun (long s, long *t)

2. while (s>0)

21、 改錯20(8.5)

N個有序整數數列已放在壹堆數組中,下列給定程序中,函數fun的功能是:利用折半查找算法找整數m再數組中的位置。若找到,則返回其下標值;反之,則返回-1。

折半查找的基本算法是:每次查找前先確定數組中待查的範圍:low和high(low<high),然後把m與中間位置(mid)中元素的值進行比較。如果m的值大於中間位置元素中的值,則下壹次的查找範圍放在中間位置之後的元素中;反之,下壹次的查找範圍落在中間位置之前的元素中。直到low>high,查找結束。

請改正程序中的錯誤,使程序能得出正確的結果。

註意,不要改多main函數,不得增行或刪行,也不得更改程序的結構!

試題程序

#include <stdio.h>

#define N 10

************************************

void fun(int a[],int m)--------------------------1

{int low=0,high=N-1,mid;

while(low<=high)

{mid=(low+high)/2;

if (m<a[mid])

high=mid-1;

***************************************

else if (m>=a[mid])----------------------------2

low=mid+1;

else return(mid);

}

return(-1);

}

main()

{int i,a[N]={-3,4,7,9,13,45,67,89,100,180},k,m;

printf("a數組中的數據如下:");

for(i=0;i<N;i++) printf("%d",a[i]);

printf("Enter m:");scanf("%d",&m);

k=fun(a,m);

if(k>=0) printf("m=%d,index=%d\n",m,k);

else printf("Not be found!\n");

}

20 題Answer:

1. int fun(int a[],int m)

2. else if (m>a[mid])

  • 上一篇:Shell編程的實現方法
  • 下一篇:殘疾人成功的故事200字左右
  • copyright 2024編程學習大全網