您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 招聘面试 > C++17道经典题目
一、//输入3个数,输出最大值#includeiostreamusingnamespacestd;intmain(){doublea,b,c,max;couta,b,c=;cinabc;max=ab?a:b;if(maxc)max=c;coutmax=maxendl;}二、//求两个数最大公约数和最小公倍数;最小公倍数:两个数乘积÷最大公约数最大公约数:辗转相除法#includeiostreamusingnamespacestd;intmain(){inta,b,m,n,c;couta,b=;cinab;m=ab?a:b;n=ab?b:a;a=m;b=n;while(b!=0){c=a%b;a=b;b=c;}coutMaximalCommonDivisorIs:aendl;coutLeastCommonMultipleIs:m*n/aendl;}三、输入一个以”.”结束的字符串统计其中字符、数字、空格和其他符号个数。#includeiostreamusingnamespacestd;intmain(){intchars=0,nums=0,spaces=0,others=0;charch;coutpleaseinputstringsendof'.'endl;cin.get(ch);while(ch!='.'){if(ch='a'&&ch='z'||ch='A'&&ch='Z'){chars++;cin.get(ch);}else{if(ch='0'&&ch='9'){nums++;cin.get(ch);}else{if(ch==''){spaces++;cin.get(ch);}else{others++;cin.get(ch);}}}}coutchars=chars,nums=nums,spaces=spaces,others=othersendl;}四、编写程序显示由符号组成的三角形图案。屏幕显示:Howmanylines?用户输入:5屏幕显示:Whatcharacter?用户输入:*则输出如下图案:*************************#includeiostream#includeiomanipusingnamespacestd;intmain(){intlines,i,j;charchars;coutHowmanylines?endl;cinlines;coutWhatcharacter?endl;cinchars;for(i=1;i=lines;i++){coutsetw(lines-i+1);//setw只对紧挨后面的字符有效,所以应该算一个符号for(j=1;j2*i;j++){coutchars;}coutendl;}system(pause);}五、用牛顿迭代法求平方根:例如:6设x1=6/2;x2=(x1+a/x1)/2如果x1-x2=0.0001则继续计算xn+1=(xn+a/xn)/2;#includeiostream#includecmathusingnamespacestd;intmain(){doublesqrts,x1,x2;coutsqrts=;cinsqrts;x1=sqrts/2;x2=(x1+sqrts/x1)/2;while(fabs(x2-x1)0.0001){x1=x2;x2=(x1+sqrts/x1)/2;}coutx2endl;}六、用牛顿迭代法求解方程f(x)=x3+2x2+10x-20=0在x0=0附近的根。牛顿迭代公式为:xn+1=xn-f(xn)/f’(xn),则f’(xn)=3x2+4x+10#includeiostream#includecmathusingnamespacestd;intmain(){doublex1,x2,epson;coutx=;cinx2;coutepson=;cinepson;do{x1=x2;x2=x1-(pow(x1,3)+2*pow(x1,2)+10*x1-20)/(3*pow(x1,2)+4*x1+10);}while(fabs(x1-x2)epson);coutx2endl;}七、将一个字符串反序输出并拼接在后面:使用string类#includeiostream#includestringusingnamespacestd;intmain(){strings1,s2;intlength;cins1;s2=s1;length=s1.length();couts2;while(length=0){couts1[length-1];length--;}coutendl;}八、输入16进制数转为8进制数(字符串输入、输出)#includeiostream#includestringusingnamespacestd;intmain(){strings1,s2;inti,num;charchars;couthex=;cins1;for(i=0;is1.length();i++){switch(s1[i]){case'0':s2+=0000;break;case'1':s2+=0001;break;case'2':s2+=0010;break;case'3':s2+=0011;break;case'4':s2+=0100;break;case'5':s2+=0101;break;case'6':s2+=0110;break;case'7':s2+=0111;break;case'8':s2+=1000;break;case'9':s2+=1001;break;case'A':case'a':s2+=1010;break;case'b':case'B':s2+=1011;break;case'c':case'C':s2+=1100;break;case'd':case'D':s2+=1101;break;case'e':case'E':s2+=1110;break;case'f':case'F':s2+=1111;break;default:s2=;break;}}if(s2.length()%3==1)s2=00+s2;if(s2.length()%3==2)s2=0+s2;for(i=0;is2.length()-2;i+=3){num=(s2[i]-'0')*4+(s2[i+1]-'0')*2+(s2[i+2]-'0')*1;chars=num+'0';if(i!=0||num!=0)coutchars;}coutendl;}九、输入十进制数,输出八进制数#includeiostream#includeiomanip//控制符头文件setwsetfillhexdecoct等usingnamespacestd;intmain(){intnums;cout.setf(ios::showbase);//输出显示控制符coutnums=;cindecnums;//输入的数看成十进制数coutoctnums=octnumsendl;//以八进制输出}十、输入一个二进制表示的正整数,转换成十进制整数输出#includeiostreamusingnamespacestd;intmain(){intdec=0;charch;coutBinary=;do{cin.get(ch);}while(ch!='0'&&ch!='1');do{dec+=ch-'0';cin.get(ch);if(ch=='0'||ch=='1')dec*=2;}while(ch=='0'||ch=='1');coutDec=decendl;}十一、输出2k+1个数的中位数。#includeiostream#includealgorithm//sort头文件usingnamespacestd;boolcompare(inta,intb){returnab;//升序默认,returnab降序}intmain(){inti,k,totals;coutK=;cink;totals=2*k+1;intnums[totals];for(i=0;itotals;i++)cinnums[i];sort(nums,nums+totals,compare);//nums开始位置,nums+totals结束位置,升序或降序函数coutmiddlenum=nums[totals/2]endl;}补充:快速排序//快速排序voidquick_sort(ints[],intl,intr){if(lr){inti=l,j=r,x=s[l];while(ij){while(ij&&s[j]=x)j--;if(ij){s[i]=s[j];i++;}while(ij&&s[i]x)i++;if(ij){s[j]=s[i];j--;}}s[i]=x;quick_sort(s,l,i-1);quick_sort(s,i+1,r);}}十二、用类模拟一个银行存取款过程#includeiostream#includestringusingnamespacestd;//储户基类classAccount{public:Account(stringi,stringn,doublem);//账号、姓名、钱数voidsaveMoney(doubles);//存钱voidgetoutMoney(doubleg);//取钱voidshowAccountInfo();//查询protected:stringid;//用户账号stringname;//用户姓名doublemoney;//用户余额};Account::Account(stringi,stringn,doublem){id=i;name=n;money=m;}voidAccount::showAccountInfo(){//查询cout姓名:nameendl账号:this-idendl余额:this-moneyendl;}voidAccount::getoutMoney(doubleg){//取钱if(this-moneyg){cout您的余额已不足!endl;}else{this-money=this-money-g;cout取款成功!endl;}}voidAccount::saveMoney(doubles){//存钱this-money=this-money+s;}intmain(){stringid,name;doublewage=100;//默认钱数intchoice;cout请输入账号、姓名endl;cinidname;AccountUser(id,name,wage);cout1.取款endl;cout2.存款endl;cinchoice;cout请输入金额endl;cinwage;if(choice==1){User.getoutMoney(wage);User.showAccountInfo();}if(choice==2){User.saveMoney(wage);User.showAccountInfo();}}十三、求斐波那契数列的前n项。#includeiostreamusingnamespacestd;intmain(){inta1,a2,n,i;a1=0;a2=1;coutn=;cinn;couta1'\t'a2'\t';for(i=1;in/2;i++){a
本文标题:C++17道经典题目
链接地址:https://www.777doc.com/doc-7304988 .html