1、2-7、用for循环计算1/302/29+3/28+30/1。 int i; float sum=0; for(i=1;=30;i+) sum+=i/float(31-i);sum=sum;2-8、用循环语句输出如下图形。AAAAAAA AAAAA AAA A int i,j,k; for(i=0;=3;i+) /控制行 for(j=0;jk-) /控制每行输出的A for(j=3;jj-) for(k=0;kan;=n; temp=a+temp*10; sum+=temp;the result is :2-11、输入一行字符,以回车结束,分别统计其中出现的大写英文字母、小写英文字母、数字字符
2、、空格和其他字符出现的次数。 char c; int lletters=0,uletters=0,spaces=0,digits=0,others=0;Please enter the characters:n while(c=cin.get()!=n) if(ca&cZ uletters+; else if(c= spaces+; else if(c09 digits+; else others+;the number of lower case letters:llettersthe number of upper case letters:ulettersthe number of di
3、gits:digitsthe number of spaces:spacesk;the combination number of n and k:comb(n,k);3-2、编写函数计算ex的近似值,在主函数中输入x及精度10-9(要求最后一项小于10-9)。cmathdouble ex(double x,double eps) int i=1; double e=1,t=1; while(fabs(t)eps) t=t*x/i; e=e+t; i+; return(e); double x,eps;Please inout x and eps:xeps;e*x:ex(x,eps);3-3、
4、编写被调函数,求出1000以内 素数,在主函数中调用函数并输出素数,每行输出5个素数。iomanipvoid PrimeNum(int n=1000) int i, j, k, flg=1, num=0; for(i=2; k=sqrt(i); for(j=2;=k; if(i%j=0) flg=0; if(flg=1) coutsetw(5) num+; if(num%5=0) cout flg=1; PrimeNum();3-4、输出1000以内的所有完数。void print(int n) /输出因子=n/2; if(n%i=0) coutvoid wanshu(int n) int a
5、,s=0; a=n/2;=a; if(n%i=0) s+=i; if(s=n) coutn its factors are: print(n);=1000; wanshu(i);3-5、编写函数(递归和非递归)求两个整数的最大公约数和最小公倍数。int gcd(int x,int y) int r; r=x%y; while (r!=0) x=y; y=r; return(y);/int gcd(int a, int b)/ if(a%b=0) / return b; /else / return gcd(b, a%b);/int gbs(int x,int y) int bs,ys; ys=
6、gcd(x,y); bs=x*y/ys; return bs; int x,y,g,bs;Enter two number: n y; g=gcd(x,y); bs=gbs(x,y);the gcd:g0)x%10; rev(x/10); int x;Please input an integer:x;reverse: if(x 1) fun(i/2);i%2;void main() int n;Please input the decimal number:the binary number: fun(n);3-9、编写函数输出图形。(略)3-10、编写min函数模板和max函数模板。#in
7、clude templateT min(T &var1,T &var2) if(var1userInt2;the min is:min(userInt1,userInt2)the max is:max(userInt1,userInt2)userFloat2;min(userFloat1,userFloat2)max(userFloat1,userFloat2)userChar2;min(userChar1,userChar2)max(userChar1,userChar2)3-11、绝对值函数模板。T Abs(const T var) if(var return(-var); return(
8、var); /测试整型量求绝对值 int test1=12,test2=-34;测试整型量:test1=test1ttest1的绝对值为:Abs(test1)test2=test2ttest2的绝对值为:Abs(test2) /测试浮点型量求绝对值 float test3=12.67f,test4=-34.43f;测试浮点型量:test3=test3ttest3的绝对值为:Abs(test3)test4=test4ttest4的绝对值为:Abs(test4)max) max=arrayi; maxid=i; if(arrayimin) min=arrayi; minid=i;the max o
9、f the array is maxthe index of the max is maxidthe min of the array is minthe index of the min is minidstri stri-=32; ch = stri; char str100;Please input a string: cin.getline(str,100); Words(str);str;4-4、编程实现线性查找账号,账号由用户输入,若找到,则输出合法信息,否则输出非法信息。#define arrSize 5int searchList(int list , int numElems
10、, int value) for(int i=0; i numElems; if( value = listi ) return i; return -1;void main ( ) int testsarrSize = 5658845, 4562555, 3852085, 4520125, 5552012; int result, x;请输入账号: result = searchList(tests, arrSize, x); if ( result = -1 ) cout x为非法账号。 endl; else为合法账号。4-5、采用二分查找实现上述程序。void selectionSort
11、(int a , int n) / 选择排序 int i, j, t, minIndex;n-1; minIndex=i; for(j=i+1; j j+) if(ajaminIndex) minIndex=j; if( minIndex != i ) t=aminIndex; aminIndex=ai; ai=t;/二分查找函数,在a数组中查找valueint binarySearch(int a , int numElems, int value) int low=0, mid, hight=numElems-1; while (low=hight) mid=(low+hight)/2;
12、if( value = amid ) / 查找成功 return mid; else if( value max) k=j; max=aij; /第i行第k列为第i行的最大元素 for(m=0, f=1; mp1&f; m+) if(amkmax) f=0; /第i行第k列不是鞍点 if(f) w=1; q1=i; q2=k; return w; int aMN=1, 2, 3, 4, 4, 5, 3, 6, 3, 5, 6, 7; /*2,4,10,6,3,4,5,8,9,1,2,3;*/ int w; w=andian(a,M,N); if(w) 该二维数组存在鞍点:aq1q2其行标为:q1t列标为:q2; else cout该二维数组没有鞍点。4-7、编写函数求一个44二维数组中周边元素的和。int arrSum(int arr4) int i,j,sum=0;4; sum+=arri0; sum+=arri3; for(j=0; sum+=arr0j; sum+=arr3j; sum
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1