zoukankan      html  css  js  c++  java
  • 猜字母

    把abcd...s共19个字母组成的序列重复拼接106次,得到长度为2014的串。
    接下来删除第1个字母(即开头的字母a),以及第3个,第5个等所有奇数位置的字母。
    得到的新串再进行删除奇数位置字母的动作。如此下去,最后只剩下一个字母,请写出该字母。
    答案是一个小写字母,请通过浏览器提交答案。不要填写任何多余的内容。
     (6分)

    基础方法:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    using namespace std;
    int main()
    {
        char s[20] = "abcdefghijklmnopqrs",t[3000] = "",u[3000];
        for(int i = 0;i < 106;i ++)
            strcat(t,s);
        while(strlen(t) > 1)
        {
            int c = 0;
            for(int i = 0;t[i];i ++)
            {
                if(i % 2)u[c ++] = t[i];
            }
            u[c] = '';
            strcpy(t,u);
        }
        cout<<t;
    }

    再次做这道题,发现,第一删的开头是1,第二次是2,第三次是4,也就是说没词的开头都是2的次方,那么最后剩下1个,也一定是2的多少次方,设这个数为n,n * 2 > 2014,不然不会只剩下一个数,所以n为1024,1024 % 19 = 17.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    using namespace std;
    int main()
    {
        char s[20] = "abcdefghijklmnopqrs";
        int c = 2;
        while(c * 2 < 19 * 106)
        {
            c *= 2;
        }
        c %= 19;
        cout<<s[c - 1];
    }
  • 相关阅读:
    下标处理问题
    C++输入输出流
    gcc和gdb
    B2C、C2C电子商务分析
    转载:java 动态代理学习(Proxy,InvocationHandler)
    Java Web开发中路径问题小结
    64位操作系统IIS降为32 位版本运行处理
    SQL Server 2000/2005 数据库分页
    iBatis简单入门教程
    JAVA中的Class类
  • 原文地址:https://www.cnblogs.com/8023spz/p/8377737.html
Copyright © 2011-2022 走看看