hdu1272 java 穷举法
2016-06-01 11:20
281 查看
import java.util.Scanner;
public class P1172 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while (sc.hasNext()) {
int n=sc.nextInt();
if(n==0){
return;
}
int a[]=new int[n+1];
int b[]=new int[n+1];
int c[]=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=sc.nextInt();
b[i]=sc.nextInt();
c[i]=sc.nextInt();
}
int count=0;
int result = 0;
boolean flag = false;
for(int i=1000;i<=9999;i++){
for(int j=1;j<=n;j++){
flag=judge(i,a[j],b[j],c[j]);
if(!flag){
break;
}
}
if (flag) {
count++;
result = i;
}
}
if(count==1){
System.out.println(result);
}else{
System.out.println("Not sure");
}
}
}
private static boolean judge(int number, int a, int b, int c) {
int num1[]=new int[5];
int num2[]=new int[5];
boolean mark[]=new boolean[5];
for(int i=0;i<=4;i++){
mark[i]=false;
}
num1[1]=number/1000;
num1[2]=(number/100)%10;
num1[3]=(number/10)%10;
num1[4]=(number%10);
num2[1]=a/1000;
num2[2]=(a/100)%10;
num2[3]=(a/10)%10;
num2[4]=(a%10);
int count=0;
for(int i=1;i<=4;i++){
if(num1[i]==num2[i]){
count++;
}
}
if(count!=c){
return false;
}
count=0;
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
if(num1[i]==num2[j]&&!mark[j]){
mark[j]=true;
count++;
break;
}
}
}
if(count!=b){
return false;
}
return true;
}
}
public class P1172 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while (sc.hasNext()) {
int n=sc.nextInt();
if(n==0){
return;
}
int a[]=new int[n+1];
int b[]=new int[n+1];
int c[]=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=sc.nextInt();
b[i]=sc.nextInt();
c[i]=sc.nextInt();
}
int count=0;
int result = 0;
boolean flag = false;
for(int i=1000;i<=9999;i++){
for(int j=1;j<=n;j++){
flag=judge(i,a[j],b[j],c[j]);
if(!flag){
break;
}
}
if (flag) {
count++;
result = i;
}
}
if(count==1){
System.out.println(result);
}else{
System.out.println("Not sure");
}
}
}
private static boolean judge(int number, int a, int b, int c) {
int num1[]=new int[5];
int num2[]=new int[5];
boolean mark[]=new boolean[5];
for(int i=0;i<=4;i++){
mark[i]=false;
}
num1[1]=number/1000;
num1[2]=(number/100)%10;
num1[3]=(number/10)%10;
num1[4]=(number%10);
num2[1]=a/1000;
num2[2]=(a/100)%10;
num2[3]=(a/10)%10;
num2[4]=(a%10);
int count=0;
for(int i=1;i<=4;i++){
if(num1[i]==num2[i]){
count++;
}
}
if(count!=c){
return false;
}
count=0;
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
if(num1[i]==num2[j]&&!mark[j]){
mark[j]=true;
count++;
break;
}
}
}
if(count!=b){
return false;
}
return true;
}
}
相关文章推荐
- Java的MyBatis框架项目搭建与hellow world示例
- Json字符串转换为java对象的各种实现方法
- Java HashSet源码解析
- JDK结构介绍
- struts2入门
- Spring学习——Spring常用数据库访问设置
- SpringMVC实现上传和下载
- java8 List转成TreeSet
- java 如何合理的序列化对象
- Shiro 整合SpringMVC 并且实现权限管理,登录和注销
- java虚拟机内存分布
- java版-金额转大写(可处理千万亿,精确到分)
- 华为机试---放苹果
- eclipse下vim插件 打开文件目录
- java获取类加载路径
- 个人收藏的文章java
- Eclipse4.5Mars安装JAD反编译插件
- JavaEE主流框架的总结
- MyEclipse 快捷键
- 利用Java的MyBatis框架获取MySQL中插入记录时的自增主键