zoukankan      html  css  js  c++  java
  • 清北学堂模拟赛d4t1 a

    分析:大模拟,没什么好说的.我在考场上犯了一个超级低级的错误:while (scanf("%s",s + 1)),导致了死循环,血的教训啊,以后要记住了.

    /*
        1.没有发生改变,check一下
        2.将一位1变成0
        3.添一位
        4.减一位
    */
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int n,len,ss[3010],b[3010];
    char s[3010];
    bool flag = false;
    
    bool check()
    {
        int res = 0;
        for (int i = 1;i <= n; i++)
            if (ss[i] == 1)
                res += i;
            if (res % (n + 1) == 0)
                return true;
            return false;
    }
    
    bool check2()
    {
        int res = 0;
        for (int i = 1; i <= n; i++)
            if (b[i] == 1)
                res += i;
            if (res % (n + 1) == 0)
                return true;
            return false;
    }
    
    void print()
    {
        for (int i = 1; i <= n; i++)
            printf("%d",ss[i]);
        printf("
    ");
    }
    
    void add(int x,int y)
    {
        memcpy(b,ss,sizeof(ss));
        for (int i = n; i > x; i--)
            b[i] = b[i-1];
            b[x] = y;
            if (check2())
            {
                memcpy(ss,b,sizeof(ss));
                flag = 1;
            }
    }
    
    void eras(int x)
    {
        memcpy(b,ss,sizeof(ss));
        for (int i = x; i <= n; i++)
            b[i] = b[i + 1];
        if (check2())
        {
            memcpy(ss,b,sizeof(ss));
            flag = 1;
        }
    }
    
    int main()
    {
        scanf("%d",&n);
        while(scanf("%s",s + 1) != EOF)
        {
            memset(b,0,sizeof(b));
            len = strlen(s + 1);
            flag = false;
            if (abs(n - len) > 1)
            {
                printf("-1
    ");
                continue;
            }
            for (int i = 1; i <= len; i++)
                ss[i] = s[i] - '0';
                if (n == len)
                {
            if (check())  //1
            {
                print();
                continue;
            }
            for (int i = 1; i <= n; i++) //2
            {
                if (ss[i] == 1)
                {
                    ss[i] = 0;
                    if (check())
                    {
                        flag = 1;
                        break;
                    }
                    ss[i] = 1;
                }
            }
            }
            if (flag)
            {
                print();
                continue;
            }
            if (len == n - 1)
            {
            for (int i = 1; i <= n + 1; i++) //3
            for (int j = 0; j <= 1; j++)
            {
                add(i,j); //i位以及以后都要往后挪1
                if (flag)
                break;
            }
            }
            if (flag)
            {
                print();
                continue;
            }
            if (len == n + 1)
            {
            for (int i = 1; i <= n; i++)
            {
                eras(i);
                if (flag)
                break;
            }
            }
            if (flag)
            {
                print();
                continue;
            }
            printf("-1
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    NOIP知识点&&模板整理【更新中】
    qbxt DAY7 T4
    qbxt DAY7 T2
    qbxt DAY 6 T3 柯西不等式和拉格朗日不等式
    qbxt DAY4 T4
    qbxt DAY4 T3
    #98. 表达式计算 杂想
    扫描线入门学习笔记 (主要讲解代码实现)
    学OI要知道的基础知识(咕咕咕)
    主定理学习笔记(总结向)
  • 原文地址:https://www.cnblogs.com/zbtrs/p/7630257.html
Copyright © 2011-2022 走看看