《C语言编程样题.doc》由会员分享,可在线阅读,更多相关《C语言编程样题.doc(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、例题: -1 (x0)y=-1;if(x!=0)if(x0) y=1;else y=0; 错 不要被对齐迷惑else与if的配对关系y=-1;if(x!=0)if(x0) y=1;else y=0; 对/* binsearch: find x in v0 = v1 = . = vn-1 */ int binsearch(int x, int v, int n) int low, high, mid; low = 0; high = n - 1; while (low = high) mid = (low+high)/2; if (x vmid) low = mid + 1; else /* f
2、ound match */ return mid; return -1; /* no match */ for (expr1;expr2;expr3)statement等价expr1;while(expr2)statementexpr3;循环体由多条语句构成,一定要用花括号括起来,注意空循环不要把循环语句的下一条语句看成是循环体。P69#include /* reverse: reverse string s in place */ void reverse(char s) int c, i, j; for (i = 0, j = strlen(s)-1; i j; i+, j-) c = s
3、i; si = sj; sj = c; 或者for (i = 0, j = strlen(s)-1; i 0 & tk = 0) return i; return -1; #include /* atoi: convert s to integer; version 2 */ int atoi(char s) int i, n, sign; for (i = 0; isspace(si); i+) /* skip white space */ ; sign = (si = -) ? -1 : 1; if (si = + | si = -) /* skip sign */ i+; for (n
4、= 0; isdigit(si); i+) n = 10 * n + (si - 0); return sign * n; int htoi(char s)int i,n;i=0;n=0;if(si=0) /*跳过0x或0X*/i+;if (si=x| si=X) i+;for( ;si!=0;i+)if(si=0&si=a&si=A&si=F)n=n*16+si-A+10; return(n); /* itoa: convert n to characters in s */ void itoa(int n, char s) int i, sign; if (sign = n) 0); /*
5、 delete it */ if (sign 0) si+ = -; si = 0; reverse(s); break与continue的区别例:输出2位数中所有能同时被3和5整除的数。#includestdio.hmain()int n;for(n=10;n100;n+)if(n%3!=0|n%5!=0) continue;printf(%5d,n);运行结果:15 30 45 60 75 90static char daytab213 = 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0, 31, 29, 31, 30, 31, 3
6、0, 31, 31, 30, 31, 30, 31 ; /* day_of_year: set day of year from month & day */ int day_of_year(int year, int month, int day) int i, leap; leap = year%4 = 0 & year%100 != 0 | year%400 = 0; for (i = 1; i daytableapi; i+) yearday -= daytableapi; *pmonth = i; *pday = yearday; 实验:求最大公约数的问题一般用辗转相除法。例如:设m
7、=35,n=15,余数用r表示。它们的最大公约数的求法如下:35/15商2余数为5 以n作m,以r作n,继续相除;15/5 商3余数为0 当余数为0时,所得的n就是两数的最大公约数。#includeint common_divisor(int m,int n)int temp;if(mn)temp=m;m=n;n=temp;while(n!=0)temp=m%n;m=n;n=temp;return m;int common_multiple(int m,int n)return m*n/common_divisor(m,n);void main()int num1,num2;printf(pl
8、ease input two numbers:);scanf(%d%d,&num1,&num2);printf(the common divisor of %d and %d is %dn, num1,num2,common_divisor(num1,num2);printf(and their common multiple is %dn, common_multiple(num1,num2);二分法求方程的根。若函数有实根,则函数的曲线应和x轴有交点,在根附近的左右区间内,函数的值的符号应当相反。利用这一原理,逐步缩小区间的范围,保持在区间的两个端点处函数值的符号相反,就可以逐步逼近函数的
9、根。设f (x)在a, b上连续,且f (a) f (b)0, 找使f (x)=0的点。如下图所示。二分法示意图二分法的步骤如下: 取区间a, b中点x=(a+b)/2。 若f (x)=0, 即(a+b)/2为方程的根。 否则,若f (x)与f (a)同号,则变区间为x,b;异号,则变区间为a,x。 重复各步,直到取到近似根为止。用二分法求下面方程在(-10,10)之间的根。2x3-4x2+3x-6=0#include#includemain()float x0,x1,x2,fx0,fx1,fx2;doprintf(Please input x1,x2: );scanf(%f%f,&x1,&x2);fx1=x1*(2*x1-4)*x1+3)-6;fx2=x2*(2*x2-4)*x2+3)-6;while(fx1*fx20);dox0=(x1+x2)/2;fx0=x0*(2*x0-4)*x0+3)-6;if(fx0*fx1)=1e-6);printf(The root is %6.2fn,x0);