+ 4
How should I generate this shape? 1 1 2 1 2 3 1 2 1
I want to build a program to show this output: 1 1 2 1 2 3 1 2 1 what should I do? I mean : << what is the source code of this program? >>
7 Réponses
+ 3
you can see it in my codes for c++
+ 5
for(int i=1;i <=3;i++)
{
for(int k=1;k<=i;k++)
{
printf ("%d ",k);
}
printf ("\n");
}
for(int i=2;i >=1;i--)
{
for(int k=1;k<=i;k++)
{
printf ("%d ",k);
}
printf ("\n");
}
+ 3
In Python you can do this. Try copying the logic to your language of choice.
n = "3"
s = "1"
lines = []
for i in range(2, n + 2, 2):
lines.append("{:^{n}}".format(s, n=n))
s += "{i}{j}".format(i=i, j=i + 1)
lines += reversed(lines[:-1])
for l in lines:
print(l)
+ 2
navid do you now know the answer
+ 2
Thank you all !
+ 1
yes use loops
+ 1
good