|
-
Mar 29th, 2005, 09:45 PM
#1
Thread Starter
Frenzied Member
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.
-
Mar 29th, 2005, 11:49 PM
#2
Dazed Member
Re: Diamond Challenge
So if we get this your gonna buy a diamond for each of us?
-
Mar 30th, 2005, 12:34 AM
#3
Dazed Member
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!");}
}
}
Last edited by Dilenger4; Mar 30th, 2005 at 12:40 AM.
Reason: Forgot [code][/code] tags
-
Mar 30th, 2005, 04:01 AM
#4
Fanatic Member
Re: Diamond Challenge
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();
}
}
}
-
Mar 30th, 2005, 10:07 AM
#5
Thread Starter
Frenzied Member
Re: Diamond Challenge
 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!
-
Mar 30th, 2005, 10:08 AM
#6
Thread Starter
Frenzied Member
Re: Diamond Challenge
 Originally Posted by nebulom
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();
}
}
}
Nice job neb, you got it...Except you have the same problem as me: The spacing inbetween the astrix.
-
Mar 30th, 2005, 10:09 AM
#7
Thread Starter
Frenzied Member
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(" ");
}
}
}
}
-
Mar 30th, 2005, 10:12 AM
#8
Thread Starter
Frenzied Member
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!
-
Mar 30th, 2005, 04:20 PM
#9
Thread Starter
Frenzied Member
Re: Diamond Challenge
What about the Pig Latin program? I was having some trouble on that one.
-
Mar 31st, 2005, 07:29 PM
#10
Dazed Member
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.
-
Mar 31st, 2005, 07:43 PM
#11
Thread Starter
Frenzied Member
Re: Diamond Challenge
 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.
-
Mar 31st, 2005, 07:45 PM
#12
Thread Starter
Frenzied Member
Re: Diamond Challenge
 Originally Posted by Dilenger4
beer and poker night. 
That's a holy time for a man.
-
Apr 1st, 2005, 12:39 AM
#13
Fanatic Member
Re: Diamond Challenge
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();
}
}
}
-
Apr 1st, 2005, 10:28 AM
#14
Thread Starter
Frenzied Member
Re: Diamond Challenge
 Originally Posted by nebulom
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?
-
Apr 1st, 2005, 11:38 AM
#15
Re: Diamond Challenge
 Originally Posted by System_Error
That is very nice, where are you actually puting the spaces between?
 Originally Posted by nebulom
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++)
[hl=#FFFF00]for(int j=0;j<n;j++) c[i][j]=' ';[/hl]
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();
}
}
}
Premade?
-
Apr 1st, 2005, 01:11 PM
#16
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|