triangle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import System.Environment (getArgs) | |
main = getArgs >>= (putStrLn . triangle . read . head) | |
where | |
triangle n = unlines $ map mkline $ [1..n] | |
mkline i = unwords $ take i $ drop (i `mod` 2) $ altern | |
altern = "0" : "1" : altern |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Triangle { | |
public static String mkline(int n) { | |
StringBuilder line = new StringBuilder(Integer.toString(n % 2)); | |
for (int i = 1; i < n; i++) | |
{ line.append(" " + Integer.toString((n + i) % 2)); } | |
return line.toString(); | |
} | |
public static String mktriangle(int n) { | |
StringBuilder triangle = new StringBuilder(); | |
for (int i = 1; i <= n; i++) | |
{ triangle.append(mkline(i) + "\n"); } | |
return triangle.toString(); | |
} | |
public static void main(String[] args) { | |
System.out.print(mktriangle(Integer.parseInt(args[0]))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment