您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 数据通信与网络 > c++编程类习题题库
1.使用库函数sqrt来计算平方根。#includeiostream#includeiomanip#includecmathusingnamespacestd;intmain(){doublex,y;cout请输入一个数x;cinx;if(x!=0)y=sin(x)+(sqrt(x*x+1));elsey=cos(x)-x*x+3*x;cout函数值:setprecision(6)yendl;return0;}2.ASCII写字母。#includeiostreamusingnamespacestd;intmain(){inta='a'-'A';intx;while(cinx){if(x='A'&&x='Z')cout(char)(x+a)endl;elsecout输入的不是大写字母的ASCII码endl;}return0;}3.3x3-4x2-5x+13=0在x=1求精度为10-6。#includeiostream#includecmathusingnamespacestd;intmain(){floatxn,xn1,f,f1;cout请输入x的初值:;cinxn1;do{xn=xn1;f=(3*(xn-4/3)*xn-5)*xn+15;f1=(9*xn-8)*xn-5;xn1=xn-f/f1;}while(fabs(xn1-xn)=1e-6);cout方程的一个根:xn1endl;return0;}4.编写函数fun()cos(x)-x=0的一个实根。xn+1=cos(xn)5.编写函数intf(intx[],intn)20并在以下main函数中进行测试。#includeiostream.hvoidmain(){inta[]={1,4,6,7,3,9,10,30,59,32,48,72,87,13,27,45,64,8,2,6};coutaf(a,20)endl;#includestdafx.h#includeiostreamusingnamespacestd;intf(intx[],intn);intmain(intargc,char*argv[]){inta[20]={1,8,10,3,7,15,28,2,4,22,0,17,25,9,19,6,14,26,20,11};printf(maxis%d\n,f(a,20));return0;}intf(intx[],intn){inti;intmax;max=x[0];for(i=1;in;i++){if(maxx[i]){max=x[i];}}returnmax;return0;}6.将给定的十进制数转换为十六进制数。#includeiostreamusingnamespacestd;intmain(){intx;while(cinx){printf(%XH\n,x);}return0;}7.intf(intM,intN)f()对给定的正整数M和N5x+6y=M的正整数解中x和y都M和N1M和N1x和y2#includeiostreamusingnamespacestd;intf(intM,intN);voidmain(){coutf(500,650)endl;}#includeiostreamusingnamespacestd;intf(intM,intN);voidmain(){coutf(500,650)endl;}intf(intM,intN){intx,y,count=0;if(M1||N1)return-1;for(x=2;x=N/5;x+=2){for(y=2;y=M/6;y+=2){if(5*x+6*y==M){count++;coutx=xy=yendl;}}}return(count?count:-1);}8.intf(intM,intN)M/N代表母鸡的单价/100数都不能为0M和N只要有一个以上参1M和N环实现。#includeiostreamusingnamespacestd;intf(intM,intN);voidmain(){coutf(15,28)endl;}#includeiostreamusingnamespacestd;intf(intM,intN);voidmain(){coutf(30,20)endl;}intf(intM,intN){intx,y,count=0;if(M1||N1)return-1;for(x=1;x=100/M;x++){for(y=1;y=100/N&&(M*x+N*y=100);y++){if(M*x+N*y==100){count++;cout公鸡=x母鸡=yendl;}}}return(count?count:-1);}9.intf(longa[],intn,longx)f()n个元素的一维数组a及某个指定数x查找数组a中是否存在元素值等于x-1。#includeiostreamusingnamespacestd;intf(longa[],intn,longx);voidmain(){longx=7;longa[5]={3,5,2,7,9};coutf(a,5,x)endl;#includeiostreamusingnamespacestd;intf(longa[],intn,longx);voidmain(){longx=7;longa[5]={3,5,2,7,9};coutf(a,5,x)endl;}intf(longa[],intn,longx){intcount=0,pos=-1;for(inti=0;in;i++){if(a[i]==x){pos=i;count++;}}if(count==0)return-1;returnpos;}10.intf(longa[],intn,longx)f()a及某个指定数xa中元素值等于xx的元素所在位置下标。在函数中输出这返回。#includeiostreamusingnamespacestd;intf(longa[],intn,longx);voidmain(){longx=7;longa[5]={3,7,2,7,9};coutf(a,5,x)endl;}#includeiostreamusingnamespacestd;intf(longa[],intn,longx);voidmain(){longx=7;longa[5]={3,5,2,7,9};coutf(a,5,x)endl;}intf(longa[],intn,longx){intcount=0,pos=-1;for(inti=0;in;i++){if(a[i]==x){pos=i;count++;}}coutcountiendl;returncount;}11.intf(longa[],intn,longx)f()n个元素的一维数组a及某个指定数xa中是否存在元素值等于x回-1。#includeiostreamusingnamespacestd;intf(longa[],intn,longx);voidmain(){longx=8;longa[5]={3,7,2,7,9};coutf(a,5,x)endl;#includeiostreamusingnamespacestd;intf(longa[],intn,longx);voidmain(){longx=7;longa[5]={3,5,2,7,9};coutf(a,5,x)endl;}intf(longa[],intn,longx){intcount=0,pos=-1;for(inti=n-1;i=0;i--){if(a[i]==x){pos=i;count++;}}if(count==0)return-1;returnpos;}12.intf(intN)f()的正整数N3x+4y=N”的正整数解。若N不是正1N现。#includeiostreamusingnamespacestd;intf(intN);voidmain(){coutf(160)endl;}#includeiostreamusingnamespacestd;intf(intN);voidmain(){coutf(160)endl;system(pause);}intf(intN){intx,y,count=0;for(x=1;x=N/3;x++){for(y=1;y=N/4;y++){if(3*x+4*y==N){count++;coutx=xy=yendl;}}}return(count?count:-1);}13.intf(intN)其中N1只以上。若N1N循环实现。#includeiostreamusingnamespacestd;intf(intN);voidmain(){coutf(160)endl;}#includeiostreamusingnamespacestd;intf(intN);voidmain(){coutf(160)endl;}intf(intN){intx,y,count=0;for(x=1;x=N/2;x++){for(y=1;y=N/4;y++){if(2*x+4*y==N){count++;cout鸡=x兔=yendl;}}}return(count?count:-1);}14.intf(intM)f()的正整数M5x+6y=M且x不超过100的正整数解。若M1M数中要求使用一重循环。#includeiostreamusingnamespacestd;intf(intM);voidmain(){coutf(500)endl;}#includeiostreamusingnamespacestd;intf(intM);voidmain(){coutf(500)endl;}intf(intM){intx,y,count=0;intt;if(M=0)return-1;for(x=1;x=M/5&&x=100;x++){t=(100-5*x)/6;if(5*x+6*t==M){count++;coutx=xy=tendl;}}return(count?count:-1);}15intf(intM,intN)f()的功能M和N7x+4y=M的正整数解中x是偶数且yM和N1M和N重1x和y2#includeiostreamusingnamespacestd;intf(intM,intN);voidmain(){coutf(500,650)endl;}#includeiostreamusingnamespacestd;intf(intM,intN);voidmain(){coutf(500,650)endl;}intf(intM,intN){intx,y,count=0;if(M1||N1)return-1;for(x=2;x=N/7;x+=2){for(y=1;y=M/4;y+=2){if(7*x+4*y==M){count++;coutx=xy=yendl;}}}return(count?count:-1);}16.intf(charx,intN)f()的功能c和整数Nc代表的符号打印一个N行的图c为'*'且N为5的图案如本题图所示。且函数值返回1。#includeiostreamusingnamespacestd;intf(charx,intN);voidmain(){f('*',5);}#includeio
本文标题:c++编程类习题题库
链接地址:https://www.777doc.com/doc-5053902 .html