C++的if

if语句使用注意事项:

  1. if 后括号内,写条件,括号后没有分号
  2. if 必须有,else 可有可无, else if 可以没有可以有多个
  3. 如果条件满足,运行接下来大括号包含的一段代码
  4. 如果这段代码只有一句,可以省略大括号,不建议大家这样做
  5. 代码如何运行,与缩进和空白字符没有关系
#include <bits/stdc++.h>
using namespace std;
int x; // 在 main 外的变量叫做全局变量,默认是 0
int main()
{
    if (x > 0)
        cout << "a" << endl; // 条件不满足,不会输出 a
        cout << "b" << endl; // 不在 if 中,会输出 b
    if (x > 0)
    {
        cout << "a" << endl; // 条件不满足,不会输出 a
        cout << "b" << endl; // 条件不满足,不会输出 b
    }
    if (x > 0);
    {
        cout << "a" << endl; // if 后不能加分号,如果加,会认为 if 只包含这个分号
        cout << "b" << endl; // 会输出 a 和 b,因为他们不在 if 中
    }
    if (x > 0)
        cout << "a" << endl;cout << "b" << endl;
    // 即使写在同一行,不会输出 a, 输出 b 的部分不在条件中,会输出 b
    return 0;
}

switch case

else if

判断练习题

abc047_a Fighting over Candies

https://atcoder.jp/contests/abc047/tasks/abc047_a
输入三个数字,问其中有没有两个的和是第三个,输出YesNo

参考代码

题解

abc052_a Two Rectangles

https://atcoder.jp/contests/abc052/tasks/abc052_a
输入 A乘B 和 C乘D 的两个矩形,输出其中较大的面积

参考代码

题解

abc053_a ABC/ARC

https://atcoder.jp/contests/abc053/tasks/abc053_a
输入x,如果x<1200输出ABC,如果x>=1200输出ARC

参考代码

题解

abc055_a Restaurant

https://atcoder.jp/contests/abc055/tasks/abc055_a
800日元一顿饭,每吃15顿可以获得200日元的优惠,问吃n顿花多少钱

参考代码

题解

abc056_a HonestOrDishonest

https://atcoder.jp/contests/abc056/tasks/abc056_a
输入两个字母,相同输出H,不同输出D

参考代码

题解

abc057_a Remaining Time

https://atcoder.jp/contests/abc057/tasks/abc057_a
现在时间是A点整,问B小时之后是几点整,输出一个0到23之间的数字

参考代码

题解

abc059_a Three-letter acronym

https://atcoder.jp/contests/abc059/tasks/abc059_a
输入三个单词,输出三个单词的首字母大写

参考代码

题解

abc072_a Sandglass2

https://atcoder.jp/contests/abc073/tasks/abc072_a
输入X和t,一个沙漏,初始X的沙子,每秒漏1的沙子,问t秒后剩多少沙子

参考代码

题解

abc158_a Station and Bus

https://atcoder.jp/contests/abc158/tasks/abc158_a
输入一个长度为3的字符串,问是不是既有A,又有B

参考代码

题解

abc165_a We Love Golf

https://atcoder.jp/contests/abc165/tasks/abc165_a
输入K, A, B。问A和B之间有没有K的倍数

参考代码

题解

abc167_a Registration

https://atcoder.jp/contests/abc167/tasks/abc167_a
输入两个字符串,问第一个是不是第二个的前缀
用substr

参考代码

string s, t;
cin >> s >> t;
if (t.substr(0, s.size()) == s)
{
	cout << "Yes" << endl;
}
else
{
	cout << "No" << endl;
}

题解

abc176_a Takoyaki

https://atcoder.jp/contests/abc176/tasks/abc176_a
做一次可以做X个零食,需要T的时间。问做N个零食需要多久?
除法上取整

参考代码

题解

abc191_a Vanishing Pitch

https://atcoder.jp/contests/abc191/tasks/abc191_a
输入V,T,S,D,V是速度,D是路程,问时间是否在T和S之间,不在输出Yes,在输出No

参考代码

题解

abc192_a Star

