-
TreePlanting Level 2 Solution
My first post, from Veracruz,Mexico
--- My optimized code
public class TreePlanting
{public long countArrangements(int total, int fancy)
{ String s;
int c=0,c1;
// 0 is plain, 1 is fancy
for ( short x = 0; x<Math.pow(2,total); x++)
{ s = Integer.toBinaryString(x);
c1 = 0;
if (s.indexOf( "11" ) == -1) for (short y = 0; y < s.length(); y++ )
if (s.startsWith("1",y)) c1 ++; if (c1 == fancy) c++; }
return c; } }
--- My Original Idea
/* Gustavo Antonio Parada Sarmiento
Instituto Tecnológico de Veracruz
Veracruz, México
21 años.
*/
import java.util.Vector;
public class TreePlanting
{
public long countArrangements(int total, int fancy)
{
Vector v = new Vector(1,2);
String s;
int c1;
// 0 is plain, 1 is fancy -
for (short x = 0; x<Math.pow(2,total); x++)
{
s = Integer.toBinaryString(x);
c1 = 0;
if (s.indexOf( "11" ) == -1)
for (short y = 0; y < s.length(); y++ )
if (s.startsWith("1",y))
c1 ++;
if (c1 == fancy)
v.add(s);
}
return v.size();
}
TreePlanting()
{
System.out.println( countArrangements(4,2) );
System.out.println( countArrangements(3,1) );
System.out.println( countArrangements(4,3) );
System.out.println( countArrangements(10,4) );
}
static public void main(String[] a)
{ TreePlanting t = new TreePlanting();
}
}
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks