找到一个字符串,使得每个字符在字典上都大于其紧邻的下一个字符
给定一个整数 N,任务是找到一个长度为 (N+1) 的字符串(仅考虑小写字符),使得任何位置的字符在字典顺序上都应该大于其紧邻的下一个字符。
例子:
Input: 2
Output: cba
c is greater than b and
b is greater than a
Input: 5
Output: fedcba
方法:
- 以相反的顺序声明所有字母的字符串。
- 以 26 取给定数字的模数。因此,如果该值小于 26,则运行从26 -(模数值 + 1)到 25 的循环,然后转到字符串的该索引并打印该索引。
- 如果值大于 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
Javascript
输出:
edcbazyxwvutsrqponmlkjihgfedcba