题目分析:
本题要求:
如果 ,输出
positive;
如果 ,输出
zero;
如果 ,输出
negative。
那么 次if判断即可,但是我教大家一个简单的方法,他就是C/C++唯一的三目运算符? :。
(x!=0?(x>0?"positive":"negative"):"zero")大家可以看到,核心代码只有这一行,但是为什么会AC呢,那就由我给大家讲讲? :的语法:
判断?如果正确要做的事:如果错误要做的事是不是很像if()else,那么就来展示一下本题的全部代码。
代码:
#include<iostream>
using namespace std;
int main(){
int x;//定义
cin>>x;//输入
cout<<(x!=0?(x>0?"positive":"negative"):"zero");//输出结果
return 0;
}