艺龙题解JAVA
艺龙
第一题:幸运数列
4
1 4 2 3
"Luck sequence"
5
6 -1 5 3 7
"Unluck sequence"
描述:第一个数为序列个数,后面n-1个数为序列数
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int len = sc.nextInt();
int[] arr = new int[len];
for(int i=0;i<len ;i++){
arr[i] = sc.nextInt();
}
int[] a = new int[len];
boolean flag = true;
int count = 0;
here:
for(int i=1;i<len;i++){
int now = Math.abs(arr[i]-arr[i-1]);
if(now>=len){
flag = false;
break here;
}else{
if(a[now]==0){
count++;
a[now]=1;
}
}
}
if(a[0]!=0)
flag = false;
System.out.println(flag && count == len-1?"Luck sequence":"Unluck sequence");
}
第二题
1
4
1 10 100 1000
100 10 1000 1
10 10 10 10
50 5 15 55
结果: 1+5+10+1 = 17
第一行为测试用例组数,第二行为n,下面四行每行n个数。求最小权值
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int num= sc.nextInt();
int[] a ; //数据
char[] f ; //方向
while(num-->0){
int n = sc.nextInt();
a = new int[n];
f = new char[n];
Arrays.fill(a,Integer.MAX_VALUE);
for(int i=0;i<4;i++){
for(int j=0;j<n;j++){
int now = sc.nextInt();
int cmp = a[j];
a[j] = Math.min(now, a[j]);
if(a[j]!=cmp){
if(i==0)
f[j] = 'E';
else if(i==1)
f[j] = 'S';
else if(i==2)
f[j] = 'W';
else
f[j] = 'N';
}
}
}
long sum = 0;
for(int i=0;i<a.length;i++){
sum += a[i];
}
System.out.println(String.valueOf(sum));
System.out.println(new String(f));
}
}
2AC,有更好思路的牛友请指出啊。大佬勿喷 (逃
