java - how to make an method return both an integer array and another integer -
public class solution { public static void main(string[] args) throws ioexception { bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); int num = 6; int[] sticks = new int[num]; string line = br.readline(); string[] strs = line.trim().split("\\s+"); for(int i=0;i<num;i++) { sticks[i] = integer.parseint(strs[i]); } recursion(sticks,num); } public static void recursion(int[] sticks,int num) { int min_num = findmin(sticks,num); int cut=0; for(int i=0;i<num;i++) { sticks[i] = sticks[i] - min_num; cut++; } int[] res = minarray(sticks,num); system.out.println(cut); int len = res.length; while(len != 0) { recursion(res,len); } } public static int findmin(int[] arr,int size) { int minimum = arr[0]; for(int i=0;i<size;i++) { if(arr[i] < minimum) { minimum = arr[i]; } } return minimum; } public static int[] minarray(int[] arr, int size) { int count=0; int flag =0; for(int i=0;i<size;i++) { if(arr[i] != 0) { count++; } } if(count == 0) { flag = 1; } else { flag = 0; } int[] new_array = new int[count]; if(count != 0) { int j=0; for(int i=0;i<size;i++) { if(arr[i] != 0) { new_array[j] = arr[i]; j++; if(j > count) { break; } } } } return new_array; } }
here minarray pass flag variable recursion method can check base class
so program tries enter 6 sticks length among 6 sticks length try find minimum length , subtract length remaining sticks lead 1 or more sticks minimum length become 0 extract non 0 elements , generate array , repeat same until sticks have exhausted. meanwhile m printing number of sticks been cut each minimum stick length.
so, if input 5 4 4 2 2 8
output : 6 4 2 1
Comments
Post a Comment