1.今天总结了一下常见选择结构if
的题型;
1)判断数正负 (n==0)?(cout<<"zero"):(cout<<"positive"):(cout<<<"negative");
2)输出绝对值 if(N>=0) cout<<N<<endl;
else cout<<-N<<endl;
3)奇偶数判断 if(N%2) cout<<"odd"<<endl; 条件书写优化
else cout<<"even"<<endl;
4)奇偶ASCII值判断 if(ch%2==0) cout<<"NO"<<endl;
5)判断是否是两位数 if(N>=10&&N<=99) cout<<1<<endl;
6)整数大小比较 if(x>y) cout<<">"<<endl;
else if(x<y) cout<<"<"<<endl;
else if(x==y) cout<<"="<<endl;
7)收集瓶盖集大奖 if(x>=10&&y>=20) cout<<1<<endl;
8)判断一个数能否同时被3.5整除 if((n%3==0)&&(n%5)==0)
else cout<<"no"<<endl;
9)有一门课不及格的学生 if((chinese>=60&&math<60)||(chinese<60&&math>=60)
cout<<1<<endl;
else cout<<0<endl;
总结:选择结构if在条件选择上可以适当下些文章提高程序效率如
2.cout<<setiosflags(ios::fixed)<<setprecision(2);
cout<<area<<endl;
等价于
cout<<setiosflags(ios::fixed)<<setprecision(2)<<area<<endl;
3.记海伦公式:area=sqrt(p*(p-a)*(p-b)*(p-c))
p=(a+b+c)/2.0
4.记题目看到 T1049