Advertisement
Guest User

TIRA 1.7 with bitwise operations

a guest
Jan 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import static java.lang.reflect.Array.set;
  2. import java.util.ArrayList;
  3.  
  4.  
  5.  
  6. public class Kolikot {
  7.  
  8. private ArrayList <Integer> kolikot;
  9.  
  10. public Kolikot() {
  11.  
  12. this.kolikot = new ArrayList<>();
  13. }
  14.  
  15. public void lisaaKolikko(int arvo) {
  16.  
  17. this.kolikot.add(arvo);
  18. }
  19.  
  20. public int laskeYhdistelmat(int summa) {
  21.  
  22. ArrayList <Boolean> booleanTaulukko = new ArrayList<>();
  23. ArrayList <Integer> taulukko = new ArrayList<>();
  24.  
  25. int maara = 0;
  26.  
  27.  
  28. int pituus = this.kolikot.size();
  29. for (int i = 0; i < (1 << pituus); i++) { //i < 16
  30. int summakaksi = 0;
  31. for(int j = 0; j < pituus; j++) {
  32.  
  33. if ((i & (1 << j)) > 0) {
  34.  
  35. taulukko.add(this.kolikot.get(j));
  36. summakaksi += this.kolikot.get(j);
  37. }
  38. }
  39. if (summakaksi == summa) {
  40. booleanTaulukko.add(true);
  41. }
  42.  
  43. }
  44.  
  45. return booleanTaulukko.size();
  46. }
  47.  
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement