接收数字并以大尺寸打印出来的程序
问题:编写一个程序,接收一个数字作为输入,并将其以大尺寸打印在同一行上,如下图所示。
例子 :
Input : 0194
Output :
##### # ##### #
# # ## # # # #
# # # # # # #
# # # ##### #####
# # # # #
# # # # #
##### ##### # #
Explanation: Each digit of the number has been printed in hash format
Input : 0123456789
Output :
##### # ##### ##### # ##### ##### ##### ##### #####
# # ## # # # # # # # # # # #
# # # # # # # # # # # # # #
# # # ##### ##### ##### ##### ##### #### ##### #####
# # # # # # # # # # # # #
# # # # # # # # # # # # #
##### ##### ##### ##### # ##### ##### # ##### #
我们使用二维字符数组来存储每个数字的哈希字符串。 for 循环读取每个数字,if-else 检查该数字并在同一行上打印来自 2D 字符数组的每个数字的每一行。然后,继续下一行并以相同的方式工作,直到打印整个设计。
C
// C program to print a number in large size
#include
#include
#include
#define H 7
// one extra room in the char array is required for storing '\0'
#define W 8
void hashprint(char num[])
{
int i, j, k;
// declaring char 2D arrays and initializing
// with hash-printed digits
char zero[H][W]={" ##### ", // H=0
" # # ", // H=1
" # # ", // H=2
" # # ", // H=3
" # # ", // H=4
" # # ", // H=5
" ##### "},// H=6
one[H][W]={" # ",
" ## ",
" # ",
" # ",
" # ",
" # ",
" ##### "},
two[H][W]={" ##### ",
" # ",
" # ",
" ##### ",
" # ",
" # ",
" ##### "},
three[H][W]={" ##### ",
" # ",
" # ",
" ##### ",
" # ",
" # ",
" ##### "},
four[H][W]={" # ",
" # # ",
" # # ",
" ##### ",
" # ",
" # ",
" # "},
five[H][W]={" ##### ",
" # ",
" # ",
" ##### ",
" # ",
" # ",
" ##### "},
six[H][W]={" ##### ",
" # ",
" # ",
" ##### ",
" # # ",
" # # ",
" ##### "},
seven[H][W]={" ##### ",
" # ",
" # ",
" #### ",
" # ",
" # ",
" # "},
eight[H][W]={" ##### ",
" # # ",
" # # ",
" ##### ",
" # # ",
" # # ",
" ##### "},
nine[H][W]={" ##### ",
" # # ",
" # # ",
" ##### ",
" # ",
" # ",
" # "};
if (strlen(num) > 10)
printf("\nYou must enter a number upto 10 digits.\nTry again!\n");
else
{
printf("\n");
k=1;
j=0; //controls H of each digit
while (k <= 7) //controls height
{
for (i=0; i
Java
// JAVA Code for Program that receives a number
// and prints it out in large size
class GFG {
public static void hashprint(String num)
{
int i, j, k;
// declaring char 2D arrays and initializing
// with hash-printed digits
String zero[]={" ##### ", // H=0
" # # ", // H=1
" # # ", // H=2
" # # ", // H=3
" # # ", // H=4
" # # ", // H=5
" ##### "};// H=6
String one[]={" # ",
" ## ",
" # ",
" # ",
" # ",
" # ",
" ##### "};
String two[]={" ##### ",
" # ",
" # ",
" ##### ",
" # ",
" # ",
" ##### "};
String three[]={" ##### ",
" # ",
" # ",
" ##### ",
" # ",
" # ",
" ##### "};
String four[]={" # ",
" # # ",
" # # ",
" ##### ",
" # ",
" # ",
" # "};
String five[]={" ##### ",
" # ",
" # ",
" ##### ",
" # ",
" # ",
" ##### "};
String six[]={" ##### ",
" # ",
" # ",
" ##### ",
" # # ",
" # # ",
" ##### "};
String seven[]={" ##### ",
" # ",
" # ",
" #### ",
" # ",
" # ",
" # "};
String eight[]={" ##### ",
" # # ",
" # # ",
" ##### ",
" # # ",
" # # ",
" ##### "};
String nine[]={" ##### ",
" # # ",
" # # ",
" ##### ",
" # ",
" # ",
" # "};
if (num.length() > 10)
System.out.println("\nYou must enter a number "+
"upto 10 digits.\nTry again!\n");
else
{
System.out.println("");
k = 1;
j = 0; //controls H of each digit
while (k <= 7) //controls height
{
for (i = 0; i < num.length(); i ++) //reads each digit
{
if (num.charAt(i) == '0')
System.out.print(zero[j]);
else if (num.charAt(i) == '1')
System.out.print(one[j]);
else if (num.charAt(i) == '2')
System.out.print(two[j]);
else if (num.charAt(i) == '3')
System.out.print(three[j]);
else if (num.charAt(i) == '4')
System.out.print(four[j]);
else if (num.charAt(i) == '5')
System.out.print(five[j]);
else if (num.charAt(i) == '6')
System.out.print(six[j]);
else if (num.charAt(i) == '7')
System.out.print(seven[j]);
else if (num.charAt(i) == '8')
System.out.print(eight[j]);
else if (num.charAt(i) == '9')
System.out.print(nine[j]);
}
System.out.println("");
k ++;
j ++;
}
}
}
/* Driver program to test above function */
public static void main(String[] args)
{
// passing 0194 as string to function hashprint
// you can pass whatever string you wish to
hashprint("0194");
}
}
// This code is contributed by Arnav Kr. Mandal.
C#
// C# Code for Program that
// receives a number and prints
// it out in large size
using System;
class GFG
{
public static void hashprint(string num)
{
int i, j, k;
// declaring char 2D arrays
// and initializing with
// hash-printed digits
String []zero={" ##### ", // H=0
" # # ", // H=1
" # # ", // H=2
" # # ", // H=3
" # # ", // H=4
" # # ", // H=5
" ##### "};// H=6
String []one={" # ",
" ## ",
" # ",
" # ",
" # ",
" # ",
" ##### "};
String []two={" ##### ",
" # ",
" # ",
" ##### ",
" # ",
" # ",
" ##### "};
String []three={" ##### ",
" # ",
" # ",
" ##### ",
" # ",
" # ",
" ##### "};
String []four={" # ",
" # # ",
" # # ",
" ##### ",
" # ",
" # ",
" # "};
String []five={" ##### ",
" # ",
" # ",
" ##### ",
" # ",
" # ",
" ##### "};
String []six={" ##### ",
" # ",
" # ",
" ##### ",
" # # ",
" # # ",
" ##### "};
String []seven={" ##### ",
" # ",
" # ",
" #### ",
" # ",
" # ",
" # "};
String []eight={" ##### ",
" # # ",
" # # ",
" ##### ",
" # # ",
" # # ",
" ##### "};
String []nine={" ##### ",
" # # ",
" # # ",
" ##### ",
" # ",
" # ",
" # "};
if (num.Length > 10)
Console.WriteLine("\nYou must enter a number "+
"upto 10 digits.\nTry again!\n");
else
{
Console.WriteLine("");
k = 1;
j = 0; //controls H of each digit
while ($k <= 7) //controls height
{
//reads each digit
for ($i = 0; $i < strlen($num); $i++)
{
if (num[i] == '0')
Console.Write(zero[j]);
else if (num[i] == '1')
Console.Write(one[j]);
else if (num[i] == '2')
Console.Write(two[j]);
else if (num[i] == '3')
Console.Write(three[j]);
else if (num[i] == '4')
Console.Write(four[j]);
else if (num[i] == '5')
Console.Write(five[j]);
else if (num[i] == '6')
Console.Write(six[j]);
else if (num[i] == '7')
Console.Write(seven[j]);
else if (num[i] == '8')
Console.Write(eight[j]);
else if (num[i] == '9')
Console.Write(nine[j]);
}
Console.WriteLine("");
k ++;
j ++;
}
}
}
// Driver Code
public static void Main()
{
// passing 0194 as string to
// function hashprint you can
// pass whatever string you wish to
hashprint("0194");
}
}
// This code is contributed by Sam007
PHP
10)
echo "\nYou must enter a " .
"number upto 10 digits.
\nTry again!\n";
else
{
echo "\n";
$k = 1;
$j = 0; //controls H of each digit
while ($k <= 7) //controls height
{
//reads each digit
for ($i = 0; $i < strlen($num); $i++)
{
if ($num[$i] == '0')
echo $zero[$j];
else if ($num[$i] == '1')
echo $one[$j];
else if ($num[$i] == '2')
echo $two[$j];
else if ($num[$i] == '3')
echo $three[$j];
else if ($num[$i] == '4')
echo $four[$j];
else if ($num[$i] == '5')
echo $five[$j];
else if ($num[$i] == '6')
echo $six[$j];
else if ($num[$i] == '7')
echo $seven[$j];
else if ($num[$i] == '8')
echo $eight[$j];
else if ($num[$i] == '9')
echo $nine[$j];
}
echo "\n";
$k++;
$j++;
}
}
}
// Driver Code
// passing 0194 as string
// to function hashprint
// you can pass whatever
// string you wish to
hashprint("0194");
// This code is contributed by ajit
?>
输出:
##### # ##### #
# # ## # # # #
# # # # # # #
# # # ##### #####
# # # # #
# # # # #
##### ##### # #