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;
    }
  • 相关阅读:
    Updatepanel 中使用 Timer 控件 失去焦点问题
    获取周一还是周日作为首日
    存储过程
    ASP.NEt ajax 弹出窗口在页面无法关闭
    Gridview 每秒刷新数据
    SharePoint 2013 set site mailbox
    SharePoint 2010 将带有工作流的模板移动到另一个站点集
    Infoapth 使用拼写 并加载web part 在Infopath的页面上
    ECMA 上传文件到SHarePoint 文档库
    数据库如何高效率处理百万以上的查询
  • 原文地址:https://www.cnblogs.com/zbtrs/p/7630257.html
Copyright © 2011-2022 走看看