Results 1 to 16 of 16

Thread: Diamond Challenge

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Diamond Challenge

    Look at this URL, and see if you can figure out the diamond problem:

    http://www.cs.wcu.edu/cscontest/questions.txt

    I've already got it figured out, but I would like to see if anyone else can get this, and if their solution looks like mine.

  2. #2

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Diamond Challenge

    Too tired to format.
    Code:
     import java.util.*; 
    
     public class Diamond{
      public static void main(String[] args) {
       try{
        if(args.length < 1)return; 
        String s = args[0]; 
        int k = Integer.parseInt(s); 
        for(int i = 1; i <= k ; ++i){
        System.out.println(); 
        char[] c = new char[i];
        Arrays.fill(c, '*');
        for(int x = 0; x < c.length; ++x){
         System.out.print(c[x]);
        }
       }
        for(int i = k; i >= 0 ; --i){
        System.out.println(); 
        char[] c = new char[i];
        Arrays.fill(c, '*');
        for(int x = 0; x < c.length; ++x){
         System.out.print(c[x]);
        }
       }
      }catch(NumberFormatException nfe){System.err.println("Must be a valid number!");}
     }
    }

  4. #4
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Diamond Challenge

    Might not be good though but here goes.
    VB Code:
    1. public class test2{
    2.     public static void main(String[] args){
    3.         int n=Integer.parseInt(args[0]);
    4.         if(n%2==0) return;
    5.         char[][] c=new char[n][n];
    6.         for(int i=0;i<n;i++)
    7.             for(int j=0;j<n;j++) c[i][j]=' ';
    8.         int startPos=(n/2),endPos=(n/2);
    9.         for(int i=0;i<n;i++){
    10.             for(int j=startPos;j<=endPos&&j>=0&&j<n;j++)
    11.                 c[i][j]='*';
    12.             startPos=(i<(n/2))?startPos-1:startPos+1;
    13.             endPos=(i<(n/2))?endPos+1:endPos-1;
    14.         }
    15.         for(int i=0;i<n;i++){
    16.             for(int j=0;j<n;j++){
    17.                 System.out.print(c[i][j]);
    18.             }
    19.             System.out.println();
    20.         }
    21.     }
    22. }

  5. #5

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Diamond Challenge

    Quote Originally Posted by Dilenger4
    Too tired to format.
    Code:
     import java.util.*; 
    
     public class Diamond{
      public static void main(String[] args) {
       try{
        if(args.length < 1)return; 
        String s = args[0]; 
        int k = Integer.parseInt(s); 
        for(int i = 1; i <= k ; ++i){
        System.out.println(); 
        char[] c = new char[i];
        Arrays.fill(c, '*');
        for(int x = 0; x < c.length; ++x){
         System.out.print(c[x]);
        }
       }
        for(int i = k; i >= 0 ; --i){
        System.out.println(); 
        char[] c = new char[i];
        Arrays.fill(c, '*');
        for(int x = 0; x < c.length; ++x){
         System.out.print(c[x]);
        }
       }
      }catch(NumberFormatException nfe){System.err.println("Must be a valid number!");}
     }
    }

    Well, you got the easy part done, but were is the spacing.....muuuuhahhah..That's the hardest part!

  6. #6

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Diamond Challenge

    Quote Originally Posted by nebulom
    Might not be good though but here goes.
    VB Code:
    1. public class test2{
    2.     public static void main(String[] args){
    3.         int n=Integer.parseInt(args[0]);
    4.         if(n%2==0) return;
    5.         char[][] c=new char[n][n];
    6.         for(int i=0;i<n;i++)
    7.             for(int j=0;j<n;j++) c[i][j]=' ';
    8.         int startPos=(n/2),endPos=(n/2);
    9.         for(int i=0;i<n;i++){
    10.             for(int j=startPos;j<=endPos&&j>=0&&j<n;j++)
    11.                 c[i][j]='*';
    12.             startPos=(i<(n/2))?startPos-1:startPos+1;
    13.             endPos=(i<(n/2))?endPos+1:endPos-1;
    14.         }
    15.         for(int i=0;i<n;i++){
    16.             for(int j=0;j<n;j++){
    17.                 System.out.print(c[i][j]);
    18.             }
    19.             System.out.println();
    20.         }
    21.     }
    22. }

    Nice job neb, you got it...Except you have the same problem as me: The spacing inbetween the astrix.

  7. #7

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Diamond Challenge

    Here was my solution, it wasn't nearly as efficent as neb, but that's ok..

    NOTE: Watch and learn dillenger!

    Code:
    import java.io.*;
    
    public class Diamonds
    {
    	public static int width;
    	public static void main(String[] args) throws IOException
    	{
    		BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
    		System.out.print("Enter the width of the largest segment of the triangle:  ");
    		width = Integer.parseInt(inputReader.readLine());
    		int countDown = 0;
    		int counter2 = width-1;
    		int count = width-2;
    
    		for (int i=0; i<width; i++)
    		{
    			calculateTopSpaces(counter2);
    			for (int j=counter2; j<width; j++)
    			{
    				
    				System.out.print("*");
    				
    			}
    			System.out.println("");
    			counter2 -= 2;
    			if (counter2 < 0)
    			{
    				for (int a=0; a<width; a++)
    				{
    					calculateBottomSpaces(count);
    					for (int d=0; d<count; d++)
    					{
    						
    						System.out.print("*");
    					}
    					count-=2;
    					System.out.println("");
    				}
    				break;
    				
    			}
    			
    		}	
    	}
    	
    	public static void calculateTopSpaces(int count)
    	{
    		int spaces = count/2+2;
    		for (int i=0; i<spaces; i++)
    		{
    			if (i == spaces-1)
    			{
    			}
    			else
    			{
    				System.out.print(" ");
    			}
    		}
    	
    	}
    	public static void calculateBottomSpaces(int count)
    	{
    			int spaces = (width-count)/2+2;
    		for (int i=0; i<spaces; i++)
    		{
    			if (i == spaces-1)
    			{
    			}
    			else
    			{
    				System.out.print(" ");
    			}
    		}
    	}
    			
    		
    }

  8. #8

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Diamond Challenge

    Is there another one that you guys want to try? I'm practicing those because I have a team competition at that university, and those questions are previous questions....So pick one out!

  9. #9

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Diamond Challenge

    What about the Pig Latin program? I was having some trouble on that one.

  10. #10
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Diamond Challenge

    Ill try that one but i probably won't have it done till sat night. It's better for me to save it for the weekend. Could do it friday but that's beer and poker night.

  11. #11

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Diamond Challenge

    Quote Originally Posted by Dilenger4
    Ill try that one but i probably won't have it done till sat night. It's better for me to save it for the weekend. Could do it friday but that's beer and poker night.
    Well, it doesn't matter when it gets done..I mean, anything will help me. I was having trouble with it, and would like to see a solution.

  12. #12

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Diamond Challenge

    Quote Originally Posted by Dilenger4
    beer and poker night.
    That's a holy time for a man.

  13. #13
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Diamond Challenge

    Diamond updated
    VB Code:
    1. public class test3{
    2.     public static void main(String[] args){
    3.         int n=Integer.parseInt(args[0]);
    4.         if(n%2==0) return;
    5.         n=(n*2)+3;
    6.         char[][] c=new char[n][n];
    7.         for(int i=0;i<n;i++)
    8.             for(int j=0;j<n;j++) c[i][j]=' ';
    9.         int startPos=(n/2),endPos=(n/2);
    10.         for(int i=0;i<n;i+=2){
    11.             for(int j=startPos;j<=endPos&&j>=0&&j<n;j+=2)
    12.                 c[i][j]='*';
    13.             startPos=(i<(n/2))?startPos-2:startPos+2;
    14.             endPos=(i<(n/2))?endPos+2:endPos-2;
    15.         }
    16.         for(int i=0;i<n;i++){
    17.             for(int j=0;j<n;j++){
    18.                 System.out.print(c[i][j]);
    19.             }
    20.             System.out.println();
    21.         }
    22.     }
    23. }

  14. #14

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Diamond Challenge

    Quote Originally Posted by nebulom
    Diamond updated
    VB Code:
    1. public class test3{
    2.     public static void main(String[] args){
    3.         int n=Integer.parseInt(args[0]);
    4.         if(n%2==0) return;
    5.         n=(n*2)+3;
    6.         char[][] c=new char[n][n];
    7.         for(int i=0;i<n;i++)
    8.             for(int j=0;j<n;j++) c[i][j]=' ';
    9.         int startPos=(n/2),endPos=(n/2);
    10.         for(int i=0;i<n;i+=2){
    11.             for(int j=startPos;j<=endPos&&j>=0&&j<n;j+=2)
    12.                 c[i][j]='*';
    13.             startPos=(i<(n/2))?startPos-2:startPos+2;
    14.             endPos=(i<(n/2))?endPos+2:endPos-2;
    15.         }
    16.         for(int i=0;i<n;i++){
    17.             for(int j=0;j<n;j++){
    18.                 System.out.print(c[i][j]);
    19.             }
    20.             System.out.println();
    21.         }
    22.     }
    23. }
    That is very nice, where are you actually puting the spaces between?

  15. #15
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Diamond Challenge

    Quote Originally Posted by System_Error
    That is very nice, where are you actually puting the spaces between?

    Quote Originally Posted by nebulom
    Diamond updated
    VB Code:
    1. public class test3{
    2.     public static void main(String[] args){
    3.         int n=Integer.parseInt(args[0]);
    4.         if(n%2==0) return;
    5.         n=(n*2)+3;
    6.         char[][] c=new char[n][n];
    7.         for(int i=0;i<n;i++)
    8.             [hl=#FFFF00]for(int j=0;j<n;j++) c[i][j]=' ';[/hl]
    9.         int startPos=(n/2),endPos=(n/2);
    10.         for(int i=0;i<n;i+=2){
    11.             for(int j=startPos;j<=endPos&&j>=0&&j<n;j+=2)
    12.                 c[i][j]='*';
    13.             startPos=(i<(n/2))?startPos-2:startPos+2;
    14.             endPos=(i<(n/2))?endPos+2:endPos-2;
    15.         }
    16.         for(int i=0;i<n;i++){
    17.             for(int j=0;j<n;j++){
    18.                 System.out.print(c[i][j]);
    19.             }
    20.             System.out.println();
    21.         }
    22.     }
    23. }

    Premade?

  16. #16

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Diamond Challenge

    I finished the PigLatin program, it was a lot easier than I thought once I started using StringTokenizer.

    Code:
    import java.io.*;
    import java.util.*;
    
    public class PigLatin4
    {
       public static void main(String[] args) throws Exception
       {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	System.out.print("Enter a phrase --> ");
    	String phrase = br.readLine();
    	StringTokenizer st = new StringTokenizer(phrase);
    	
    	while (st.hasMoreTokens())
    	{
    		StringBuffer sb = new StringBuffer(st.nextToken());
    		sb.append(sb.charAt(0));
    		sb.append("ay");
    		sb.deleteCharAt(0);
    		System.out.print(sb.toString() + " ");
    	}
       }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width