B 题
B题刚开始自己看数据猜到了是和最大公因数有关,所以自己就直接莽了,直接写了个最大公因数就交了,结果WA了。自己又分析了一下发现,这个只是一部分和最大公因数有关,当两个数都为奇数时才是最大公因数,如果两个偶数能化成两个奇数答案也是最大公因数,其他情况都是0。
#include <iostream>
#include <cstdio>
typedef long long ll;
using namespace std;
ll gcd (ll a, ll b){
if (a < b){
ll p = a;
a = b;
b = p;
}
while (b){
ll t = a % b;
a = b;
b = t;
}
return a;
}
int main(){
ll a, b;
cin >> a >> b;
ll t;
if (a == b)
cout << a;
else if (a % 2 == 0 && b % 2 == 0){
if (a / gcd (a, b) % 2 == 0 || b / gcd (a, b) % 2 == 0)
cout << 0;
else
cout << gcd (a, b);
}
else if (a % 2 != 0 && b % 2 != 0)
cout << gcd (a, b);
else
cout << 0;
}
A 题
关于这个A题,就是一个用栈可以解决的问题,在自己写题解时可能因为脑子乱了,自己用scanf读入第一行数据导致空格被读了进去,一直报数组越界,最后才发现是那里出了问题,还有个问题就是自己把判断字母那个地方刚开始放在了前面,这样会导致数组索引是负数而报错,必须放在所有判断后面。
#include <iostream>
#include <cstdio>
#include <string>
#include <stack>
using namespace std;
stack<int> stk;
int a[500];
string tmp;
int main(){
int n, j = 0;
cin >> n;
char t;
for (int i = 0; i < n; i++){
cin >> t;
if (t == 'T')
a[i] = 1;
else if (t == 'F')
a[i] = 0;
}
while (cin >> t){
tmp += t;
}
for (int i = 0; i < tmp.size(); i++){
if (tmp[i] == '*'){
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(a & b);
}
else if (tmp[i] == '+'){
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
stk.push(a | b);
}
else if (tmp[i] == '-'){
int a = stk.top();
stk.pop();
stk.push(!a);
}
else if (a[tmp[i] - 'A'] == 1){
stk.push(1);
}
else if (a[tmp[i] - 'A'] == 0)
stk.push(0);
}
if (stk.top() == 1)
cout << 'T';
else
cout << 'F';
}
D 题
水题一个,用map可以轻松解决。
#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
map<int, int> m;
int a[300];
int main(){
int n;
cin >> n;
for (int i = 0; i < n; i++){
scanf("%d", &a[i]);
m[a[i]] = 1;
}
int max = a[n - 1];
int flag = 1;
for (int i = 1; i <= max; i ++){
if (m[i] == 0){
cout << i << endl;
flag = 0;
}
}
if (flag == 1)
puts("good job");
}
本文概述了博主在编程竞赛中的三个题目解法:B题涉及最大公因数的复杂情况,A题中栈操作的陷阱,以及D题的简单查找问题。通过解析错误和解决方案,展示了如何避免常见误区并提升编程技巧。
4万+

被折叠的 条评论
为什么被折叠?



