1、正方形数目
#include <iostream> #include<cstdio> using namespace std; int main() { int a, b, cnt; while (scanf_s("%d %d", &a, &b) != EOF) { cnt = 0; while (a && b) { if (a > b) a -= b; else b -= a; cnt++; } printf("%d\n", cnt); } return 0; }
2、a, b 能否经过运算得到 c
#include <iostream> using namespace std; bool judge(long long a, long long b, long long c){ if (a + b == c) return true; if (a * b == c) return true; if (a - b == c || b - a == c) return true; if (b != 0 && a % b == 0 && a / b == c) return true; if (a != 0 && b % a == 0 && b / a == c) return true; return false; } int main(){ long long a, b, c; cin >> a >> b >> c; if(judge(a, b, c)) cout << "YES"; else cout << "NO"; return 0; }
3、实现一个优先队列
#include<iostream> #include<set> #include<string> using namespace std; struct node { int N, P; bool operator<(const node& A)const {return P != A.P ? P > A.P : N < A.N;} }; int main() { int n; set<node>S; scanf("%d", &n); for (int i = 0; i < n; i++) { string op; cin >> op; if (op[0] == 'A') { node tmp; scanf("%d%d", &tmp.N, &tmp.P); S.insert(tmp); } else if (op[0] == 'N') { if (S.size() == 0) printf("-1\n"); else { printf("%d\n", S.begin()->N); S.erase(S.begin()); } } else if (op[0] == 'R') { int ID; scanf("%d", &ID); for (auto it = S.begin(); it != S.end(); it++) { if (it->N == ID) { S.erase(it); break; } } } else if (op[0] == 'C') printf("%d\n", S.size()); } return 0; }