资源描述
三、程序填空题3-32程序阅读:下列程序将输出(5分)。# include int s;int f(int m) static int k=0; for(; k=m; k+) s+; return s;int main(void ) int s=1; s=f(2)+f(2); printf(%d#%d#, s, f(20); return 0;四、函数4-3求m到n之和(10分)int sum(int m, int n)int i;int s=0;for(i=m; i=n; i+)s= s + i;return (s);4-34简单输出整数(10分)void PrintN ( int N )int i;for(i=1; i=N; i+)printf(%dn, i);4-36简单求和(10分)int Sum ( int List, int N )int k;int sum=0;for(k=0; kN; k+)sum += Listk;return sum;4-40简单阶乘计算(10分)int Factorial( const int N )int i;int s=1;if( N 0 )return 0;else if( N = 0)return 1;else for(i=1; i=N; i+)s*=i;return s;4-41统计个位数字(15分)int Count_Digit(const int N, const int D) int x; int count=0; int temp=0; x=N; if(x0); return count; 4-43找两个数中最大者(10分)int max( int a, int b )int max;if(a = b)max = a;elsemax = b;return max;4-44数字金字塔(15分)void pyramid(int n)int i, j, space;for(i=1; i = n; i+)space = n - i;for(j=0; j space; j+)putchar( );for(j=0; j i; j+)printf(%-2d, i); putchar(n);4-45判断奇偶性(10分)int even( int n )if( n0 )n = -n;if( n%2=0 )return 1;elsereturn 0;4-46使用函数求奇数和(15分)int even( int n )if( n0 )n = -n;if( n%2=0 )return 1;elsereturn 0;int OddSum( int List, int N )int k;int oddsum = 0;for(k=0; kN; k+)if(even(Listk)=0)oddsum += Listk;return oddsum;4-47使用函数计算两点间的距离(10分)double dist( double x1, double y1, double x2, double y2 )double Dist;Dist = sqrt(x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );return Dist;4-48使用函数求素数和(20分)int prime( int p )int i,j;int count =0;for(j=2;j0&p!=1)return 1; elsereturn 0;int PrimeSum( int m, int n ) int i,sum=0; for(i=m;i=n;i+)if(prime(i)=1)sum=sum+i;return sum;4-19使用函数统计指定数字的个数(15分)int CountDigit( int number, int digit ) int count,temp,x; x=number; if(x0); return count; 4-20使用函数输出水仙花数(20分)int narcissistic(int number) int i, j, digit, power, powerSum, integer, count, flag; count = 0; integer = number; while (integer 0) integer = integer / 10; count+; powerSum = 0; integer = number; for (i = 0; i count; i+) digit = integer % 10; integer = integer / 10; power = 1; for (j = 0; j count; j+) power = power*digit; powerSum = powerSum + power; if (powerSum = number) flag = 1; else flag = 0; return flag;void PrintN(int m, int n) int i; for (i = m + 1; i e)count+;if(count%2!=0) cj=1;for(j=1; j=i; j+) cj=cj*j;d=pow(x,i)/cj;sum=sum+d;i+=2; else cj=1;for(j=1; j=i; j+)cj=cj*j;d=pow(x,i)/cj;sum=sum-d;i+=2;return sum;4-49分类统计字符个数(15分)void f( long int x, char *p ) char q; static int i=0,j=0; if(x0) x=-x; *p=-; p+; q=x%16; if(q0) f(x, p); *(p+(i-j)=q; j-; 4-7使用函数求特殊a串数列和(20分)int fn( int a, int n )int x;int p=0;int i;for(i=0; in; i+)x= a * pow(10,i);p = p + x;return p;int SumA( int a, int n )int k;int sum=0;for(k=0; kn+1; k+)sum = sum + fn( a , k );return sum;4-22使用函数输出指定范围内的完数(20分)void PrintPN( int m, int n )int k;int t;int found = 0;for(k=m; k=n; k+)if (factorsum( k ) = k)found = 1;printf(%d = 1,k); for(t=1; t1 & k%t = 0)printf( + %d, t);printf(n);if (found = 0)printf(No perfect number); 4-25使用函数输出一个整数的逆序数(20分)int reverse( int number )int m=0;if(number 0)m = m*10 + number%10;number=number/10;return m;4-50移动字母(10分)void Shift( char s )int i;int n;char a3;n = strlen(s);for(i=0; i3 )for(i=3; in; i+)si-3 = si;for(i=0; i3; i+)sn-3+i=ai;return s;4-51在数组中查找指定元素(15分)int search(int list, int n, int x) int i, result; for (i=0; i=n)m=m%n;if(m!=0)for(i=n-m; i=0; i-)ai+m=ai;for(i=0; i=A & si=a & si=0 & si=9)digit+;elseother+;printf(%d %d %d %d %d,bletter,sletter,blank,digit,other);4-26使用递归函数计算1到n之和(10分)int sum( int n )int i;int Sum=0;if(n=0)return 0;else for(i=1; i=n; i+)Sum += i;return Sum;4-28递归求阶乘和(15分)double fact( int n )int i;double m=1;if(n=0)return m;elsefor(i=1; i0)for(j=1; j=n; j+)sum += fact(j);return sum;4-29递归实现指数函数(15分)double calc_pow( double x, int n )int i;double m=1;for(i=1; i9)N+; m=m/10;for(i=N-1; i=0; i-)shang = n * pow(10,-i);yushu = n - shang * pow(10 , i);n = yushu;printf(%dn, shang);4-9统计各位数字之和是5的数(20分)int is( int number )int s=0;int yushu = number;while(number 0)yushu = number % 10;s = yushu + s;number = number / 10;if(s = 5)return 1;else return 0;void count_sum( int a, int b )int i;int count=0;int count_Sum=0;for(i=a; i=b; i+)if( is(i) )count+;count_Sum = count_Sum + i;printf(count = %d, sum = %d, count, count_Sum);4-10简单实现x的n次方(10分)double mypow( double x, int n )int i;double s=1;for(i=0; in; i+)s = s*x;return s;4-13使用函数求1到10的阶乘和(10分)double fact( int n )int k;int s=1;for(k=1;k=n; k+)s = s * k;return s;4-14使用函数求最大公约数(10分)int gcd( int x, int y )int i,t;if(x=1;i-)if(x%i=0 & y%i=0)break;return i;4-15使用函数的选择法排序(25分)void sort( int a, int n )int temp,k,min,i;for(k=0;kn-1;k+)min=k;for(i=k+1;in;i+)if(ai p) temp = *p; *p = *end; *end = temp; -end; +p; int my_strlen(char *p) char *q = p; while(*p != 0) p+; return p-q; 4-17递归计算P函数(15分)double P( int n, double x )double z;if(n = 0)z = 1;else if(n = 1)z=x;else if(n 1)z = (2*n-1)*P(n-1,x) - (n-1)*P(n-2,x)/n;return z;4-1计算存款利息-genlib(10分)int main(void) int money, year;double interest, rate;scanf(%d, &money);scanf(%d, &year);scanf(%lf, &rate);interest = money * pow(1+rate,year) - money; printf(interest = %.2fn, interest); return 0;4-2弹球距离(15分)double dist( double h, double p )double d=h;while(p!=0)h=p*h;if(hTOL)break;d=d+2*h;return (d);25借鉴答案类
展开阅读全文