




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、15s 中的main第 1 題 【程序改錯(cuò)】功能:先將在字符串s 中的字符按逆序存放到t 串中,然后把字符按正序連接到t 串的后面。例如:當(dāng)s中的字符串為:“ ABCDE時(shí),則 t中的字符串應(yīng)為:“EDCBAABCDE*/#include <conio.h>#include <stdio.h>#include <string.h> void fun (char *s, char *t) /*FOUND*/int i;sl = strlen(s);for (i=0; i<sl; i+)/*FOUND*/ti = ssl-i;for (i=0; i<
2、sl; i+) tsl+i = si;/*FOUND*/t2*sl = "0" main()char s100, t100;printf("nPlease enter string s:"); scanf("%s", s);fun(s, t);printf("The result is: %sn", t);答案:1) . int i,sl;2) . ti = ssl-i-1;3) . t2*sl = '0'或 t2*sl = 0;第 2 題 【程序改錯(cuò)】功能:求出以下分?jǐn)?shù)序列的前n 項(xiàng)之和。和值通過
3、函數(shù)值返回函數(shù)。2/1+3/2+5/3+8/5+13/8+21/13 ,例如:若n = 5 ,則應(yīng)輸出:8.391667*/#include <conio.h>#include <stdio.h>/*FOUND*/ fun ( int n ) int a, b, c, k; double s;s = 0.0; a = 2; b = 1;for ( k = 1; k <= n; k+ )/*FOUND*/s = (double)a / b;c = a;a = a + b;b = c;/*FOUND*/return c; main( )int n = 5;printf
4、( "nThe value of function is: %lfn", fun ( n ) );答案:1) . double fun(int n)2) . s = s + (double)a / b;或 s += (double)a / b; 或 s += a /(double)b; 或s=s+a/(double)b;3) . return s;第 3 題 【程序改錯(cuò)】功能:讀入一個(gè)整數(shù)m( 5 Vme 20 ),函數(shù) getarr調(diào)用函數(shù)rnd獲得m個(gè)隨機(jī)整數(shù),函數(shù) sortpb將這m個(gè)隨機(jī)整數(shù)從小到大排序。例如:若輸入整數(shù)7,則應(yīng)輸出:3 10 17 28 32 36
5、 47。*/#include "conio.h"#include <stdio.h> sortpb ( int n, int *a ) /*FOUND*/int i, j, p, tfor ( j = 0; j < n-1 ; j+ )p = j;for ( i = j + 1; i < n ; i + )/*FOUND*/if ( ap > aj ) p = i;/*FOUND*/if ( p = j )t = aj;aj = ap;ap = t; double rnd ( )static t = 29, c = 217, m = 1024,
6、 r = 0;r =( r*t + c )%m; return( ( double )r/m );getarr( int n, int *x )int i;for( i = 1; i <= n; i+, x+ ) *x = ( int )( 50*rnd() ); putarr( int n, int *z )int i;for( i = 1; i <= n; i+, z+ )printf( "%4d", *z );if ( !( i%10 ) ) printf( "n" );printf("n");main()int a
7、a20, n;printf( "nPlease enter an integer number between 5 and 20: " );scanf( "%d", &n );getarr( n, aa );printf( "nnBefore sorting %d numbers:n", n ); putarr( n, aa );sortpb( n, aa );printf( "nAfter sorting %d numbers:n", n ); putarr( n, aa );答案:1) . int i,
8、 j, p, t;2) . if ( ap > ai ) p = i;3) . if ( p != j )第 4 題 【程序改錯(cuò)】功能:以下程序能求出1*1+2*2+n*n<=1000中滿足條件的最大的 n。*/ #include <stdio.h> #include "string.h main()int n,s;/*FOUND*/s=n=0;/*FOUND*/while(s>1000) +n;s+=n*n;/*FOUND*/printf("n=%dn",&n-1);答案:1) . s=n=0;2) . while(s<
9、;=1000)3) . printf("n=%dn",n-1);第 5 題 【程序改錯(cuò)】功能:求出a 所指數(shù)組中最大數(shù)和次最大數(shù)( 規(guī)定最大數(shù)和次最大數(shù)不在 a0 和 a1 中 ) ,依次和a0 、 a1 中的數(shù)對(duì)調(diào)。例如:數(shù)組中原有的數(shù):7、 10、 12、 0、 3、 6、 9、 11、 5、 8,輸出的結(jié)果為:12、11 、7、0、3、6、9、10、5、8。*/#include <conio.h>#include <stdio.h>#define N 20 void fun ( int * a, int n )int k,m1,m2,max1,
10、max2,t;max1=max2= -32768; m1=m2=0;for ( k = 0; k < n; k+ )if ( ak>max1 )max2 = max1; m2 = m1;max1 = ak; m1 = k;/*FOUND*/else if( ak>max1 )max2 = ak; m2 = k; /*FOUND*/t = a0;am1=a0;am1 = t;/*FOUND*/t = a1;am2=a1;am2 = t; main( )int bN=7,10,12,0,3,6,9,11,5,8, n=10, i;for ( i = 0; i<n; i+)p
11、rintf("%d ",bi);printf("n");fun (b, n);for ( i=0; i<n; i+ )printf("%d ",bi);printf("n");答案:1) . else if( ak>max2 )2) . t = a0; a0=am1; am1 = t;3) . t = a1; a1=am2; am2 = t;第 6 題 【程序改錯(cuò)】功能:讀入一個(gè)整數(shù)k(2<k< 10000),打印它的所有質(zhì)因子(即所有為素?cái)?shù)的因子)。例如:若輸入整數(shù):2310,則應(yīng)輸出:2
12、、 3、 5、 7、 11 。請(qǐng)改正程序中的語法錯(cuò)誤,使程序能得出正確的結(jié)果。*/#include "conio.h"#include <stdio.h>/*FOUND*/ isPrime(integer n ) int i, m;m = 1;for ( i = 2; i < n; i+ )/*FOUND*/if ( n%i ) m = 0;break;/*FOUND*/return n ; main( )int j, k;printf( "nPlease enter an integer number between 2 and 10000:
13、" );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");答案:1) . IsPrime(int n)2) . if(!(n%i)3) . return m ;第 7 題 【程序改錯(cuò)】功能:為一維數(shù)組輸入10 個(gè)整數(shù);將
14、其中最小的數(shù)與第一個(gè)數(shù)對(duì)換,將最大的數(shù)與最后一個(gè)數(shù)對(duì)換,輸出數(shù)組元素。*/#include <stdio.h>main()int a10;void input();void output();void max_min();input(a,10);max_min(a,10);output(a,10); void input(int *arr,int n) int *p,i;p=arr;printf("please enter 10 integers:n");for(i=0;i<n;i+)/*FOUND*/scanf("%d",p); vo
15、id max_min(int *arr,int n) int *min,*max,*p,t;min=max=arr;for(p=arr+1;p<arr+n;p+)/*FOUND*/if(*p<*max)max=p;else if(*p<*min) min=p;t=*arr;*arr=*min;*min=t;/*FOUND*/if(max=arr) max=min;t=*(arr+n-1);*(arr+n-1)=*max;*max=t; void output(int *arr,int n) int *p,i;p=arr;printf("The changed arr
16、ay is:n");/*FOUND*/while(i=0;i<n;i+)printf("%3d",*p+);printf("n"); 答案:1) . scanf("%d",p +); scanf("%d",arr+i);2) . if(*p>*max)3) . if( max = arr )4) . for(i=0;i<n;i+) for(i=0;i<=n-1;i+)或 scanf("%d",arri);或 if(*max<*p)或 for(i=0;n&g
17、t;i;i+)或 for(i=0;n-1>=i;i+)或 scanf("%d",p+i);或 for(p=arr;p<arr+n;)或 for(p=arr;p<=arr+n-1;)for(p=arr;arr+n-1>=p;)第 8 題 【程序改錯(cuò)】功能:求出在字符串中最后一次出現(xiàn)的子字符串的地址,通過函數(shù)值返回,在主函數(shù)中輸出從此地址開始的字符串;若未找到,則函數(shù)值為NULL。例如:當(dāng)字符串中的內(nèi)容為:"abcdabfabcdx" , t 中的內(nèi)容為:"ab" 時(shí),輸出結(jié)果應(yīng)是:abcdx。當(dāng)字符串中的內(nèi)容為:
18、"abcdabfabcdx" , t 中的內(nèi)容為:"abd" 時(shí),則程序輸出未找到信息:not found! 。*/#include <conio.h>#include <stdio.h>#include <string.h> char * fun (char *s, char *t ) char *p , *r, *a;/*FOUND*/a = NULL; while ( *s ) p = s;r = t;while ( *r )/*FOUND*/if ( r = p ) r+;p+; elsebreak;/*FOU
19、ND*/if ( *r ='0' ) a = s;s+;return a ; main()char s100, t100, *p;printf("nPlease enter string S :"); scanf("%s", s );printf("nPlease enter substring t :"); scanf("%s", t );p = fun( s, t );if ( p )printf("nThe result is : %sn", p);elseprintf(&
20、quot;nNot found !n" );答案:1) . a = NULL;2) . if ( *r = *p )3) . if ( *r = '0' ) a = s;第 9 題 【程序改錯(cuò)】功能:從m個(gè)學(xué)生的成績(jī)中統(tǒng)計(jì)出高于和等于平均分的學(xué)生人數(shù), 此人數(shù)由函數(shù)值返回。平均分通過形參傳回,輸入學(xué)生成績(jī)時(shí),用-1 結(jié)束輸入,由程序自動(dòng)統(tǒng)計(jì)學(xué)生人數(shù)。例如:若輸入8 名學(xué)生的成績(jī),輸入形式如下:80.5 60 72 90.5 98 51.5 88 64 -1結(jié)果為:The number of students :4Ave = 75.56。*/#include <c
21、onio.h>#include <stdio.h>#define N 20 int fun ( float *s, int n, float *aver ) float av, t ; int count , i;count = 0; t=0.0;for ( i = 0; i < n; i+ ) t += s i ; av = t / n; printf( "ave =%fn",av );for ( i = 0; i < n; i+ )/*FOUND*/if ( s i < av ) count+;/*FOUND*/aver = av;/
22、*FOUND*/return count main()float a, s30, aver;int m = 0;printf ( "nPlease enter marks ( -1 to end):n "); scanf("%f",&a );while( a>0 )sm = a;m+;scanf ( "%f", &a );printf( "nThe number of students : %dn" , fun ( s, m, &aver ); printf( "Ave =
23、%6.2fn",aver );答案:1) . if ( s i >= av ) count+;2) . *aver = av;3) . return count;第 10題【程序改錯(cuò)】功能:根據(jù)以下公式求n值,并作為函數(shù)值返回。Pi=3.140578x + .例如:給指定精度的變量eps 輸入 0.0005 時(shí),應(yīng)當(dāng)輸出7111212312 34 =1 +x+x x + x x233535735 79*/ #include <stdio.h> double fun(double eps) double s,t;int n=1;s=0.0;t=1;/*FOUND*/w
24、hile(t<=eps) s+=t;/*FOUND*/t=n/(2*n+1)*t;n+; /*FOUND*/ return s;main()double x;scanf("%lf",&x);printf("neps=%lf,Pi=%lfnn",x,fun(x);答案:1). while(t>eps)或 while(eps<t) 或 while(t>=eps) 或 while(eps<=t) 或 while(t>eps) 或 while (eps<t) 2). t=t*n/(2*n+1);或 t=n/(2.
25、0*n+1.0)*t;3). return 2 * s ;return(s*2);或 while (t>=eps)或 t=1.0*n/(2*n+1)*t;或 t=1.0*n/(2.0*n+1.0)*t;或 return (2*s);或 while (eps<=t)或 t=n/(2*n+1.0)*t;或 t=n/(2.0*n+1)*t;return(2*s); 或 return (s*2);第 11 題 【程序改錯(cuò)】功能:實(shí)現(xiàn)兩個(gè)字符串的連接。例如:輸入dfdfqe 和 12345 時(shí),則輸出dfdfqe12345.*/#include <stdio.h>main()ch
26、ar s180,s280;void scat(char s1,char s2);gets(s1);gets(s2);scat(s1,s2);puts(s1); void scat (char s1,char s2) int i=0,j=0;/*FOUND*/while(s1i= ='0')i+;/*FOUND*/ while(s2j= ='0') /*FOUND*/s2j=s1i;i+;j+;/*FOUND*/s2j='0'答案:1) . while( s1i != '0' )2) . while( s2j != '0
27、39; )或while(s1i)或while( s1i != 0 )或while(s2j)或while( s2j != 0 )3) . s1i=s2j;4) . s1i='0'或 *(s1+i)='0' 或 s1i=0;第 12題【程序改錯(cuò)】功能:求二分之一的圓面積,函數(shù)通過形參得到圓的半徑,函數(shù)返回二分之一的圓面積。例如:輸入圓的半徑值:19.527 輸出為:s = 598.950017*/#include <stdio.h>#include <conio.h>/*FOUND*/ double fun( r) double s;/*FOUND*/s=1/2*3.14159* r * r;/*FOUND*/return r; main()float x;printf ( "Enter x: ");scanf ( "%f", &x );printf (" s = %fn ", fun ( x ) ); 答案:或 double fun(double r)1) . float fun(float r)或 double fun(float r)2) . s=1.0/2*
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 技術(shù)服務(wù)合同協(xié)議
- 基于農(nóng)業(yè)資源整合的現(xiàn)代農(nóng)業(yè)發(fā)展協(xié)議
- 項(xiàng)目進(jìn)度統(tǒng)計(jì)表-互聯(lián)網(wǎng)項(xiàng)目進(jìn)度監(jiān)控
- 公司前臺(tái)接待與咨詢工作表格
- 以科技創(chuàng)新推動(dòng)農(nóng)業(yè)現(xiàn)代化智能化發(fā)展的路徑探索
- 歷史文物保護(hù)修復(fù)技術(shù)考試試題集
- 山中訪友:六年級(jí)自然美文欣賞教案
- 員工培訓(xùn)與技能評(píng)估記錄表
- 醫(yī)院場(chǎng)地租賃合同
- 中國古詩詞故事的欣賞感受
- 2025年南昌理工學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試題庫完整
- 2025年黑龍江省安全員A證考試題庫附答案
- 2025年公共交通衛(wèi)生提升方案
- 2025屆上海市高三下學(xué)期2月聯(lián)考調(diào)研英語試題【含答案解析】
- 建(構(gòu))筑物消防員初級(jí)技能培訓(xùn)課件
- 2025年潛江市城市建設(shè)發(fā)展集團(tuán)招聘工作人員【52人】高頻重點(diǎn)提升(共500題)附帶答案詳解
- TCALC 003-2023 手術(shù)室患者人文關(guān)懷管理規(guī)范
- 五金公司績(jī)效考核(共22頁)
- 體育課(軍體拳)教案(共43頁)
- 市場(chǎng)營銷費(fèi)用核銷管理制度(共4頁)
- 安徽省第八屆“徽匠”建筑技能大賽砌筑工實(shí)操比賽試題
評(píng)論
0/150
提交評(píng)論