16.7.22

//今日代码
2016-07-22 training

A. I Wanna Be the Guy

(两个人玩游戏各能通过一部分,问1~n是否都能通过。对1~n有出现过的数字标记下,若其中有未被标记的则游戏失败。)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;

int arr[110];

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n;
    cin >> n;
    int p,q;
    cin >> p;
    for(int i = 0; i < p ; i++)
    {
        int a;
        cin >> a;
        arr[a]++;
    }
    cin >> q;
    for(int i = 0; i < q; i++)
    {
        int a;
        cin >> a;
        arr[a]++;
    }

    bool ok = 1;
    for(int i = 1; i <= n;i++)
        if(arr[i] == 0)
        {
            cout << "Oh, my keyboard!" << endl;
            ok = 0;
            break;
        }
    if(ok) cout <<"I become the guy."<< endl;

    return 0;   
}

B. Chat Online

( 前一个人通话时间固定,后一个人通话时间加上给定区间中的数,要能与前一人有交集,问能满足的区间中的数的个数。对于p的通话时间范围中的数全部标记上,然后for一遍区间中的数k加到q的区间上看新的区间范围内是否有被标记的,有则这个k能满足,cnt++)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;

int vis[2020];
bool ok[2020];
int c[1010];
int d[1010];

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int p,q,l,r;
    cin >> p >> q >> l >> r;
    for(int i = 0; i <p ;i++)
    {
        int a,b;
        cin >> a >>b;
        for(int j = a; j <= b; j++)
            vis[j] = 1; 
    } 


    for(int i = 0; i < q; i++)
    {
        cin >> c[i] >> d[i];
    }


    for(int k = l; k <= r; k++)
    {
        for(int i = 0; i < q; i++)
        {
                int ll = c[i]+k;
                int rr = d[i]+k;

                for(int j = ll; j <= rr ;j++)
                {
                    if(vis[j]) 
                        ok[k] = true;
                }
        }
    }

    int cnt = 0;
    for(int i = l; i <= r; i++)
        if(ok[i]) cnt++;

    cout << cnt << endl;

    return 0;   
}

C. 24 Game

(对于给定的数n问用1~n数字四则运算拼凑24。可知小于4的数无论如何不能拼凑。n = 4时,1×(2×3)×4 = 24,n = 5时,1×2×((3+4)+5) = 24,n大于5时后面的数字都可以相减成1相乘不变)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;

void prineven(int n)
{
    int cnt = (n-4)/2;
    cout <<"4 * 3 = 12"<< endl;
    cout <<"12 * 2 = 24" << endl;
    cout <<"24 * 1 = 24" << endl;

    while(n>4)
    {
        cout <<n <<" - "<< n-1<<" = 1"<< endl;
        n-=2;
    }
    while(cnt--)
        cout <<"24 * 1 = 24"<< endl;
}


void prinodd(int n)
{
    int cnt = (n-5)/2;
    cout <<"3 + 4 = 7"<< endl;
    cout <<"7 + 5 = 12"<< endl;
    cout <<"1 * 2 = 2" << endl;
    cout <<"2 * 12 = 24"<< endl;

    while(n>5)
    {
        cout <<n <<" - "<< n-1<<" = 1"<< endl;
        n-=2;
    }
    while(cnt--)
        cout <<"24 * 1 = 24"<< endl;
}

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n;
    cin >> n;

    if(n <= 3) 
    {
        cout <<"NO"<< endl; 
        return 0;
    }

    cout <<"YES" << endl;
    if(n % 2) prinodd(n);
    else prineven(n);

    return 0;   
}

F. inc ARG

(对给定的二进制问+1之后改动的位数。只要找到第一个0出现的位置就是所求改动位数。)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;

char arr[111];

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n;
    cin >>n;
    int count = 0;
    for(int i = 1; i <=n ; i++)
        cin >> arr[i]; 
    for(int i = 1; i <=n; i++)
    {
        if(arr[i] == '0')
        {
            count = i;
            break;
        }
    }

    if(count == 0) count = n;
    cout << count<< endl;

    return 0;   
}

G. Inbox (100500)

(从第一个1的位置走到最后一个1的位置,每走过1则+1,每走过0区域段+1。)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;

int arr[1010];

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n;
    int sum = 0;
    cin >> n;
    for(int i = 0; i <n; i++)
    {
        cin >> arr[i];
        sum+=arr[i];
    }

    if(sum == 0)
    {
        cout << 0 << endl;
        return 0;
    }

    int l = 0,r = 0;
    for(int i = 0; i < n; i++)
        if(arr[i] == 1) 
        {
            l = i;
            break;
        }
    for(int i = n-1; i >= 0; i--)
        if(arr[i] == 1) 
        {
            r = i;
            break;
        }


    bool flag = true;
    int cnt = 0;    
    for(int i = l; i <= r ; i++)
    {
        if(arr[i] == 1) 
        {
            flag = true;
            cnt++;
        }
        else if(flag && arr[i] == 0)
        {
            cnt++;
            flag = false;
        }
    }

    cout << cnt << endl;

    return 0;   
}

H. No to Palindromes!

(求第一个满足比给定字符串字典序大的、其中所有字符都为第1~p个字母、且不能有长度大于等于2的回文子串。做法是dfs,从字符串末尾开始增大字符,增大到超过给定范围则减为最小还小这里用-1,然后进位到前一个字符进行增大操作。每改变一次判断下是否会造成和前2位(由于原串没有回文串所以会形成回文串最多是3位)字符形成回文串,若会则在该位继续增大。若不会则退位。)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define LL long long 
#define INF 0x1f1f1f1f
using namespace std;

int n,p;
string s;

bool check(int num)
{
    if(num >= 1 && s[num] == s[num-1])
        return false;

    if(num >= 2 && s[num] == s[num-2])
        return false;

    return true;
}

bool solve(int num)
{
    while(1)
    {
        cout << s << endl;
        if(num >= n)
            return true;
        if(num < 0)
            return false;

        if(s[num]-'a' == p-1)
        {
            s[num] = 'a'-1;
            num--;
        }
        else
        {
            s[num]='a'+(s[num]-'a'+1)%p;

            if(check(num)) num++;
        }
    }
    return false;
}

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    cin >> n >>p;
    cin >> s;

    if(solve(n-1) == false) 
        cout <<"NO"<< endl;
    else
        cout << s<< endl; 

    return 0;   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值