test.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.test.context.junit4.SpringRunner;
  6. import platform.common.util.DateUtil;
  7. import java.text.ParseException;
  8. import java.util.*;
  9. /**
  10. * Created by luohaifeng on 2017/10/31.
  11. */
  12. @RunWith(SpringRunner.class)
  13. @SpringBootTest()
  14. public class test {
  15. @Test
  16. public void propsTest() throws ParseException {
  17. System.out.println( DateUtil.getPayTime(3,"2017-11-10"));
  18. }
  19. @Test
  20. public void lis() throws ParseException {
  21. char[] chs = {'a','b','c','d'};
  22. comb(chs);
  23. }
  24. public static void comb(char[] chs) {
  25. int len = chs.length;
  26. int nbits = 1 << len;
  27. for (int i = 0; i < nbits; ++i) {
  28. int t;
  29. for (int j = 0; j < len; j++) {
  30. t = 1 << j;
  31. if ((t & i) != 0) { // 与运算,同为1时才会是1
  32. System.out.print(t+"---"+i);
  33. System.out.print(chs[j]);
  34. }
  35. }
  36. System.out.println();
  37. }
  38. }
  39. @Test
  40. public void test() throws ParseException {
  41. System.out.println( DateUtil.isGenerate("2017-12-10",3));
  42. }
  43. public static void main(String[] args) {
  44. Scanner in = new Scanner(System.in);
  45. System.out.println("Please enter the number of objects(请输入物品的数量:):");
  46. int n = in.nextInt();
  47. int[] w = new int[n]; // 物品重量数组
  48. int[] v = new int[n]; // 物品价钱数组
  49. System.out
  50. .println("Now, please enter the weight of these objects(现在请输入这些物品的重量:)");
  51. for (int i = 0; i < n; i++) {
  52. w[i] = in.nextInt();
  53. }
  54. System.out
  55. .println("Now, please enter the value of these objects(现在请输入这些物品的价值:)");
  56. for (int i = 0; i < n; i++) {
  57. v[i] = in.nextInt();
  58. }
  59. System.out
  60. .println("Now, please enter the capacity of the pack(现在请输入背包的容量:)");
  61. int c = in.nextInt();
  62. /**
  63. * 按单位重量价值r[i] = v[i] / w[i]降序排列
  64. *
  65. * ps:排序用到了选择排序,详情请查看选择排序
  66. */
  67. double startTime = System.currentTimeMillis();
  68. double[] r = new double[n];
  69. int[] index = new int[n];
  70. for (int i = 0; i < n; i++) {
  71. r[i] = (double) v[i] / (double) w[i];
  72. index[i] = i;
  73. }
  74. double temp = 0;
  75. for (int i = 0; i < n - 1; i++) {
  76. for (int j = i + 1; j < n; j++) {
  77. if (r[i] < r[j]) {
  78. temp = r[i];
  79. r[i] = r[j];
  80. r[j] = temp;
  81. int x = index[i];
  82. index[i] = index[j];
  83. index[j] = x;
  84. }
  85. }
  86. }
  87. /**
  88. * 排序后的重量和价值分别存到w1[]和v1[]中
  89. */
  90. int[] w1 = new int[n];
  91. int[] v1 = new int[n];
  92. for (int i = 0; i < n; i++) {
  93. w1[i] = w[index[i]];
  94. v1[i] = v[index[i]];
  95. }
  96. /**
  97. * 初始化解向量x[n]
  98. */
  99. int[] x = new int[n];
  100. for (int i = 0; i < n; i++) {
  101. x[i] = 0;
  102. }
  103. /**
  104. * 求解并打印解向量
  105. */
  106. for (int i = 0; i < n; i++) {
  107. if (w1[i] < c) {
  108. x[i] = 1;
  109. c = c - w1[i];
  110. }
  111. }
  112. System.out
  113. .println("The solution vector is(解向量是:)" + Arrays.toString(x));
  114. /**
  115. * 根据解向量求出背包中存放物品的最大价值并打印
  116. */
  117. int maxValue = 0;
  118. for (int i = 0; i < n; i++) {
  119. if (x[i] == 1)
  120. maxValue += v1[i];
  121. }
  122. double endTime = System.currentTimeMillis();
  123. System.out
  124. .println("Now, the largest values of objects in the pack is(背包中物品的最大价值为:)"
  125. + maxValue);
  126. System.out.println("Basic Statements take(基本语句用时)"
  127. + (endTime - startTime) + " milliseconds!");
  128. }
  129. }