赞
踩
直接按题目的意思判断即可
- void solve(){
- int a, b, c;
- cin >> a >> b >> c;
- if (a < b && b < c) {
- cout << "STAIR" << endl;
- }
- else if (a<b && b>c) {
- cout << "PEAK" << endl;
- }
- else {
- cout << "NONE" << endl;
- }
- }
考虑输出的时候交替一下奇偶即可
- void solve(){
- int n;
- cin >> n;
- for (int i = 1; i <= n; i++) {
- if (i & 1) {
- for (int j = 1; j <= n; j++) {
- if (j & 1) {
- cout << "##";
- }
- else cout << "..";
- }
- cout << endl;
- for (int j = 1; j <= n; j++) {
- if (j & 1) {
- cout << "##";
- }
- else cout << "..";
- }
- cout << endl;
- }
- else {
- for (int j = 1; j <= n; j++) {
- if (j & 1) {
- cout << "..";
- }
- else cout << "##";
- }
- cout << endl;
- for (int j = 1; j <= n; j++) {
- if (j & 1) {
- cout << "..";
- }
- else cout << "##";
- }
- cout << endl;
- }
- }
- }
特判一下时钟为00和12的时候,然后分成大于12和小于12两个情况来讨论即可。
- void solve(){
- string s;
- cin >> s;
- string h = "",m="";
- h += s[0];
- h+=s[1];
- m += s[3];
- m+=s[4];
- if (stoi(h) == 12) {
- cout << s << " PM" << endl;
- return;
- }
- if (stoi(h) == 0) {
- cout << 12 << ":" << m << " AM" << endl;
- return;
- }
- if (stoi(h) < 12) {
- cout << s << " AM" << endl;
- return;
- }
- else {
- if(stoi(h)-12>=10) cout << stoi(h) - 12 << ":" << m << " PM" << endl;
- else cout <<0<< stoi(h) - 12 << ":" << m << " PM" << endl;
- }
- }
筛出给定数字的因子,其中因子应该满足是1,0组成的数,然后再从从小到大除以一下因子,最后判断剩下的数是否满足01组成的数即可。
- void solve() {
- int n;
- cin >> n;
- string s = to_string(n);
- bool ok = true;
- for (auto& x : s) {
- if (x != '1' && x != '0') ok = false;
- }
- if (ok) {
- cout << "YES" << endl;
- return;
- }
- auto check = [&](string s) {
- for (auto& x : s) {
- if (x != '1' && x != '0') return false;;
- }
- return true;
- };
- vector<int>b;
- for (int i = 2; i <= sqrt(n); ++i) {
- if (n % i == 0) {
- if (check(to_string(i))) {
- b.push_back(i);
- }
- }
- }
- for (auto& x : b) {
- while (n % x == 0) n /= x;
- }
- if (check(to_string(n))) cout << "YES" << endl;
- else cout << "NO" << endl;
- }
筛选出字符串长度的所有因子,然后枚举因子,用map存下所有的子串大小,判断不同子串的个数,如果子串个数为1,则直接成立。如果子串个数大于2,则直接不成立。如果不同子串个数为2,并且其中一个子串只有一个,那么判断一下不同的两个子串不同的字符个数,若为1,则成立,反之,不成立。
- void solve() {
- int n;
- string s;
- cin >> n >> s;
- s = " " + s;
- set<int>st;
- for (int i = 1; i <= sqrt(n); ++i) {
- if (n % i == 0) {
- st.insert(i);
- st.insert(n / i);
- }
- }
- for (auto& x : st) {
- map<string, int>mp;
- for (int i = 1; i <= n; i += x) {
- mp[s.substr(i, x)]++;
- }
- if (mp.size() == 1) {
- cout << x << endl;
- return;
- }
- else if (mp.size() == 2) {
- string s1 = (*mp.begin()).first;
- string s2 = (*mp.rbegin()).first;
- int cnt = 0;
- for (int i = 0; i < s1.size(); ++i) {
- if (s1[i] != s2[i]) ++cnt;
- if (cnt > 1) {
- break;
- }
- }
- if (cnt == 1 && ((*mp.begin()).second == 1 || (*mp.rbegin()).second == 1)) {
- cout << x << endl;
- return;
- }
- }
- }
- cout << n << endl;
- }
首先先判断一下是否能构成二叉树,构成二叉树的条件是:后代为2的节点*2+后代为1的节点+1=总结点,即:2*a+b+1=a+b+c。如果不成立,直接输出-1,如果成立,则需要构造一颗最小深度的二叉树。如何构造?考虑一棵二叉树节点数量不变的条件下,如果越接近满二叉树,那么这棵树高度越小。所以在构造时尽可能往满二叉树方向构造,即先使用后代为2的点,然后再使用后代为1的点,最后使用后代为0的叶子结点,当叶子结点不够则结束。由于数据a+b+c<2e5,所以可以直接模拟。
- void solve() {
- int a, b, c;
- cin >> a >> b >> c;
- int sum = a + b + c;
- if (2 * a + b + 1 != sum) {
- cout << -1 << endl;
- return;
- }
- queue<int>q;
- if (a) {
- q.push(2);
- --a;
- }
- else {
- if (b) {
- q.push(1);
- --b;
- }
- else {
- cout << 0 << endl;
- return;
- }
- }
- int h = 0;
- while (1) {
- ++h;
- int m = q.size();
- bool ok = true;
- for (int i = 1; i <= m; ++i) {
- int node = q.front(); q.pop();
- if (node == 0) continue;
- else if (node == 2) {
- if (a >= 2) {
- a -= 2;
- q.push(2);q.push(2);
- }
- else if (a == 1) {
- q.push(2);
- --a;
- if (b) {
- q.push(1);
- --b;
- }
- else {
- if (c) {
- q.push(0);
- --c;
- if (c <= 0) ok=false;
- }
- }
- }
- else {
- if (b >= 2) {
- q.push(1); q.push(1);
- b -= 2;
- }
- else if (b == 1) {
- q.push(1); q.push(0);
- --b; --c;
- if (c <= 0) ok=false;
- }
- else {
- q.push(0); q.push(0);
- c -= 2;
- if (c <= 0) ok=false;
- }
- }
- }
- else if (node == 1) {
- if (b >= 1) {
- b --;
- q.push(1);
- }
- else {
- q.push(0);
- c--;
- if (c <= 0) ok=false;
- }
- }
- }
- if (!ok) break;
- }
- cout << h << endl;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。