打印正弦波图案的程序
给出波浪的高度和宽度以打印正弦波模式
例子:
Input : wave_height=5
wave_length=10
Output :
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
推荐:请先在 {IDE} 上尝试您的方法,然后再继续解决。
方法:
首先,检查需要打印元素的行和列。然后,使用嵌套的 for 循环以相应的顺序打印元素。保持单独的循环以跟踪 wave_height 和 wave_length。
C++
// C++ program to print sign wave pattern.
#include
using namespace std;
void printSinWave(int wave_height,
int wave_length)
{
// inner space and outer space.
int is = 1, os = 2;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++)
{
// for loop for wave length
for (int j = 1; j <= wave_length; j++)
{
// intermediate spaces
for (int k = 1; k <= os; k++)
{
cout << " ";
}
// put any symbol
cout << "0";
for (int k = 1; k <= is; k++)
cout << " ";
// put any symbol
cout << "0";
for (int k = 1; k <= os; k++)
cout << " ";
cout << " ";
}
// set a value of os to 1 if i+1 is not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height);
// set value of is to 3 if i+1 is not equal
// to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
cout << "\n";
}
}
// Driver code
int main()
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
return 0;
}
// This code is contributed by shivanisinghss2110
C
// C program to print sign wave pattern.
#include
void printSinWave(int wave_height, int wave_length)
{
// inner space and outer space.
int is = 1, os = 2;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++) {
// for loop for wave length
for (int j = 1; j <= wave_length; j++) {
// intermediate spaces
for (int k = 1; k <= os; k++) {
printf(" ");
}
// put any symbol
printf("0");
for (int k = 1; k <= is; k++)
printf(" ");
// put any symbol
printf("0");
for (int k = 1; k <= os; k++)
printf(" ");
printf(" ");
}
// set a value of os to 1 if i+1 is not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height);
// set value of is to 3 if i+1 is not equal
// to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
printf("\n");
}
}
// Driver code
int main()
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
return 0;
}
Java
// Java program to print
// sign wave pattern.
class GFG
{
static void printSinWave(int wave_height,
int wave_length)
{
// inner space and outer space.
int is = 1, os = 2;
// for loop for height of wave
for (int i = 1;
i <= wave_height; i++)
{
// for loop for wave length
for (int j = 1;
j <= wave_length; j++)
{
// intermediate spaces
for (int k = 1; k <= os; k++)
{
System.out.printf(" ");
}
// put any symbol
System.out.printf("0");
for (int k = 1; k <= is; k++)
System.out.printf(" ");
// put any symbol
System.out.printf("0");
for (int k = 1; k <= os; k++)
System.out.printf(" ");
System.out.printf(" ");
}
// set a value of os to 1 if i+1
// is not equal to wave height
// or 0 otherwise
os = (i + 1 != wave_height) ? 1 : 0;
// set value of is to 3 if i+1 is not
// equal to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
System.out.printf("\n");
}
}
// Driver code
public static void main(String []args)
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
}
}
// This code is contributed by Smitha
Python3
# Python3 program to print sign wave pattern.
def printSinWave(wave_height, wave_length):
# inner space and outer space.
Is = 1
os = 2
# for loop for height of wave
for i in range(1, wave_height + 1):
# for loop for wave length
for j in range(1, wave_length + 1):
# intermediate spaces
for k in range(1, os + 1):
print(end = " ")
# put any symbol
print("0", end = "")
for k in range(1, Is + 1):
print(end = " ")
# put any symbol
print("0", end = "")
for k in range(1, os + 1):
print(end = " ")
print(end = " ")
# set a value of os to 1 if i+1 Is not
# equal to wave height or 0 otherwise
if (i + 1 != wave_height):
os = 1
else:
os = 0
# set value of Is to 3 if i+1 Is not
# equal to wave height or 5 otherwise
if (i + 1 != wave_height):
Is = 3
else:
Is = 5
print()
# Driver code
wave_height, wave_length = 5, 10
printSinWave(wave_height, wave_length)
# This code is contributed by
# Mohit kumar 29
C#
// C# program to print
// sign wave pattern.
using System;
class GFG
{
static void printSinWave(int wave_height,
int wave_length)
{
// inner space and outer space.
int Is = 1, os = 2;
// for loop for height of wave
for (int i = 1;
i <= wave_height; i++)
{
// for loop for wave length
for (int j = 1;
j <= wave_length; j++)
{
// intermediate spaces
for (int k = 1; k <= os; k++)
{
Console.Write(" ");
}
// put any symbol
Console.Write("0");
for (int k = 1; k <= Is; k++)
Console.Write(" ");
// put any symbol
Console.Write("0");
for (int k = 1; k <= os; k++)
Console.Write(" ");
Console.Write(" ");
}
// set a value of os to 1 if i+1
// is not equal to wave height
// or 0 otherwise
os = (i + 1 != wave_height) ? 1 : 0;
// set value of is to 3 if i+1 is not
// equal to wave height or 5 otherwise
Is = (i + 1 != wave_height) ? 3 : 5;
Console.Write("\n");
}
}
// Driver code
public static void Main()
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
Javascript
C++
// C++ program to print sign wave pattern.
#include
void printSinWave(int wave_height, int wave_length)
{
// inner space and outer space.
int is = 1, os = 2;
int inc = 1;
int jump = (wave_height * 3) - (wave_height + 1);
int ch = 'A' + wave_height - 1;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++) {
// for loop for wave length
for (int j = 1; j <= wave_length; j++) {
// intermediate spaces
for (int k = 1; k <= os; k++)
printf(" ");
printf("%c", ch);
for (k = 1; k <= is; k++)
printf(" ");
ch += inc;
if (ch > 'Z')
ch = ch - 26;
printf("%c", ch);
for (k = 1; k <= os; k++)
printf(" ");
printf(" ");
ch += jump;
if (ch > 'Z')
ch = ch - 26;
}
// set value of os to 1 if i+1 is not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height);
// set value of is to 3 if i+1 is not
// equal to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
ch = 'A' + wave_height - i - 1;
inc = inc + 2;
jump -= 2;
printf("\n");
}
}
// Driver code
int main()
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
return 0;
}
Java
// Java program to print sign wave pattern.
class GFG
{
static void printSinWave(int wave_height,
int wave_length)
{
// inner space and outer space.
int is = 1, os = 2;
int inc = 1;
int jump = (wave_height * 3) -
(wave_height + 1);
int ch = 'A' + wave_height - 1;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++)
{
// for loop for wave length
for (int j = 1; j <= wave_length; j++)
{
// intermediate spaces
for (int k = 1; k <= os; k++)
System.out.printf(" ");
System.out.printf("%c", ch);
for (int k = 1; k <= is; k++)
System.out.printf(" ");
ch += inc;
if (ch > 'Z')
ch = ch - 26;
System.out.printf("%c", ch);
for (int k = 1; k <= os; k++)
System.out.printf(" ");
System.out.printf(" ");
ch += jump;
if (ch > 'Z')
ch = ch - 26;
}
// set value of os to 1 if i+1 is not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height) ? 1 : 0;
// set value of is to 3 if i+1 is not
// equal to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
ch = 'A' + wave_height - i - 1;
inc = inc + 2;
jump -= 2;
System.out.printf("\n");
}
}
// Driver code
public static void main(String[] args)
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python program to print sign wave pattern.
def printSinWave(wave_height, wave_length):
# inner space and outer space.
Is = 1
os = 2
inc = 1
jump = (wave_height * 3) -(wave_height + 1)
ch = ord('A') + wave_height - 1
# for loop for height of wave
for i in range(1, wave_height + 1):
# for loop for wave length
for j in range(1, wave_length + 1):
# intermediate space
for k in range(1, os + 1):
print(end = " ")
print(chr(ch), end = "")
for k in range(1, Is + 1):
print(end = " ")
ch += inc
if(ch > ord('Z')):
ch = ch - 26
print(chr(ch), end = "")
for k in range(1, os + 1):
print(end = " ")
print(end = " ")
ch += jump
if(ch > ord('Z')):
ch = ch - 26
# set value of os to 1 if i+1 is not
# equal to wave height or 0 otherwise
if(i + 1 != wave_height):
os = 1
else:
os = 0
# set value of is to 3 if i+1 is not
# equal to wave height or 5 otherwise
if(i + 1 != wave_height):
Is = 3
else:
Is = 5
ch = ord('A') + wave_height - i - 1
inc = inc + 2
jump -= 2
print()
# Driver code
wave_height = 5
wave_length = 10
printSinWave(wave_height, wave_length)
# This code is contributed by avanitrachhadiya2155
C#
// C# program to print sign wave pattern.
using System;
class GFG
{
static void printSinWave(int wave_height,
int wave_length)
{
// inner space and outer space.
int iS = 1, os = 2;
int inc = 1;
int jump = (wave_height * 3) -
(wave_height + 1);
int ch = 'A' + wave_height - 1;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++)
{
// for loop for wave length
for (int j = 1; j <= wave_length; j++)
{
// intermediate spaces
for (int k = 1; k <= os; k++)
Console.Write(" ");
Console.Write("{0}", (char)ch);
for (int k = 1; k <= iS; k++)
Console.Write(" ");
ch += inc;
if (ch > 'Z')
ch = ch - 26;
Console.Write("{0}", (char)ch);
for (int k = 1; k <= os; k++)
Console.Write(" ");
Console.Write(" ");
ch += jump;
if (ch > 'Z')
ch = ch - 26;
}
// set value of os to 1 if i+1 iS not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height) ? 1 : 0;
// set value of iS to 3 if i+1 iS not
// equal to wave height or 5 otherwise
iS = (i + 1 != wave_height) ? 3 : 5;
ch = 'A' + wave_height - i - 1;
inc = inc + 2;
jump -= 2;
Console.Write("\n");
}
}
// Driver code
public static void Main(String[] args)
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
}
}
// This code is contributed by Princi Singh
Javascript
输出:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
如何打印一个罪恶的字母波?
Input : wave_height=5
wave_length=10
Output :
E F O P Y Z I J S T C D M N W X G H Q R
D G N Q X A H K R U B E L O V Y F I P S
C H M R W B G L Q V A F K P U Z E J O T
B I L S V C F M P W Z G J Q T A D K N U
A J K T U D E N O X Y H I R S B C L M V
推荐:请先在 {IDE} 上尝试您的方法,然后再继续解决。
方法与上面相同,现在我们不使用单个元素,而是使用 Upper_Case 中的所有 26 个英文字母。
C++
// C++ program to print sign wave pattern.
#include
void printSinWave(int wave_height, int wave_length)
{
// inner space and outer space.
int is = 1, os = 2;
int inc = 1;
int jump = (wave_height * 3) - (wave_height + 1);
int ch = 'A' + wave_height - 1;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++) {
// for loop for wave length
for (int j = 1; j <= wave_length; j++) {
// intermediate spaces
for (int k = 1; k <= os; k++)
printf(" ");
printf("%c", ch);
for (k = 1; k <= is; k++)
printf(" ");
ch += inc;
if (ch > 'Z')
ch = ch - 26;
printf("%c", ch);
for (k = 1; k <= os; k++)
printf(" ");
printf(" ");
ch += jump;
if (ch > 'Z')
ch = ch - 26;
}
// set value of os to 1 if i+1 is not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height);
// set value of is to 3 if i+1 is not
// equal to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
ch = 'A' + wave_height - i - 1;
inc = inc + 2;
jump -= 2;
printf("\n");
}
}
// Driver code
int main()
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
return 0;
}
Java
// Java program to print sign wave pattern.
class GFG
{
static void printSinWave(int wave_height,
int wave_length)
{
// inner space and outer space.
int is = 1, os = 2;
int inc = 1;
int jump = (wave_height * 3) -
(wave_height + 1);
int ch = 'A' + wave_height - 1;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++)
{
// for loop for wave length
for (int j = 1; j <= wave_length; j++)
{
// intermediate spaces
for (int k = 1; k <= os; k++)
System.out.printf(" ");
System.out.printf("%c", ch);
for (int k = 1; k <= is; k++)
System.out.printf(" ");
ch += inc;
if (ch > 'Z')
ch = ch - 26;
System.out.printf("%c", ch);
for (int k = 1; k <= os; k++)
System.out.printf(" ");
System.out.printf(" ");
ch += jump;
if (ch > 'Z')
ch = ch - 26;
}
// set value of os to 1 if i+1 is not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height) ? 1 : 0;
// set value of is to 3 if i+1 is not
// equal to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
ch = 'A' + wave_height - i - 1;
inc = inc + 2;
jump -= 2;
System.out.printf("\n");
}
}
// Driver code
public static void main(String[] args)
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python program to print sign wave pattern.
def printSinWave(wave_height, wave_length):
# inner space and outer space.
Is = 1
os = 2
inc = 1
jump = (wave_height * 3) -(wave_height + 1)
ch = ord('A') + wave_height - 1
# for loop for height of wave
for i in range(1, wave_height + 1):
# for loop for wave length
for j in range(1, wave_length + 1):
# intermediate space
for k in range(1, os + 1):
print(end = " ")
print(chr(ch), end = "")
for k in range(1, Is + 1):
print(end = " ")
ch += inc
if(ch > ord('Z')):
ch = ch - 26
print(chr(ch), end = "")
for k in range(1, os + 1):
print(end = " ")
print(end = " ")
ch += jump
if(ch > ord('Z')):
ch = ch - 26
# set value of os to 1 if i+1 is not
# equal to wave height or 0 otherwise
if(i + 1 != wave_height):
os = 1
else:
os = 0
# set value of is to 3 if i+1 is not
# equal to wave height or 5 otherwise
if(i + 1 != wave_height):
Is = 3
else:
Is = 5
ch = ord('A') + wave_height - i - 1
inc = inc + 2
jump -= 2
print()
# Driver code
wave_height = 5
wave_length = 10
printSinWave(wave_height, wave_length)
# This code is contributed by avanitrachhadiya2155
C#
// C# program to print sign wave pattern.
using System;
class GFG
{
static void printSinWave(int wave_height,
int wave_length)
{
// inner space and outer space.
int iS = 1, os = 2;
int inc = 1;
int jump = (wave_height * 3) -
(wave_height + 1);
int ch = 'A' + wave_height - 1;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++)
{
// for loop for wave length
for (int j = 1; j <= wave_length; j++)
{
// intermediate spaces
for (int k = 1; k <= os; k++)
Console.Write(" ");
Console.Write("{0}", (char)ch);
for (int k = 1; k <= iS; k++)
Console.Write(" ");
ch += inc;
if (ch > 'Z')
ch = ch - 26;
Console.Write("{0}", (char)ch);
for (int k = 1; k <= os; k++)
Console.Write(" ");
Console.Write(" ");
ch += jump;
if (ch > 'Z')
ch = ch - 26;
}
// set value of os to 1 if i+1 iS not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height) ? 1 : 0;
// set value of iS to 3 if i+1 iS not
// equal to wave height or 5 otherwise
iS = (i + 1 != wave_height) ? 3 : 5;
ch = 'A' + wave_height - i - 1;
inc = inc + 2;
jump -= 2;
Console.Write("\n");
}
}
// Driver code
public static void Main(String[] args)
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
}
}
// This code is contributed by Princi Singh
Javascript
输出:
E F O P Y Z I J S T C D M N W X G H Q R
D G N Q X A H K R U B E L O V Y F I P S
C H M R W B G L Q V A F K P U Z E J O T
B I L S V C F M P W Z G J Q T A D K N U
A J K T U D E N O X Y H I R S B C L M V