C# 编程题:字母序列重复拼接与奇数位置删除
C# 编程题:字母序列重复拼接与奇数位置删除
本题要求使用 C# 编程,将字母序列 'abcdefghijklmnopqrstuvwxyz' 重复拼接 106 次,然后不断删除奇数位置的字母,直到最后剩下一个字母。使用循环或递归实现,并打印最终结果。
使用循环实现:
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/qsgS 著作权归作者所有。请勿转载和采集!