简体   繁体   English

C#中的连续字母

[英]Sequential letters in C#

I want to generate sequential letters in C#. 我想在C#中生成连续的字母。

For example the first value of a variable would be "a", followed by "b", "c", ... After "z", the range would continue with "aa", "bb", ... 例如,变量的第一个值是“a”,后跟“b”,“c”,......在“z”之后,范围将继续“aa”,“bb”,......

Something like 就像是

i=0;
while(i<40)
{
   Console.WriteLine(i);
   i++;
}

but using letters. 但是用字母。

You can increment a character as you can increment an integer. 您可以递增一个字符,因为您可以递增一个整数。

char c = 'a';

while(c <= 'z')
{
     // Do something with c
     c++;
}

Note, i just whipped this up. 请注意,我只是掀起了这个。 I haven't tried it. 我没试过。

for (int i=0; i<40; ++i)
{
    char digit = (char) (97 + i%26);  // utf/ascii code 97 == 'a'
    Console.WriteLine(new String(digit, i/26 + 1));
}

The String(char c, int n) constructor gives you back a string with the char c repeated n times. String(char c, int n)构造函数返回一个字符串,其中char c重复n次。 From there, all you need is what to repeat ('a' for 0, 26, 52, etc), and how many times to repeat it (1 for 0, 2 for 26, 3 for 52, etc). 从那里,你需要的只是重复('a'代表0,26,52等),重复多少次(1代表0,2代表26,3代表52等)。

Also note, i can be anything (well, any positive number). 还要注意, i可以是任何东西(好吧,任何正数)。 I just looped from 0 to 40 as you were doing. 正如你所做的那样,我只是从0到40循环。 You don't have to work up to it or store intermediate results. 您无需进行操作或存储中间结果。

Since you've added a new homework tag, I'm not going to give you the outright answer, but I'll give you a point in the right direction. 既然你已经添加了一个新的家庭作业标签,我不会给你一个彻头彻尾的答案,但我会给你一个正确的方向。 Like Florian Greinacher said, you can 'count' though ascii characters just like you would numbers (actually, I think you can, I've never done this with C#. Does the strong typing allow it?). 就像Florian Greinacher所说的那样,你可以“算”ascii字符,就像你想要数字一样(实际上,我认为你可以,我从来没有用C#做过这一点。强类型是否允许它?)。

Use Florian's advice, but keep track of how many times you have completed the az loop, and print out more copies of what you need through more iterations. 使用Florian的建议,但要记录完成az循环的次数,并通过更多迭代打印出更多需要的副本。

Do you need to print out things like "ab" and "ba," or just "aa" "aaa" etc? 你需要打印出“ab”和“ba”之类的东西,还是只打印“aa”“aaa”等等?

You can convert an int to a char (like so ). 您可以将int转换为char( 如此 )。
This way you can use a for loop on an int. 这样你就可以在int上使用for循环。
You can use an if statement to check if your int exceeded z's value, and if so subtract z's value from your int, to find out what's the difference, which you can use to write the double chars. 你可以使用if语句来检查你的int是否超过z的值,如果是这样,从你的int中减去z的值,找出差异,你可以用它来写双字符。
I would advice you against looping on a char, since it might get messy once you passed 'z'. 我建议你不要在char上循环,因为一旦你传递'z',它可能会变得混乱。

Psuedo code: Psuedo代码:

Declare a base string
Inialize base string to ""
for each letter in alphabet
    print base_string + letter
    if(letter == last letter)
       base_string = base_string + letter
       letter = first letter

next letter

I would declare an array of 26 characters, where [0] = a, [1] = b, etc. Then, for a given integer, divide by 26, and store the quotient and mod values as variables. 我将声明一个包含26个字符的数组,其中[0] = a,[1] = b等。然后,对于给定的整数,除以26,并将商和mod值存储为变量。 The answer will be the element at position (mod-1), printed out (quotient + 1) times. 答案将是位置(mod-1)的元素,打印出来(商+ 1)次。 This code below isn't perfect; 以下代码并不完美; you'll probably have to use a cast to make the ints play nicely. 你可能不得不使用一个演员来使得整个游戏很好玩。 Also, I'm using the number 92 for the purpose of this example, but you could use any (number)... 此外,我使用数字92作为此示例的目的,但您可以使用任何(数字)...

string[] letters = {'a', 'b', ... };
int number = 92;
int quotient = number / 26; // answer is 3
int mod = number % 26; // answer is 14
string answer = "";

for (int i = 0; i <= quotient; i ++) {
    answer += letters[mod - 1];
}

return answer;

This problem works great with a stack data structure. 这个问题适用于堆栈数据结构。 Every time you increment a letter, you either increment the item on top or change it to an 'a' and then change the next item. 每次递增一个字母时,您可以将项目增加到顶部或将其更改为“a”,然后更改下一个项目。 If you get to the base letter and it is az, you change it to an a and then add an a to the stack. 如果你到达基本字母并且它是az,你将它改为a然后在堆栈中添加a。

I won't give you the code. 我不会给你代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM