Webkul 面试编码圆形模式
这是一种混合模式,它使用了不同的类型,当我在 Webkul 面试中进行第一轮编码时,有人问我这个模式。
例子:
Input : 3
Output :
@
@@@
@@@@@
* *
**@@@**
* *
Input : 5
Output :
@
@@@
@@@@@
@@@@@@@
* *
** **
***@@@@@***
** **
* *
下面是上述模式的实现:
C++
// C++ implementation
#include
using namespace std;
// Driver code
int main() {
int n, i, j;
cin >> n ;
for (i = 0; i < (n/2) + 2; i++){
for (j = 0; j < n - i; j++){
cout << " ";
}
for (j = 0; j < 2 * i + 1; j++){
cout <<"@";
}
cout << "\n";
}
for (i = 0; i < n; i++){
for (j = 0; j < (n/2) + 1; j++){
if (j >= n / 2 - i && j >= i - n / 2){
cout << "*" ;
}
else
cout << " ";
}
for (j = 0; j < n; j++){
if (i == n / 2)
cout <<"@" ;
else
cout <<" ";
}
for (j = 0; j < (n / 2) + 1; j++){
if (j >= n / 2 - i && j >= i - n / 2)
cout <<"*";
}
cout <<"\n";
}
return 0;
}
// This code is contributed by shivanisinghss2110
C
// C implementation
#include
// Driver code
int main() {
int n, i, j;
scanf("%d", &n);
for (i = 0; i < (n/2) + 2; i++){
for (j = 0; j < n - i; j++){
printf(" ");
}
for (j = 0; j < 2 * i + 1; j++){
printf("@");
}
printf("\n");
}
for (i = 0; i < n; i++){
for (j = 0; j < (n/2) + 1; j++){
if (j >= n / 2 - i && j >= i - n / 2){
printf("*");
}
else
printf(" ");
}
for (j = 0; j < n; j++){
if (i == n / 2)
printf("@");
else
printf(" ");
}
for (j = 0; j < (n / 2) + 1; j++){
if (j >= n / 2 - i && j >= i - n / 2)
printf("*");
}
printf("\n");
}
return 0;
}
// This code is contributed by shubhamsingh10
Java
// Java implementation
import java.util.*;
class GFG{
// Driver Code
public static void main(String[] args)
{
int n, i, j;
Scanner s = new Scanner(System.in);
n = s.nextInt();
for(i = 0; i < (n / 2) + 2; i++)
{
for(j = 0; j < n - i; j++)
{
System.out.print(" ");
}
for(j = 0; j < 2 * i + 1; j++)
{
System.out.print("@");
}
System.out.println();
}
for(i = 0; i < n; i++)
{
for(j = 0; j < (n / 2) + 1; j++)
{
if (j >= n / 2 - i && j >= i - n / 2)
{
System.out.print("*");
}
else
System.out.print(" ");
}
for(j = 0; j < n; j++)
{
if (i == n / 2)
System.out.print("@");
else
System.out.print(" ");
}
for(j = 0; j < (n / 2) + 1; j++)
{
if (j >= n / 2 - i && j >= i - n / 2)
System.out.print("*");
}
System.out.println();
}
}
}
// This code is contributed by divyesh072019
Python3
# Python3 implementation
n=int(input())
for i in range(n//2+2):
for j in range(n-i):
print(" ",end="")
for j in range(2*i+1):
print("@",end="")
print()
for i in range(n):
for j in range(n//2+1):
if (j>=n//2-i and j>=i-n//2):
print("*",end="")
else:
print(" ",end="")
for j in range(n):
if i==n//2:
print("@",end="")
else:
print(" ",end="")
for j in range(n//2+1):
if (j>=n//2-i and j>=i-n//2):
print("*",end="")
print()
C#
// C# implementation
using System;
class GFG {
static void Main()
{
int n, i, j;
string val;
val = Console.ReadLine();
n = Convert.ToInt32(val);
for (i = 0; i < (n/2) + 2; i++)
{
for (j = 0; j < n - i; j++)
{
Console.Write(" ");
}
for (j = 0; j < 2 * i + 1; j++)
{
Console.Write("@");
}
Console.WriteLine();
}
for (i = 0; i < n; i++)
{
for (j = 0; j < (n/2) + 1; j++)
{
if (j >= n / 2 - i && j >= i - n / 2)
{
Console.Write("*");
}
else
Console.Write(" ");
}
for (j = 0; j < n; j++)
{
if (i == n / 2)
Console.Write("@");
else
Console.Write(" ");
}
for (j = 0; j < (n / 2) + 1; j++)
{
if (j >= n / 2 - i && j >= i - n / 2)
Console.Write("*");
}
Console.WriteLine();
}
}
}
// This code is contributed by divyeshrabadiya07
输入:
5
输出:
@
@@@
@@@@@
@@@@@@@
* *
** **
***@@@@@***
** **
* *