P5712 【深基3.例4】Apples 使用三目运算符if-else

在这里插入图片描述

#include<iostream>
using namespace std;
int main()
{
    int x;cin >> x;
    if(x > 1)
        cout << "Today, I ate " <<x<<" apples.";
    else
    {
        cout << "Today, I ate " <<x<<" apple.";
    }
    
}

大佬

使用三目运算符if-else

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int n;
int main()
{
    cin>>n;
    if(n<=1)printf("Today, I ate %d apple.",n);
    //判断 n==1 或者 n==0 
    else printf("Today, I ate %d apples.",n);
    //否则就加 s
    //可以简写成这样
    //(n<=1)?(printf("Today, I ate %d apple.",n)):(printf("Today, I ate %d apples.",n));
    return 0;
} 
发布了372 篇原创文章 · 获赞 48 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/dghcs18/article/details/104309830