java吧 关注:1,250,156贴子:12,734,510
  • 4回复贴,共1

这两题不会,新手小白,求代码指导。。。

只看楼主收藏回复

1.
An integer 12310 could be split into two integers, for examples 1 and 2310, or 12 and 310, or 123 and10, or 1231 and 0. If we multiply the corresponding split integers, the results are 1 x 2310 = 2310, 12 x310 = 3720, 123 x 10 = 1230, and 1231 x 0 = 0. Here, 3720 is the largest result of multiplying two splitintegers of 12310.
Write a Java program to read an integer from the user, and print out the largest value of themultiplication of its split integers. In our previous example, the output should be 3720. Some examples:
Input: 10011
The largest output is 1100 = 100*11
Input: 222222
The largest output is 49284 = 222*222
2.When we use the statement int j = (int) (Math.random() * 2), the probability of j=0 equals to theprobability of j=1, which is 0.5. Write a Java method that randomly returns 0 or 1 but with theprobabilities set by a user. For example, when a user set the probability of 0 to 0.2, your method willreturn 1 with probability 0.8. Write a test program that outputs 20 random numbers (0 or 1) accordingto the probability given by the user. For example:
Enter the probability of generating zero: 0.2Output: 1 1 1 0 1 1 0 1 1 1 1 1 1 0 1 0 1 1 0 1
Enter the probability of generating zero: 0.01Output: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


1楼2017-12-17 13:42回复
    么看明白,这些是报的错误吗?


    来自iPhone客户端2楼2017-12-17 17:15
    回复
      第一个:
      public static int getMax(int value) {
      String number = String.valueOf(value);
      int rtnValue = 0;
      for (int i = 1, j = number.length(); i < j; i++) {
      int left = Integer.valueOf(number.substring(0, i));
      int right = Integer.valueOf(number.substring(i, j));
      if (rtnValue < left * right) {
      rtnValue = left * right;
      }
      }
      return rtnValue;
      }


      IP属地:北京3楼2017-12-17 20:19
      回复
        第二个:
        public static void getRandom(double probability){
        int pro = (int) (probability*10);
        for(int i = 0 ;i<20;i++){
        int value = (int)(Math.random()*10)<pro?0 :1;
        System.out.print(value+" ");
        }
        }


        IP属地:北京5楼2017-12-17 20:31
        回复