Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6.  
  7. //Cut the sticks exercise
  8.  
  9. public class Solution {
  10.  
  11. public static void main(String[] args) {//reads and formats the input
  12. Scanner in= new Scanner(System.in);
  13. int stick_size= Integer.parseInt(in.nextLine());
  14. int[] sticks= new int[stick_size];
  15. int item;
  16. String next= in.nextLine();
  17. String[] next_split= next.split(" ");
  18. for(int i=0; i<stick_size; i++){
  19. item=Integer.parseInt(next_split[i]);
  20. sticks[i]=item;
  21. }
  22. cut(sticks);
  23. }
  24.  
  25. public static void cut(int[] sticks){//gets the number of cuts
  26. while(sticks.length>0){
  27. int mini= findMin(sticks);
  28. System.out.println(sticks.length);
  29. minus(sticks, mini);
  30. sticks= removeZeros(sticks);
  31. }
  32. }
  33. public static int findMin(int[] in){//finds the smallest number
  34. int min= in[0];
  35. for(int i: in){
  36. if(i<min) min= i;
  37. }
  38. return min;
  39. }
  40. public static void minus(int[] input, int min){//takes off the number
  41. for(int i=0; i<input.length; i++){
  42. input[i]=input[i]-min;
  43. }
  44. }
  45. public static int[] removeZeros(int[] in){//mutates the array
  46. int j=0;
  47. for(int i=0; i<in.length; i++){
  48. if(in[i]!=0) in[j++]=in[i];
  49. }
  50. int[] newArray= new int[j];
  51. System.arraycopy(in, 0, newArray,0,j);
  52. return newArray;
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement