把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] = '\0'; 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]; }
如果觉得有帮助,点个推荐啦~