打印图案序列为3个字符

时间:2014-11-08 10:34:19

标签: php

我想用PHP打印3个字符的模式。像aaa,aab,aac .... zzz。

现在,我正在使用这些PHP代码,但它是随机打印而不是序列。

$i = 1; 
$chars = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

while($i > 0){ $current = $chars[rand(0,25)].$chars[rand(0,25)].$chars[rand(0,25)]; }

4 个答案:

答案 0 :(得分:0)

快速但草率

$i=0;
$j=0;
$k=0;
while($i<26){
  while($j<26){
    while($k<26){
      echo $chars[$i] . $chars[$j] . $chars[$k];
      $k++;
    }
    $j++;
    $k=0;
  }
  $i++;
  $j=0;
  $k=0;
}

答案 1 :(得分:0)

从0到26 ^ 3 -1计数。然后将您的数字转换为基数26,根据需要用字母替换数字,如果转换结果是单数字或双数字,则前缀为“a”或“aa”。

使用此函数:string base_convert(string $ number,int $ frombase,int $ tobase)

我不会为你编写全部代码。你需要从中学习。

答案 2 :(得分:0)

我在 C# 中得到了它。

        string sample = "AAA";
        char[] chars = sample.ToCharArray();

        char first = chars[0];
        char second = chars[1];
        char third = chars[2];

        if (first == second && first == third)
        {
            third++;
            bool isAlphaBet = Regex.IsMatch(third.ToString(), "[a-z]", RegexOptions.IgnoreCase);
            if (isAlphaBet)
            {
                return $"{first}{second}{third}";
            }
            return $"empty";
        }
        if (first == second && first != third)
        {
            third++;
            bool isAlphaBet = Regex.IsMatch(third.ToString(), "[a-z]", RegexOptions.IgnoreCase);
            if (isAlphaBet)
            {
                return $"{first}{second}{third}";
            }
            else
            {
                second++;
                third = 'A';

                return $"{first}{second}{third}";
            }
        }//"ABA";
        if (first != second && first == third)
        {
            third++;

            bool isAlphaBet = Regex.IsMatch(third.ToString(), "[a-z]", RegexOptions.IgnoreCase);
            if (isAlphaBet)
            {
                return $"{first}{second}{third}";
            }
            else
            {
                second++;
                third = 'A';

                return $"{first}{second}{third}";
            }
           
        }
        if (first != second && first != third && second != third)
        {
            third++;

            bool isAlphaBet = Regex.IsMatch(third.ToString(), "[a-z]", RegexOptions.IgnoreCase);
            if (isAlphaBet)
            {
                return $"{first}{second}{third}";
            }
            else
            {
                second++;
                third = 'A';
                bool isAlphaBetSecond = Regex.IsMatch(second.ToString(), "[a-z]", RegexOptions.IgnoreCase);
                if (isAlphaBetSecond)
                {
                    return $"{first}{second}{third}";
                }
                else
                {
                    second = 'A';
                    first++;
                    bool isAlphaBetFirst = Regex.IsMatch(first.ToString(), "[a-z]", RegexOptions.IgnoreCase);
                    if (isAlphaBetFirst)
                    {
                        return $"{first}{second}{third}";
                    }
                    else
                    {
                        return $"empty";
                    }
                }
              
            }

        }
        if (first != second && second == third)
        {
            third++;
            bool isAlphaBetThird = Regex.IsMatch(third.ToString(), "[a-z]", RegexOptions.IgnoreCase);
            if (isAlphaBetThird)
            {
                return $"{first}{second}{third}";
            }
            else
            {
                third = 'A';
                second++;
                bool isAlphaBetSecond = Regex.IsMatch(second.ToString(), "[a-z]", RegexOptions.IgnoreCase);
                if (isAlphaBetSecond)
                {
                    return $"{first}{second}{third}";
                }
                else
                {
                    second = 'A';
                    first++;
                    bool isAlphaBetFirst = Regex.IsMatch(first.ToString(), "[a-z]", RegexOptions.IgnoreCase);
                    if (isAlphaBetFirst)
                    {
                        return $"{first}{second}{third}";
                    }
                    else
                    {
                        return $"empty";
                    }
                }
            }
        }

        return $"{first}{second}{third}";

答案 3 :(得分:-1)

如果您需要随机打印 - 例如'aaa'或'vvv'或'ddd' - 尝试使用

$char = $chars[rand(0,25)]; 
echo $current = $char.$char.$char;

如果你想打印'aaa','bbb','ccc',...-使用

for ($i = 0; $i < 26; $i++) {
    $char = $chars[$i]; 
    echo $current = $char.$char.$char;
}