https://atcoder.jp/contests/abc192/tasks/abc192_a
输入x,问至少将x增加多少,才能使得结果是100的倍数,不能不增加。

参考代码

#include <bits/stdc++.h>
using namespace std;
int x;
int main()
{
	cin >> x;
	cout << (100 - x % 100) << endl;
}

题解

abc195_a Health M Death

https://atcoder.jp/contests/abc195/tasks/abc195_a
输入M和H,问H是否是M的倍数

参考代码

题解

abc199_a Square Inequality

https://atcoder.jp/contests/abc199/tasks/abc199_a
输入 A,B,C
判断A*A+B*B<C*C是否成立,输出Yes或No

参考代码

#include <bits/stdc++.h>
using namespace std;
int a, b, c;
int main()
{
	cin >> a >> b >> c;
	cout << (a * a + b * b < c * c ? "Yes" : "No") << endl;
	return 0;
}

题解

abc200_a Century

https://atcoder.jp/contests/abc200/tasks/abc200_a
输入n,问是哪个世纪?

参考代码

#include <bits/stdc++.h>
using namespace std;
int n;
int main()
{
	cin >> n;
	cout << (n + 99) / 100 << endl;
	return 0;
}

题解

abc203_a Chinchirorin

https://atcoder.jp/contests/abc203/tasks/abc203_a
输入三个数字,如果其中有2个相同,输出第三个,如果三个两两不同,输出0。

参考代码

#include <bits/stdc++.h>
using namespace std;
int a, b, c;
int main()
{
	cin >> a >> b >> c;
	if (a == b || b == c || a == c)
	{
		cout << (a ^ b ^ c) << endl;
	}
	else
	{
		cout << 0 << endl;
	}
	return 0;
}

题解

abc204_a Rock-paper-scissors

https://atcoder.jp/contests/abc204/tasks/abc204_a
你和另外2个人玩石头剪刀布,你知道另外2个人出的是什么,你要让结果变为平局。

参考代码

#include <bits/stdc++.h>
using namespace std;
int a, b;
int main()
{
	cin >> a >> b;
	cout << (a == b ? a : 3 - a - b) << endl;
}

题解

abc207_a Repression

https://atcoder.jp/contests/abc207/tasks/abc207_a
输入3个数字A, B, C问其中较大的两个的和是多少?

参考代码

题解

abc209_a Counting

https://atcoder.jp/contests/abc209/tasks/abc209_a
输入A和B,问有多少个数字大于等于A且小于等于B

参考代码

#include <bits/stdc++.h>
using namespace std;
int a, b;
int main()
{
	cin >> a >> b;
	cout << max(b - a + 1, 0) << endl;
	return 0;
}

题解

abc212_a Alloy

https://atcoder.jp/contests/abc212/tasks/abc212_a
输入A和B
如果A大于0且B等于0,输出Gold
如果B大于0且A等于0,输出Silver
如果A和B都大于0,输出Alloy

参考代码

#include <bits/stdc++.h>
using namespace std;
int a, b;
int main()
{
	cin >> a >> b;
	if (b == 0)
	{
		cout << "Gold" << endl;
	}
	else if (a == 0)
	{
		cout << "Silver" << endl;
	}
	else
	{
		cout << "Alloy" << endl;
	}
	return 0;
}

题解

  1. C++的if
  2. switch case
  3. else if
  4. 判断练习题
    1. abc047_a Fighting over Candies
    2. abc052_a Two Rectangles
    3. abc053_a ABC/ARC
    4. abc055_a Restaurant
    5. abc056_a HonestOrDishonest
    6. abc057_a Remaining Time
    7. abc059_a Three-letter acronym
    8. abc072_a Sandglass2
    9. abc158_a Station and Bus
    10. abc165_a We Love Golf
    11. abc167_a Registration
    12. abc176_a Takoyaki
    13. abc191_a Vanishing Pitch
    14. abc192_a Star
    15. abc195_a Health M Death
    16. abc199_a Square Inequality
    17. abc200_a Century
    18. abc203_a Chinchirorin
    19. abc204_a Rock-paper-scissors
    20. abc207_a Repression
    21. abc209_a Counting
    22. abc212_a Alloy