2020-05-24 — 习题训练二题解

A - Candies

大意就是 x(1 + .... 2 ^ (k - 1) ) = x * (2 ^ k - 1) = n;

求x

所以 x = n / (2 ^ k - 1);//写个快速幂就可以求解了

#include <iostream>
using namespace std;
//x (1 + ... 2 ^ k - 1) = x * (2 ^ k - 1) = n;
//x = n / (2 ^ k - 1);
inline int q_pow(int a, int b)
{
    int res = 1;
    while(b)
    {
        if(b & 1) res = res * a;
        a = a * a;
        b >>= 1;
    }    
    return res;
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n, x;
        cin >> n;
        int k = 2;
        while(q_pow(2,k) - 1 <= n)
        {
            if(n % (q_pow(2, k) - 1) == 0){
                x = n / (q_pow(2,k) - 1);
                cout << x << endl;
                break;
            }
            k ++;
        }    
    }
}

B - Balanced Array

给定数组n个数,数组前半部分全为偶数, 后半部分全为奇数, 求如果又满足条件的,输出"YES" 和数组,else print "NO";

n 个数就选 1 2 3 4 ... n //选别的也没啥区别

举个例子

n = 8

2 8 4 6 1 9 3 7

n = 10 就不成立了

当n = 10 的时候 n / 2 = 5; 5 个奇数不能等于 5 个偶数,因为 奇数个奇数还是奇数,不可能等于偶数

即n / 2 为偶数时符合题意 

#include <iostream>
using namespace std;
//1 2 3 4 5 6 7 8 9
//2 8 4 6 1 9 3 7
//(n / 2) % 2 
int t, n;
int main()
{
    cin >> t;
    while(t--)
    {
        cin >> n;
        if(n / 2 % 2)
        {
            cout << "NO" << endl;
            continue;
        }
        cout << "YES" << endl;
        int q1 = 2, q2 = n;
        while(q1 < q2)
        {
            cout << q1 << " " << q2 << " ";
            q1 += 2;
            q2 -= 2;
        } 
        q1 = 1, q2 = n + 1;
        while(q1 < q2)
        {
            cout << q1 << " " << q2 << " ";
            q1 += 2;
            q2 -= 2;
        }
        cout << endl;
    }
}

C - Ichihime and Triangle1

给定三个区间 假设为 [a, b], [b, c] [c, d], a <= b <= c <= d, 假设一定能构成三角形, 求三角形三边

 

 

可以先取两个长边,取两个c ,另一个边取b

 

b + c > c , c + c > b // 一定是三角形了

 

#include <iostream>
using namespace std;
long long t, a, b, c, d;
int main()
{
    cin >> t;
    while(t--)
    {
        cin >> a >> b >> c >> d;
        cout << b << " " << c << " " << c << endl;
    }
}

D - Kana and Dragon Quest game

 

 

达拉崩吧遇到了巨龙,他又两个技能(还有两个蓝条), 一技能是让巨龙的血条减半 + 10, 二技能是让巨龙血条 - 10, 已知巨龙血量,问达拉崩吧能否击败巨龙

当巨龙血量下降到 20以下时, 他的一技能就是给巨龙加血,这时只能用二技能磨死它, 先放一技能, 放完再放二技能, 没能量后巨龙还没死,自己就扑该吧

#include <iostream>
using namespace std;
int t, x, n, m;
int main()
{
//n : x = x / 2  + 10    
//m : x = x - 10
    cin >> t;
    while(t--)
    {
        cin >> x >> n >> m;
        while(n && x >= 20){
            x = x/2 + 10;
            n --;
        }
        while(m-- && x > 0)
        {
            x -= 10;
        }
        if(x <= 0) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
}

E - Candies and two sisters

题解:如果n 是奇数,就输出n/2 - 1, n 是偶数 就输出n / 2;

#include <iostream>
using namespace std;
int t, n, res;
int main()
{
    cin >> t;
    while(t--)
    {
        cin >> n;
        
        if(n % 2 == 0) cout << n / 2 - 1 << endl;
        
        else cout << n / 2 << endl;
    }
}

F - Construct the String

大概就是循环输出字符从'a' ... 'xxx'//依次递增,到b次, 总字符为n个

#include <iostream>
using namespace std;
int t, n, a, b;
int main()
{
    cin >> t;
    while(t--)
    {
        cin >> n >> a >> b;
        char j = 0;
        int flag = 0, cnt = 0;
        while(1)
        {
        for(int i = 1;i <= a; ++ i)
        {
            if(j >= b) j = 0;
            char ch = 'a' + j;
            cout << ch;
            j ++;
            cnt ++;
            if(cnt == n){
                flag = 1;
                break;
            }
        }
        if(flag) break;
        }
        cout << endl;
    }
}
//abcabcabc

附个题目链接:https://vjudge.net/contest/375138#overview

posted @ 2020-05-24 21:17  IIlIlIlI  阅读(131)  评论(0编辑  收藏  举报