1 条题解
-
0
【深基3.例4】Apples
解题思路
先输出固定句子与数量,x=0 或 1 时使用 apple,x>1 时使用 apples,最后输出句号。
正确性说明
题面特别说明没有吃时把单数句中的 1 换为 0,因此仅 x>1 时增加 s,其余字符按原句输出。
易错点
本题对 0 的表述按题面约定处理;注意空格、逗号与句号。
复杂度
时间 O(1),空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int x; cin >> x; cout << "Today, I ate " << x << " apple" << (x > 1 ? "s" : "") << ".\n"; return 0; }
- 1