najimCseJu

bitcoin_brute_force

Jul 27th, 2018
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. package com.example.cefaloschoolproject.commands;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.     static class Transaction {
  9.         private int id;
  10.         private long size;
  11.         private double fee;
  12.         public Transaction(int id, long size, double fee) {
  13.             this.id = id;
  14.             this.size = size;
  15.             this.fee = fee;
  16.         }
  17.  
  18.         public int getId() {
  19.             return id;
  20.         }
  21.  
  22.         public void setId(int id) {
  23.             this.id = id;
  24.         }
  25.  
  26.         public long getSize() {
  27.             return size;
  28.         }
  29.  
  30.         public void setSize(long size) {
  31.             this.size = size;
  32.         }
  33.  
  34.         public double getFee() {
  35.             return fee;
  36.         }
  37.  
  38.         public void setFee(double fee) {
  39.             this.fee = fee;
  40.         }
  41.     }
  42.     public static void main(String[] args){
  43.         Scanner sc = new Scanner(System.in);
  44.         List<Transaction> transactions = new ArrayList<>();
  45.         int toalNumberOfTransactions = 12;
  46.         for(int i =0;i<toalNumberOfTransactions;i++){
  47.             String line = sc.nextLine();
  48.             String[] tokens = line.split(" ");
  49.             int id = Integer.parseInt(tokens[0]);
  50.             long byteSize = Long.parseLong(tokens[1]);
  51.             double fee = Double.parseDouble(tokens[2]);
  52.             transactions.add(new Transaction(id,byteSize,fee));
  53.         }
  54.         int resultSet = 0;
  55.         long maxBytes = 1000000;
  56.         double result = 0;
  57.         double eps = 1e-15;
  58.         double defaultReward = 12.5;
  59.         for(int mask=0;mask<(1<<toalNumberOfTransactions);mask++){
  60.             long byteForCurrentSet = 0;
  61.             double currentScore = 0;
  62.             for(int j=0;j<toalNumberOfTransactions;j++)
  63.             {
  64.                 Transaction t = transactions.get(j);
  65.                 if((mask&(1<<j))!=0){
  66.                     byteForCurrentSet+=t.getSize();
  67.                     currentScore+=t.getFee();
  68.                 }
  69.             }
  70.             if(byteForCurrentSet<=maxBytes && result+eps<currentScore){
  71.                 result = currentScore;
  72.                 resultSet = mask;
  73.             }
  74.         }
  75.         System.out.println("Current Score = " + (result+defaultReward ) + "BTC");
  76.         long sizeTaken = 0;
  77.         for(int i=0;i<toalNumberOfTransactions;i++)
  78.         {
  79.             if((resultSet&(1<<i))!=0){
  80.                 Transaction t = transactions.get(i);
  81.                 sizeTaken+=t.getSize();
  82.                 System.out.println(String.format("Id = %d Size = %d Fee = %f",t.getId(),t.getSize(),t.getFee()));
  83.             }
  84.         }
  85.         System.out.println(String.format("Size taken = %d",sizeTaken));
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment