http://hihocoder.com/contest/offers49/problems
题目1 : 相似颜色
Java写的真的麻烦
package l491;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next().substring(1);
List l = new ArrayList();
String[] ss = {"00","11","22","33","44","55","66","77","88","99",
"aa","bb","cc","dd","ee","ff"};
for(int i=0;i<16;i++)
for(int j=0;j<16;j++)
for(int k=0;k<16;k++)
l.add(ss[i]+ss[j]+ss[k]);
String res="000000";
int d = 999999999;
int[] t2= new int[]{Integer.valueOf(s.substring(0,2),16), Integer.valueOf(s.substring(2,4),16), Integer.valueOf(s.substring(4,6),16)};
for(String t : l) {
int[] t1= new int[]{Integer.valueOf(t.substring(0,2),16), Integer.valueOf(t.substring(2,4),16), Integer.valueOf(t.substring(4,6),16)};
int tmp=0;
for(int i=0; i<3; i++)
tmp+=(t1[i]-t2[i])*(t1[i]-t2[i]);
if(tmp<=d) {
res=t;
d=tmp;
}
}
System.out.println("#"+res.charAt(0)+res.charAt(2)+res.charAt(4));
}
}
题目2 : 挑选子集
先定下最小的数(没有什么是一层循环解决不了的),然后求个组合数,注意数据溢出
package l492;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt(),m=sc.nextInt(),k=sc.nextInt();
int[]a=new int[n];
for(int i=0;i
题目3 : 矩阵迷宫
i,j位置肯定要出现在DP数组中,还有朝向,转了多少次(因为转到后面cost更大),所以一共4个维度,dp[i][j][k][l]表示到ij位置,朝向为k,已经转了l次,知道Aij最大100,所以cost最大也就100199+1,所以不能一直转,最多也就转15次,因为2^15=102432>19901了
package l493;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[][]a=new int[n][n];
for(int i=0;i