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.
Printable View
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.
So if we get this your gonna buy a diamond for each of us? :lol:
Too tired to format. :sick:
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!");}
}
}
Might not be good though but here goes.
VB Code:
public class test2{ public static void main(String[] args){ int n=Integer.parseInt(args[0]); if(n%2==0) return; char[][] c=new char[n][n]; for(int i=0;i<n;i++) for(int j=0;j<n;j++) c[i][j]=' '; int startPos=(n/2),endPos=(n/2); for(int i=0;i<n;i++){ for(int j=startPos;j<=endPos&&j>=0&&j<n;j++) c[i][j]='*'; startPos=(i<(n/2))?startPos-1:startPos+1; endPos=(i<(n/2))?endPos+1:endPos-1; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ System.out.print(c[i][j]); } System.out.println(); } } }
Quote:
Originally Posted by Dilenger4
Well, you got the easy part done, but were is the spacing.....muuuuhahhah..That's the hardest part!
Quote:
Originally Posted by nebulom
Nice job neb, you got it...Except you have the same problem as me: The spacing inbetween the astrix.
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(" ");
}
}
}
}
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!
What about the Pig Latin program? I was having some trouble on that one.
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.Quote:
Originally Posted by Dilenger4
That's a holy time for a man.Quote:
Originally Posted by Dilenger4
Diamond updated
VB Code:
public class test3{ public static void main(String[] args){ int n=Integer.parseInt(args[0]); if(n%2==0) return; n=(n*2)+3; char[][] c=new char[n][n]; for(int i=0;i<n;i++) for(int j=0;j<n;j++) c[i][j]=' '; int startPos=(n/2),endPos=(n/2); for(int i=0;i<n;i+=2){ for(int j=startPos;j<=endPos&&j>=0&&j<n;j+=2) c[i][j]='*'; startPos=(i<(n/2))?startPos-2:startPos+2; endPos=(i<(n/2))?endPos+2:endPos-2; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ System.out.print(c[i][j]); } System.out.println(); } } }
That is very nice, where are you actually puting the spaces between?Quote:
Originally Posted by nebulom
Quote:
Originally Posted by System_Error
Quote:
Originally Posted by nebulom
Premade? :)
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() + " ");
}
}
}