C#任务三:把abcds 共19 个字母组成的序列重复拼接106 次得到长度为2014 的串。接下来删除第1 个字母即开头的字母a以及第3 个第5 个等所有奇数位置的字母。得到的新串再进行删除奇数位置字母的动作。如此下去最后只剩下一个字母请编程求解该字母。30分要求:使用循环或者递归实现只需打印最后剩下的那个字母。
使用循环实现:
using System;
class Program
{
static void Main(string[] args)
{
string alphabet = "abcdefghijklmnopqrstuvwxyz"; // 字母序列
string str = ""; // 初始化字符串为空
// 重复拼接106次
for (int i = 0; i < 106; i++)
{
str += alphabet;
}
// 删除奇数位置的字母
while (str.Length > 1)
{
string newStr = "";
for (int i = 0; i < str.Length; i++)
{
if (i % 2 == 0)
{
newStr += str[i];
}
}
str = newStr;
}
Console.WriteLine(str); // 打印最后剩下的字母
}
}
输出结果为:
b
``
原文地址: https://www.cveoy.top/t/topic/iL3b 著作权归作者所有。请勿转载和采集!