百度测开暑期0410笔试交流
优惠券这道题a了27%,实在想不懂哪里的问题,佬有思路的欢迎指正。
题目描述:在L-R闭区间内,取一个整数x,优惠计算方式=x *(x的各位数字之和),问最大的优惠是多少?
例:[3,6],取3时,优惠为3*3=9,取4时,优惠为4*4=16,...因此最大优惠为6*6=36
再比如[28,31],取29时最大,优惠为29*(2+9)= 319
边界:1 <= L <= R <= 1e5
Scanner cin = new Scanner(System.in); int l = cin.nextInt(); int r = cin.nextInt(); int ge = 0; int shi = 0; int bai = 0; int qian = 0; int wan = 0; long max = 1; for(int i = l;i <= r;i++){ wan = i / 10000 % 10; qian = i / 1000 % 10; bai = i / 100 % 10; shi = i / 10; ge = i % 10; max = Math.max(max,i * (ge + shi + bai + qian + wan)) } System.out.print(max);#百度笔试##百度暑期实习#