A. Broken Clock(Codeforces 722A)
思路
解决这个问题有两种方法。
第一种方法。分类讨论。
第二种方法。将所有合法的情况枚举出来放到集合中,然后从集合中查找是否有匹配的情况。
下面的代码采用第一种方法。
代码
#include <bits/stdc++.h>
using namespace std;
int a, h, m;
int main() {
scanf("%d\n%d:%d", &a, &h, &m);
if(a == 12) {
if(h == 0) {
h = 1;
}
else if(h > 12) {
if(h < 20) {
h = 12;
}
else if(h % 10 <= 2) {
h = 10 + h % 10;
}
else {
h = h % 10;
}
}
}
else {
if(h > 23) {
if(h < 30) {
h = 23;
}
else if(h % 10 <= 3) {
h = 20 + h % 10;
}
else {
h = h % 10;
}
}
}
if(m > 59) {
m = m % 10;
}
if(h / 10 == 0) {
putchar('0');
}
printf("%d:", h);
if(m / 10 == 0) {
putchar('0');
}
printf("%d\n", m);
return 0;
}
B. Verse Pattern(Codeforces 722B)
思路
每输入一行字符串就统计该行内有多少个元音字母就能解决该题了。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
bool flag = true;
char b[maxn];
int n, cnt, len, a[maxn];
set <char> s;
int main() {
s.insert('a');
s.insert('e');
s.insert('i');
s.insert('o');
s.insert('u');
s.insert('y');
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
getchar();
for(int i = 0; i < n; i++) {
gets(b);
len = strlen(b);
cnt = 0;
for(int j = 0; j < len; j++) {
if(s.count(b[j])) {
cnt++;
}
}
if(cnt != a[i]) {
flag = false;
}
}
puts(flag ? "YES" : "NO");
return 0;
}
C. Destroying Array(Codeforces 722C)
思路1
我们可以用一系列离散的点来代表被分裂的区间。例如 0,3,n+1 。这三个点代表区间 [1,n] 已经被分割成区间 [1,2] 和区间 [4,n] 。然后我们建立一个映射 data[] ,其中 d