刚刚做完的爱奇艺编程题,有没有大佬会做的??求教学~
//小C有一张票,这张票的ID是长度为6的字符串,每个字符都是数字,他想让这个ID变成他的辛运ID,所以他就开始更改ID,每一次操作,他可以选择任意一个数字并且替换它。
#爱奇艺##笔试题目#
//如果这个ID的前三位数字之和等于后三位数字之和,那么这个ID就是辛运的。你帮小C求一下,最少需要操作几次,能使ID变成辛运ID
输入/
输入只有一行,是一个长度为6的字符串。
输出
输出这个最小操作次数
样例输入
000000
样例输出
0
Hint
输入样例2
000018
输出样例2
1
---------------------------------------------------------------------------无情分割线-----------------------------------------
我的代码,只有18%的正确率,嘤嘤嘤。。。
package aiqiyi;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int str = scan.nextInt();
int [] numarr = new int[6];
for(int i = 0; i < 6; i++){
numarr[i] = str%10;
str /= 10;
}
getDoNum(numarr);
}
public static void getDoNum(int [] numarr){
int sum;
if(numarr[0]+numarr[1]+numarr[2] == numarr[3]+numarr[4]+numarr[5]){
System.out.println(0);
}
else{
sum = doCount(numarr);
System.out.println(sum);
}
}
public static int doCount(int [] numarr){
int sum = 0;
int left = numarr[0]+numarr[1]+numarr[2];
int right = numarr[3]+numarr[4]+numarr[5];
int lack = left - right;
if(lack > 0){
if((9 - numarr[0] > Math.abs(lack)) || (9 - numarr[1] > Math.abs(lack)) || (9 - numarr[2] > Math.abs(lack))){
return 1;
}
}
return sum;
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int str = scan.nextInt();
int [] numarr = new int[6];
for(int i = 0; i < 6; i++){
numarr[i] = str%10;
str /= 10;
}
getDoNum(numarr);
}
public static void getDoNum(int [] numarr){
int sum;
if(numarr[0]+numarr[1]+numarr[2] == numarr[3]+numarr[4]+numarr[5]){
System.out.println(0);
}
else{
sum = doCount(numarr);
System.out.println(sum);
}
}
public static int doCount(int [] numarr){
int sum = 0;
int left = numarr[0]+numarr[1]+numarr[2];
int right = numarr[3]+numarr[4]+numarr[5];
int lack = left - right;
if(lack > 0){
if((9 - numarr[0] > Math.abs(lack)) || (9 - numarr[1] > Math.abs(lack)) || (9 - numarr[2] > Math.abs(lack))){
return 1;
}
}
return sum;
}
}