原文:

给定一个整数 n,任务是找到一个长度为(n 1)的字符串(仅考虑小写字符),使得任意位置的字符在字典上应大于其紧接的下一个字符。 例:

input: 2
output: cba
c is greater than b and
b is greater than a
input: 5
output: fedcba

进场:

  1. 以相反的顺序声明一个包含所有字母的字符串。
  2. 用 26 取给定数的模。因此,如果该值小于 26,运行从26 –(模数值 1) 到 25 的循环,转到字符串的索引并打印该索引。
  3. 如果模数值大于 0,则用 26 除模数值,然后循环到 0 到 25,并根据给定的计算值打印字符串的每个元素。

以下是上述方法的实现:

c

// c   program to print a string in reverse
// alphabetical order upto given number
#include 
using namespace std;
// function that prints the required string
string printstring(int n, string str)
{
    string str2 = "";
    // find modulus with 26
    int extrachar = n % 26;
    // print extra characters required
    if (extrachar >= 1) {
        for (int i = 26 - (extrachar   1); i <= 25; i  )
            str2  = str[i];
    }
    int countofstr = n / 26;
    // print the given reverse string countofstr times
    for (int i = 1; i <= countofstr; i  ) {
        for (int j = 0; j < 26; j  )
            str2  = str[j];
    }
    return str2;
}
// driver code
int main()
{
    int n = 30;
    // initialize a string in reverse order
    string str = "zyxwvutsrqponmlkjihgfedcba";
    cout << printstring(n, str);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print a string in reverse
// alphabetical order upto given number
class gfg {
// function that prints the required string
    static string printstring(int n, string str) {
        string str2 = "";
        // find modulus with 26
        int extrachar = n % 26;
        // print extra characters required
        if (extrachar >= 1) {
            for (int i = 26 - (extrachar   1); i <= 25; i  ) {
                str2  = str.charat(i);
            }
        }
        int countofstr = n / 26;
        // print the given reverse string countofstr times
        for (int i = 1; i <= countofstr; i  ) {
            for (int j = 0; j < 26; j  ) {
                str2  = str.charat(j);
            }
        }
        return str2;
    }
// driver code
    public static void main(string[] args) {
        int n = 30;
        // initialize a string in reverse order
        string str = "zyxwvutsrqponmlkjihgfedcba";
        system.out.println(printstring(n, str));
    }
}
// this code is contributed by rajput-ji

python 3

# python 3 program to print a
# string in reverse alphabetical
# order upto given number
# function that prints the
# required string
def printstring(n, str):
    str2 = ""
    # find modulus with 26
    extrachar = n % 26
    # print extra characters required
    if (extrachar >= 1) :
        for i in range( 26 - (extrachar   1), 26):
            str2  = str[i]
    countofstr = n // 26
    # print the given reverse
    # string countofstr times
    for i in range(1, countofstr   1) :
        for j in range(26):
            str2  = str[j]
    return str2
# driver code
if __name__ == "__main__":
    n = 30
    # initialize a string in
    # reverse order
    str = "zyxwvutsrqponmlkjihgfedcba"
    print(printstring(n, str))
# this code is contributed
# by chitranayal

c

// c# program to print a string in reverse
// alphabetical order upto given number
using system;
public class gfg {
// function that prints the required string
    static string printstring(int n, string str) {
        string str2 = "";
        // find modulus with 26
        int extrachar = n % 26;
        // print extra characters required
        if (extrachar >= 1) {
            for (int i = 26 - (extrachar   1); i <= 25; i  ) {
                str2  = str[i];
            }
        }
        int countofstr = n / 26;
        // print the given reverse string countofstr times
        for (int i = 1; i <= countofstr; i  ) {
            for (int j = 0; j < 26; j  ) {
                str2  = str[j];
            }
        }
        return str2;
    }
// driver code
    public static void main() {
        int n = 30;
        // initialize a string in reverse order
        string str = "zyxwvutsrqponmlkjihgfedcba";
        console.write(printstring(n, str));
    }
}
// this code is contributed by rajput-ji

java 描述语言


输出:

edcbazyxwvutsrqponmlkjihgfedcba

本文由 供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 contribute@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。 如果发现有不正确的地方,或者想分享更多关于上述话题的信息,请写评论。