给定一个整数N ,任务是生成长度为N的简单、中等和强级别的随机密码。
Easy level Password: Consists of only numbers or letters.
Medium level Password: Consists of Letters as well as numbers.
Strong level Password – Consists of Letters, numbers, and/or special characters.
例子:
Input: N = 5
Output:
Easy level password (only numbers): 98990
Easy password (only letters): tpFEQ
Medium level password: b3bC8
Strong level password: 7`74n
Input: N = 7
Output:
Easy level password (only numbers): 7508730
Easy level password (only letters): mDzKCjN
Medium level password: 4Z05s66
Strong level password: 2384Qu9
方法:
请按照以下步骤解决问题:
- 对于每个密码级别,遍历给定的长度。
- 要生成每个密码,请根据密码级别使用随机数生成随机分配字符和数字。
下面是上述方法的实现:
C
// C program to generate
// password of given length
#include
#include
#include
// Function to generate easy level
// password with numbers
void easylevelpassnumbers(int n)
{
int i;
// Random character generation
// setting the seed as TIME
srand(time(NULL));
printf("Easy level password "
"(only numbers): ");
for (i = 0; i < n; i++) {
// rand() to assign random
// characters in the password
printf("%d", rand() % 10);
}
printf("\n");
}
// Function to generate easy level
// password with letters
void easylevelpassletters(int n)
{
int i, d;
printf("Easy level password"
" (only letters): ");
for (i = 0; i < n; i) {
d = rand() % 123;
if ((d >= 65 && d <= 90)
|| d >= 97) {
printf("%c", (char)d);
i++;
}
}
printf("\n");
}
// Function to generate random
// medium level password
void midlevelpass(int n)
{
int i, d;
printf("Medium level password: ");
for (i = 0; i < n; i++) {
d = rand() % 123;
// Random alphabetic characters
if ((d >= 65 && d <= 90)
|| d >= 97) {
printf("%c", (char)d);
}
else {
// Random digits
printf("%d", d % 10);
}
}
printf("\n");
}
// Function to generate strong
// level password
void stronglevelpass(int n)
{
int i, d;
printf("Strong level password: ");
for (i = 0; i < n; i++) {
d = rand() % 200;
// Random special characters
if (d >= 33 && d <= 123) {
printf("%c", (char)d);
}
else {
// Random digits
printf("%d", d % 10);
}
}
printf("\n");
}
// Driver Code
int main()
{
int n = 5;
easylevelpassnumbers(n);
easylevelpassletters(n);
midlevelpass(n);
stronglevelpass(n);
}
Easy level password (only numbers): 36707
Easy level password (only letters): cQWxF
Medium level password: 56G4w
Strong level password: 83s20
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live