2017-10-17 3 views
-2

je veux imprimer comme cette sortiecomment imprimer ce genre de motif en utilisant 3 rapide?

1 
    121 
    12312 
1234123 
123454321 

et voici mon code

var no = 1 
var numberOfRow = 5 
for i in 1...numberOfRow { 
    for _ in 1..<(6-i) { 
     print("_", terminator: " ") 
    } 
    for _ in 1...i { 
     //no += 1 
     print("\(no)", terminator: " ") 
     no += 1 
    } 
    for _ in 1..<no - 1 { 
     no -= 1 
     print("\(no - 1)", terminator: " ") 
    } 
    print("\(no)") 
} 

mais ses spectacles de sortie qui, comme ci-dessous

_ _ _ _ 1 2 
_ _ _ 2 3 2 1 2 
_ _ 2 3 4 3 2 1 2 
_ 2 3 4 5 4 3 2 1 2 
2 3 4 5 6 5 4 3 2 1 2 

où est mon problème dans ce code?

Répondre

0

S'il vous plaît vérifier:

var no = 1 
var numberOfRow = 5 
for i in 1...numberOfRow { 
    for _ in 1..<(6-i) { 
     print(" ", terminator: " ") 
    } 
    for j in 1...i { 
     print("\(j)", terminator: " ") 
     no = j 
    } 
    for k in 1..<no { 
     no -= 1 
     print("\(no)", terminator: " ") 
    } 
    print(" ") 
} 
0

Voici le pseudo-code pour vous. changez vos boucles pour en conséquence.

  int rc = 5; 
     for(int i=1;i<=rc;i++) 
     { 
      for(int j=0;j<(rc-i);j++) 
      { 
       Print("_"); 
      } 
      for(int k=0;k<i;k++) 
      { 
       Print(k + 1); 
      } 
      for(int l=(i-1);l>0;l--) 
      { 
       Print(l); 
      } 
      print("\(no)") 
     }