第1套
1.程式填空題
給定程式中,函式fun的功能是:統計整型變數m中各數字出現的次數,并存放到陣列a中,其中,a[0]存放0出現的次數,a[1]存放1出現的次數,……,a[9]存放9出現的次數,
例如,若m為14579233,則輸出結果為:0,1,1,2,1,1,0,1,0,1,
請在下劃線處填入正確的內容并將下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdio.h> void fun( int m, int a[10]) { int i; for (i=0; i<10; i++) /**********found**********/ __1__ = 0; while (m > 0) { /**********found**********/ i = ___2___; a[i]++; /**********found**********/ m = ___3___; } } int main() { int m, a[10],i; printf("請輸入一個整數 : "); scanf("%d", &m); fun(m, a); for (i=0; i<10; i++) printf("%d,",a[i]); printf("\n"); return 0; }
2.程式修改題
給定程式中,函式fun的功能是:求矩陣(二維陣列)a[N][N]中每行的最小值,結果存放到陣列b中,
例如,程式運行后,輸出結果應為1,5,9,13,
請改正函式fun中指定部位的錯誤,使它能得出正確的結果,
注意:不要改動main函式,不得增行或刪行,也不得更改程式的結構,
#include <stdio.h> #define N 4 void fun(int a[][N], int b[]) { int i, j; for (i=0; i<N; i++) { /**********found**********/ b[i] = a[0][0]; /**********found**********/ for (j=1; j<N-1; j++) /**********found**********/ if ( b[i] < a[i][j] ) b[i] = a[i][j]; } } int main() { int a[N][N]={{1,4,3,2},{8,6,5,7},{11,10,12,9}, {13,16,14,15}},b[N]; int i; fun(a,b); for (i=0; i<N; i++) printf("%d,", b[i]); printf("\n"); return 0; }
3.程式設計題
撰寫函式fun,其功能是:將一組得分中,去掉一個最高分和一個最低分,然后求平均值,并通過函式回傳,函式形參a指向存放得分的陣列,n存放得分個數(n>2),
例如,若輸入9.9 8.5 7.6 8.5 9.3 9.5 8.9 7.8 8.6 8.4十個得分,則輸出結果為:8.687500,
注意:請勿改動主函式main和其他函式中的任何內容,僅在函式fun的花括號中填入你撰寫的若干陳述句,
#include <stdio.h> void NONO(void); double fun(double a[] , int n) { } int main() { double b[10], r; int i; printf("輸入10個分數放入b陣列中 : "); for (i=0; i<10; i++) scanf("%lf",&b[i]); printf("輸入的10個分數是 : "); for (i=0; i<10; i++) printf("%4.1f ",b[i]); printf("\n"); r = fun(b, 10); printf("去掉最高分和最低分后的平均分 : %f\n", r ); NONO(); return 0; } void NONO() {/* 本函式用于打開檔案,輸入資料,呼叫函式,輸出資料,關閉檔案, */ FILE *fp, *wf ; int i, j ; double b[10], r ; fp = fopen("in.dat","r"); wf = fopen("out.dat","w"); for(i = 0 ; i < 10 ; i++) { for(j = 0 ; j < 10 ; j++) fscanf(fp, "%lf ", &b[j]); r = fun(b, 10) ; fprintf(wf, "%f\n", r); } fclose(fp); fclose(wf); }
1.(1)a[i] (2)m%10 (3)m/10 2. b[i] = a[i][0]; for (j=1; j<N; j++) if ( b[i] > a[i][j] ) 3. double fun(double a[] , int n) { double max,min,s; s=max=min=a[0]; int i; for (i=1;i<n;i++) { if (max<a[i]) max=a[i]; if (min>a[i]) min=a[i]; s+=a[i]; } return (s-max-min)/(n-2); }第1套參考答案
第2套
1.程式填空題
圍繞山頂一圈有N個山洞,編號為0、1、2、…、N-1,有一只狐貍和一只兔子在洞中居住,狐貍總想找到兔子并吃掉它,它的尋找方法是先到第一個洞(即編號為0的洞)中找;再隔1個洞,到編號為2的洞中找;再隔2個洞,到編號為5的洞中找;再隔3個洞,到編號為9的洞中找;……,若狐貍找一圈,請為兔子指出所有不安全的洞號,若形參n的值為30時,不安全的洞號是0、2、5、9、14、20、27,
請在下劃線處填入正確的內容并將下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdio.h> #define N 100 void fun(int *a ,int n) { int i, t; for( i=0; i<n; i++ ) /**********found**********/ a[i]=___1___; i=0; /**********found**********/ ___2___=1; while( i<n ) { a[i]= 1; t++; /**********found**********/ i=___3___; } } int main() { int a[N], i, n=30; fun( a, n); for(i=0; i<n; i++) if (a[i]==1) printf("不安全的洞號是 : %d\n",i ); return 0; }
2.程式修改題
函式fun的功能是:統計a所指字串(串中字符全部為字母)中每個字母在字串中出現的次數(統計時不區分大小寫),并將出現次數最高的字母輸出(如果有多個相同,輸出一個即可),例如對于字串dadbcdbabdbdb,對應的輸出應為b或d,
請改正函式fun中指定部位的錯誤,使它能得出正確的結果,
注意:不要改動main函式,不得增行或刪行,也不得更改程式的結構,
#include <stdio.h> #include <string.h> void fun(char a[]) { int b[26], i, n,max; for (i=0; i<26; i++) /**********found**********/ a[i] = 0; n= strlen(a); for (i=0; i<n; i++) if (a[i] >='a' && a[i]<='z') /**********found**********/ b[a[i] - 'A']++; else if (a[i] >='A' && a[i]<='Z') b[a[i] -'A']++; max = 0; for (i=1; i<26; i++) /**********found**********/ if (b[max] > b[i]) max=i; printf("出現次數最多的字符是:%c\n", max + 'a'); } int main( ) { char a[200]; printf("請輸入一個待統計的字串 : "); scanf("%s", a); fun(a); return 0; }
3.程式設計題
撰寫函式fun,其功能是將一個數字字串轉換成與其面值相同的長整型整數,例如,在鍵盤輸入字串2345210,函式回傳長整型數2345210,
注意:請勿改動主函式main和其他函式中的任何內容,僅在函式fun的花括號中填入你撰寫的若干陳述句,
#include <stdio.h> #include <string.h> void NONO(); long fun( char *s ) { } int main() { char s[10]; long r; printf("請輸入一個長度不超過9個字符的數字字串: "); gets(s); r = fun( s ); printf(" r = %ld\n" , r ); NONO(); return 0; } void NONO() {/* 本函式用于打開檔案,輸入資料,呼叫函式,輸出資料,關閉檔案, */ FILE *fp, *wf ; int i; long r; char s[10], *p; fp = fopen("in.dat","r") ; wf = fopen("out.dat","w") ; for(i = 0 ; i < 10 ; i++) { fgets(s, 10, fp); p = strchr(s, '\n'); if (p) *p = 0; r = fun(s); fprintf(wf, "%ld\n", r); } fclose(fp) ; fclose(wf) ; }
1.(1)0 (2)t (3)i+t 2. b[i]=0 b[a[i] - 'a']++; if (b[max] < b[i]) 3. long fun( char *s ) { long num=0; int i; for (i=0;s[i]!='\0';i++) { num=num*10+s[i]-'0'; } return num; }第2套參考答案
第3套
1.程式填空題
給定程式的主函式中,已給出由結構體構成的鏈表結點a、b、c,各結點的資料域中均存入字符,函式fun的功能是:將a、b、c三個結點鏈接成一個單向鏈表,并輸出鏈表結點中的資料,
請在下劃線處填入正確的內容并將下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdio.h> typedef struct list { char data; struct list *next; }Q; void fun(Q *pa, Q *pb, Q *pc) { Q *p; /**********found**********/ pa->next=___1___; pb->next=pc; p=pa; while( p ) { /**********found**********/ printf(" %c",____2_____); /**********found**********/ p=____3____; } printf("\n"); } int main() { Q a, b, c; a.data='E'; b.data=https://www.cnblogs.com/cs-whut/p/'F'; c.data=https://www.cnblogs.com/cs-whut/p/'G'; c.next=NULL; fun( &a, &b, &c ); return 0; }
2.程式修改題
函式fun的功能是:統計s所指一維陣列中0的個數和1的個數,并輸出結果,
請改正函式fun中指定部位的錯誤,使它能得出正確的結果,
注意:不要改動main函式,不得增行或刪行,也不得更改程式的結構,
#include <stdio.h> void fun(int *s, int n) { /**********found**********/ int i, one=0, zero ; for(i=0; i<n; i++) /**********found**********/ switch( s[i] ); { /**********found**********/ case 0 : zero++; case 1 : one ++; } printf( "one : %d zero : %d\n", one, zero); } int main() { int a[20]={1,1,1,0,1,0,0,0,1, 0,0,1,1,0,0,1,0,1,0,0}, n=20; fun( a, n ); return 0; }
3.程式設計題
撰寫函式fun,其功能是將形參s所指字串放入形參a所指的字符陣列中,使a中存放同樣的字串,說明:不得使用系統提供的字串函式,
注意:請勿改動主函式main和其他函式中的任何內容,僅在函式fun的花括號中填入你撰寫的若干陳述句,
#include <stdio.h> #define N 20 void NONO(void); void fun( char *a , char *s) { } int main() { char s1[N], *s2="abcdefghijk"; fun(s1,s2); printf("%s\n", s1); printf("%s\n", s2); NONO(); return 0; } void NONO(void) {/* 本函式用于打開檔案,輸入資料,呼叫函式,輸出資料,關閉檔案, */ FILE *fp, *wf ; int i; char s1[256], s2[256]; fp = fopen("in.dat","r") ; wf = fopen("out.dat","w") ; for (i = 0 ; i < 10 ; i++) { fgets(s2, 255, fp); fun(s1,s2); fprintf(wf, "%s", s1); } fclose(fp) ; fclose(wf) ; }
1.(1)pb (2)p->data (3)p->data 2. int i, one=0, zero=0 ; switch(s[i]) case 0 : zero++; break; 3. void fun( char *a , char *s) { int i; for (i=0;s[i]!='\0';i++) a[i]=s[i]; a[i]='\0'; }第3套參考答案
第4套
1.程式填空題
函式fun的功能是:輸出a所指陣列中得前n個資料,要求每行輸出5個數,
請在下劃線處填入正確的內容并將下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdio.h> #include <stdlib.h> void fun( int *a, int n ) { int i; for(i=0; i<n; i++) { /**********found**********/ if( ___1___==0 ) /**********found**********/ printf("___2___"); /**********found**********/ printf("%d ",___3___); } } int main() { int a[100]={0}, i,n; n=22; for(i=0; i<n;i++) a[i]=rand()%21; fun( a, n); printf("\n"); return 0; }
2.程式修改題
給定程式中,函式fun的功能是:在任意給定的N個正整數中,從左到右依次逐個取三個數作為一組,按值大小找出該組數的中值,用該中值替換與該組數對應的原三個數的中間位置的數,處理后原數列中首尾2個數不變,
例如,有10個正整數如下:
初始數列為:6 5 7 23 18 5 8 21 45 38
第1組數為:6 5 7,中值為6,替換后數列為:6 6 7 23 18 5 8 21 45 38
第2組數為:5 7 23,中值為7,替換后數列為:6 6 7 23 18 5 8 21 45 38
第3組數為:7 23 18,中值為18,替換后數列為:6 6 7 18 18 5 8 21 45 38
第4組數為:23 18 5,中值為18,替換后數列為:6 6 7 18 18 5 8 21 45 38
第5組數為:18 5 8,中值為8,替換后數列為:6 6 7 18 18 8 8 21 45 38
第6組數為:5 8 21,中值為8,替換后數列為:6 6 7 18 18 8 8 21 45 38
第7組數為:8 21 45,中值為21,替換后數列為:6 6 7 18 18 8 8 21 45 38
第8組數為:21 45 38,中值為38,替換后數列為:6 6 7 18 18 8 8 21 38 38
最終結果為:6 6 7 18 18 8 8 21 38 38
請改正函式fun中指定部位的錯誤,使它能得出正確的結果,
注意:不要改動main函式,不得增行或刪行,也不得更改程式的結構,
#include <stdio.h> #define N 10 int findmid(int a, int b, int c) { int t; t = (a>b)?(b>c?b:(a>c?c:a)):((a>c)?a:((b>c)?c:b)); /**********found**********/ return b; } void fun(int x[]) { int i,a,b,c,t[N]; /**********found**********/ for(i=0;i<N;i++) t[i]=x[i] for(i=0;i<N-2;i++) { a=t[i]; b=t[i+1]; c=t[i+2]; /**********found**********/ t[i+1]=findmid(a,b,c); } } int main() { int i, x[N]={6,5,7,23,18,5,8,21,45,38}; for(i=0; i<N; i++) printf("%d ",x[i]); printf("\n"); fun(x); for(i=0; i<N; i++) printf("%d ",x[i]); printf("\n"); return 0; }
3.程式設計題
撰寫函式fun,其功能是:查找x在s所指陣列中下標的位置,并作為函式值回傳,若x不存在,則回傳-1,
注意:請勿改動主函式main和其他函式中的任何內容,僅在函式fun的花括號中填入你撰寫的若干陳述句,
#include <stdio.h> #include <stdlib.h> #define N 15 void NONO(void); int fun(int *s, int x) { } int main() { int a[N]={29,13,5,22,10,9,3,18,22, 25,14,15,2,7,27},i,x,index; printf("a陣列中的資料 :\n"); for(i=0; i<N; i++) printf("%4d",a[i]); printf("\n"); printf("給x輸入待查找的數 : "); scanf("%d",&x); index=fun( a, x ); printf("index=%d\n",index); NONO(); return 0; } void NONO(void) {/* 本函式用于打開檔案,輸入資料,呼叫函式,輸出資料,關閉檔案, */ FILE *fp, *wf ; int i, j, a[10], x, index; fp = fopen("in.dat","r") ; wf = fopen("out.dat","w") ; for(i = 0 ; i < 10 ; i++) { for(j = 0 ; j < 10 ; j++) fscanf(fp, "%d ", &a[j]); fscanf(fp, "%d", &x); index = fun(a, x); fprintf(wf, "%d\n", index); } fclose(fp); fclose(wf); }
1.(1)i%5 (2)\n (3)a[i] 2. return t; for(i=0;i<N;i++) t[i]=x[i]; x[i+1]=findmid(a,b,c); 3. int fun(int *s, int x) { int i; for (i=0;i<N;i++) if (s[i]==x) return i; return -1; }第4套參考答案
第5套
1.程式填空題
給定程式中,函式fun的功能是:在任意給定的9個正整數中找出按升序排列時處于中間的數,將原資料序列中比該中間數小的數用該中間數替換,位置不變,并將中間數作為函式值回傳,在主函式中輸出處理后的資料序列,
例如,有9個正整數:1 5 7 23 87 5 8 21 45
按升序排列時的中間數為:8
處理后主函式中輸出的數列為:8 8 8 23 87 8 8 21 45
請在下劃線處填入正確的內容并將下劃線洗掉,使程式得出正確的結果,
注意:不得增行或刪行,也不得更改程式的結構!
#include <stdio.h> #define N 9 int fun(int x[]) { int i,j,k,t,mid,b[N]; for(i=0;i<N;i++) b[i]=x[i]; for(i=0;i<=N/2;i++) { k=i; for(j=i+1;j<N;j++) if(b[k]>b[j]) k=j; if(k != i ) { /**********found**********/ t=b[i]; b[i]=___1___; b[k]=t; } } /**********found**********/ mid=b[___2___]; for(i=0; i<N; i++) /**********found**********/ if(x[i] ___3___ mid) x[i]=mid; return mid; } int main() { int i, x[N]={1,5,7,23,87,5,8,21,45}; for(i=0; i<N; i++) printf("%d ",x[i]); printf("\nThe mid data is: %d\n",fun(x)); for(i=0; i<N; i++) printf("%d ",x[i]); printf("\n"); return 0; }
2.程式修改題
給定程式中函式fun的功能是:求整數x的y次方的低3位值,例如,整數5的6次方為15625,此值得低3位值為625,
請改正函式fun中指定部位的錯誤,使它能得出正確的結果,
注意:不要改動main函式,不得增行或刪行,也不得更改程式的結構
#include <stdio.h> long fun(int x,int y,long *p ) { int i; long t=1; /**************found**************/ for(i=1; i<y; i++) t=t*x; *p=t; /**************found**************/ t=t/1000; return t; } int main() { long t,r; int x,y; printf("\nInput x and y: "); scanf("%d%d",&x,&y); t=fun(x,y,&r); printf("\n\nx=%d, y=%d, r=%ld, last=%ld\n\n",x, y,r,t ); return 0; }
3.程式設計題
撰寫函式fun,其功能是:統計s所指字串中數字字符個數,并作為函式值回傳,
例如,s所指字串中得內容為:2def35adh25 3kjsdf 7/kj8655x,函式fun回傳值為11,
注意:請勿改動主函式main和其他函式中的任何內容,僅在函式fun的花括號中填入你撰寫的若干陳述句,
#include <stdio.h> void NONO(void); int fun(char *s) { } int main() { char *s="2def35adh25 3kjsdf 7/kj8655x"; printf("%s\n",s); printf("%d\n",fun(s)); NONO(); return 0; } void NONO(void) {/* 本函式用于打開檔案,輸入資料,呼叫函式,輸出資料,關閉檔案, */ FILE *fp, *wf ; int i; char s[256]; fp = fopen("in.dat","r") ; wf = fopen("out.dat","w") ; for(i = 0 ; i < 10 ; i++) { fgets(s, 255, fp); fprintf(wf, "%d\n", fun(s)); } fclose(fp) ; fclose(wf) ; }
1.(1)b[k] (2)N/2 (3)< 2. for(i=1; i<=y; i++) t=t%1000; 3. int fun(char *s) { int n=0,i; for (i=0;s[i]!='\0';i++) if (s[i]>='0' && s[i]<='9') n++; return n; }第5套參考答案
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/527746.html
標籤:C
