Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Triangle lines of numbers
02-05-2010, 11:04 PM
Post: #1
Triangle lines of numbers
This is an interesting task. You need to input a number "n" that represents number of lines, like on the picture :
[Image: 38227_linesofnumberstask.jpg]
That is simple algorithm, though the biggest problem is to make a Equilateral Triangle and not Right Triangle.
I made it with 3 for loops, two inside of one, but it can be made with one too.
Here is the solution in TPW :
Code:
program lines_of_numbers;
uses wincrt;
var
i,j,n:integer;
s:string;
begin
readln(n);
s:='';
for i:=1 to n-1 do
   s:=s+' ';
for i:=1 to n do
   begin
    write(s);
     for j:=i downto 1 do
       write(j);
     if i>1 then
       for j:=2 to i do
        write(j);
     writeln;
     s:=copy(s,1,length(s)-1);
   end;
end.
And here is picture of the solution in TWP for n=9 :
[Image: 38228_linesofnumberstpwsolution.jpg]
Also code in C++ :
Code:
# include <iostream>
# include <cstdio>
# include <string>

using namespace std;

int main()
{
   int i,j,n;
   cin>>n;
   string s;
   s="";
   for (i=0;i<n;i++)
     s+=" ";
    for (i=1;i<=n;i++)
      {
         cout<<s;
         for (j=i;j>=1;j--)
           cout<<j;
         if (i>1)
           for (j=2;j<=i;j++)
             cout<<j;
         cout<<endl;
         s=s.substr(0,s.length()-1);              
      }
    cout<<endl;
    system("pause");
}
And how solution should look like for n=9 :
[Image: 38226_linesofnumberscppsolution.jpg]
That's all, i hope that you like this small task !!!

We attack any new problem we encounter with techniques we already know, and try small modifications if difficulties turn up.
Bram Cohen - Bit Torrent Creator
Visit this user's website Find all posts by this user
Quote this message in a reply
02-06-2010, 12:38 PM (This post was last modified: 02-06-2010 12:52 PM by codecaine.)
Post: #2
RE: Triangle lines of numbers
here a python example I did I used center for formating Smile
Code:
import sys;
from string import center;
tempStr = "";
tri = int(raw_input("Enter in a number: "));
for x in xrange(1,tri+1): #loop through inputted number
    for b in xrange(x,1,-1): #loop backwards
        tempStr = tempStr + str(b);
    for f in xrange(1,x+1): #loop fowards
        tempStr = tempStr + str(f);
    print center(tempStr,tri*3); #print the result with center spacing
    tempStr = ""; #empty string for next loop

PHP Code:
Enter in a number20
                             1                              
                            212                             
                           32123                            
                          4321234                           
                         543212345                          
                        65432123456                         
                       7654321234567                        
                      876543212345678                       
                     98765432123456789                      
                   109876543212345678910                    
                 1110987654321234567891011                  
               12111098765432123456789101112                
             131211109876543212345678910111213              
           1413121110987654321234567891011121314            
         15141312111098765432123456789101112131415          
       161514131211109876543212345678910111213141516        
     1716151413121110987654321234567891011121314151617      
   18171615141312111098765432123456789101112131415161718    
 191817161514131211109876543212345678910111213141516171819  
2019181716151413121110987654321234567891011121314151617181920 
Visit this user's website Find all posts by this user
Quote this message in a reply
02-06-2010, 06:08 PM
Post: #3
RE: Triangle lines of numbers
Very good code, also it is short !

We attack any new problem we encounter with techniques we already know, and try small modifications if difficulties turn up.
Bram Cohen - Bit Torrent Creator
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: