本文仅整理C++中易错(不一定难)的题目,有疑问欢迎私聊。题目的答案需要使用编译器运行后自行对照,不便之处,敬请谅解。程序已经测试可以运行。
注意每题都有坑,认真分析。
#include <bits/stdc++.h> using namespace std; main() { int a=12; a+=a-=a*a; cout<<a<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; main() { int i,x=4,y=5; i=++x==5||++y==6; cout<<"x="<<x<<endl<<"y="<<y<<endl<<"i="<<i<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; main() { int i,x=4,y=5; i=x++==5&&y++==6; cout<<"x="<<x<<endl<<"y="<<y<<endl<<"i="<<i<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; main() { int x=1,y=2,z=3; x+=y+=z; cout<<(x<y?y:x)<<endl; cout<<(x<y?x++:y++)<<endl; cout<<x<<","<<y<<endl; cout<<(z+=x>y?x++:y++)<<endl; cout<<y<<","<<z<<endl; x=3;y=z=4; cout<<(z>y&&y==x?1:0)<<endl; cout<<(z>=y&&y>=x)<<endl; return 0; }
本题输入为2473
#include <bits/stdc++.h> using namespace std; main() { char ch; while(cin.get(ch)&&ch!=‘\n‘) switch(ch-‘2‘) {case 0: case 1:cout<<(char)(ch+4); case 2:cout<<(char)(ch+4);break; case 3:cout<<(char)(ch+3); default:cout<<(char)(ch+2);break; } cout<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; main() { int a=10,y=0; do { a+=2;y+=a; cout<<"a="<<a<<",y="<<y<<endl; if(y>50) break; } while(a=14); return 0; }
#include <bits/stdc++.h> using namespace std; main() { int i; for (i=1;i<=5;i++) { if(i%2) cout<<"*"; else continue; cout<<"#"; } cout<<"$\n"; return 0; }
#include <bits/stdc++.h> using namespace std; main() { int k=0;char c=‘A‘; do {switch(c++) {case‘A‘:k++;break; case‘B‘:k--; case‘C‘:k+=2;break; case‘D‘:k=k%2;continue; case‘E‘:k=k*10;break; default:k=k/3; } k++; }while(c<‘G‘); cout<<"k="<<k<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; void fun (int x,int y) { x=x*10; y=y+x; cout<<x<<‘\t‘<<y<<endl; } main() { int a=2,b=3; fun(a+b,a*b); cout<<a<<‘\t‘<<b<<endl; return 0; }
原文:https://www.cnblogs.com/Atsuhiro/p/14843402.html