一生一世陪你


私信TA

用户名:wodeshijie

访问量:23434

签 名:

我要好好地写代码..........

等  级
排  名 428
经  验 4926
参赛次数 1
文章发表 19
年  龄 10
在职情况 学生
学  校 河南农业大学
专  业

  自我简介:

我就是我,可能不是太好,但是我就是我,谁也不能取代........

        题目描述:

                        说白了,其实就是求a和b不能组成的最大数;


       题目解析:

                        这个题我看了网络上给的答案,可是我感觉有点投机取巧的感觉了,答案是直接给输出来的,

                直接就a*b-a-b,可是这个答案一般人都不知道的,所以我用动态规划来解决这个题,下面我来介绍

                一下我的方法,其实就是我打个比方,定义一个长度为100的array数组,并且给他们全部都赋值为0

                如下所示:

                   0 0 0 0 0 0 0 0 0 0 0 

                  1 2 3 4 5 6 7 8 9 10 11

                  打个比方就是输入的是a=3,b=5;

                  那么就把array[3]=1,array[5]=1;

                  然后我们可以遍历这个数组的,如果被遍历的数组的值是1的话,那么我们把array[i+a]和

                   array[i+b]都赋值为1,赋值为1的意思就是这个数可以被a和b给组合起来的,

                  但是为了找到最后的大数,我们还要定义两个数number,count;

                    number是用来表示数组的下标的,而count是用来表示相邻的数组的值为1的个数,

                    在上面你会发现一旦count大于或等于a,b中的大数的时候就可以了,这时候的number表示的下                           标就可以表示我们要找的数了


                    代码如下:

                                    

                                    import java.util.Arrays;
                                    import java.util.Scanner;

                                        public class Main {
                                                  public static void main(String[] args) {
                                                       Scanner m=new Scanner (System.in);
   
                                                       int a=m.nextInt();
                                                       int b=m.nextInt();
                                                       int max=a>b?a:b;
                                                       long array[]=new long[9999999];
   
                                                       Arrays.fill(array,0);
                                                       array[a]=1;
                                                       array[b]=1;
       
                                                        int number=0;
                                                        int count=0;
                                                       for(int k=1;k<array.length;k++)
                                                           {
                                                                if(array[k]==0)
                                                                    {
                                                                     number=k;
                                                                     count=0;
                                                                    }
                                                                else
                                                                {
                                                                     array[k+a]=1;
                                                                     array[k+b]=1;
                                                                     count++;
                                                                        }
                                                        if(count==max)
                                                             break;
                                                               }
                                                       System.out.println(number);
                                                          }
                                                        }



                        

 

0.0分

16 人评分

  评论区

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();

        int[] num = new int[a * b + a + b];
        num[a] = 1;
        num[b] = 1;
        int max = 0;
        for (int i = 0; i <= a*b; i++) {
            if (num[i] == 1) {
                num[i + a] = 1;
                num[i + b] = 1;
            }else {
                max = i;
            }
        }
        System.out.println(max);

    }
2020-10-16 21:39:45
为什么count大于较大得数就行啊
2020-10-07 21:06:30
其实只需要count≥min{a,b}就行了
2020-09-09 22:50:59
如果两个数都是偶数,那所有的奇数岂不是都不能被组合,我傻了....
2020-05-04 20:24:06
  • «
  • 1
  • »