Java algorithm to make a straight pyramid -
___________1 __________1 2 1 _________1 2 3 2 1 ________1 2 3 4 3 2 1 ______1 2 3 4 5 4 3 2 1 _____1 2 3 4 4 4 4 4 3 2 1 ___1 2 3 3 3 3 3 3 3 3 3 2 1 __1 2 2 2 2 2 2 2 2 2 2 2 2 2 1 _1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
i create pyramid using java? suggestion?
this surve purpose. can change number 5 number other 5. eg. 1,2,3,.. , 6,8
public static void main(string[] args) { list<string> list = new linkedlist<string>(); for(int = 5; > 0; i-- ){ wrapwithnumber(list, i); } (string string : editlisttobeintriangleshape(list)) { system.out.println(string); }; } /** * wrap number strings in llist perticular number. * @param list list of strings * @param ba number need wrapp list with. */ private void wrapwithnumber(list<string> list, final int ba) { list.add(0, string.format("%d",ba)); (int = 1; < list.size(); i++) { string newformat = "%1$d " + list.get(i) + " %1$d"; list.remove(list.get(i)); list.add(i,string.format(newformat, ba)); } string lastformat = "%1$d"; for(int = 0; < 2 * list.size();i++){ lastformat += " %1$d"; } if(list.size() != 1) { list.add(string.format(lastformat, ba)); } } /** * arrage strings in list in triangular manner. * @param list list of strings. */ private list<string> editlisttobeintriangleshape(final list<string> list) { final list<string> returnlist = new linkedlist<string>(); (int = list.size(); > 0; i--) { string s = list.get(list.size()-i); int possition = list.size()*2 + s.length()/2; returnlist.add(string.format("%"+possition+"s", s)); } return returnlist; }
out put of :
1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 4 4 4 4 3 2 1 1 2 3 3 3 3 3 3 3 3 3 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Comments
Post a Comment