hdu1270小希的数表-枚举(java)
作者:互联网
关键点:
1.通过构造将num[2]+num[3]转化为sum[i],使得枚举num[2]+num[3]的过程转化为遍历sum[i]的过程
2.通过删减子区间来获得重叠子问题,以更新num
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class Main {
private static int[] sum,num ;
private static boolean[] vis ;
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))) ;
while(in.nextToken()!=StreamTokenizer.TT_EOF){
int n = (int)in.nval ;
if(n==0)break ;
int m = (n-1)*n /2 ;
sum = new int[110*110] ;
num = new int[105] ;
vis = new boolean[110*110] ;
for (int i = 1; i <= m; i++) {
in.nextToken() ;
sum[i] = (int)in.nval ;
}
for(int i=3;i<=m;i++){
num[2] = (sum[1]-sum[2]+sum[i]) / 2 ;
num[1] = sum[1] - num[2] ;
num[3] = sum[2] - num[1] ;
if(num[2]+num[3]!=sum[i])continue ;
Arrays.fill(vis,false) ;
int start = 3 ;
vis[i] = true ;
boolean flag = true ;
for(int j=4;j<=n&&flag;j++){
while(vis[start])start++ ;
num[j] = sum[start] - num[1] ;
vis[start] = true ;
for(int k=2;k<j&&flag;k++){
for(int x=start+1;x<=m;x++){
flag = false ;
if(!vis[x]&&num[k]+num[j]==sum[x]){
vis[x] = true ;
flag = true ;
break ;
}
}
}
}
if(flag)break ;
}
for(int i=1;i<=n;i++){
if(i!=n){
System.out.print(num[i]+" ") ;
}else{
System.out.println(num[n]) ;
}
}
}
}
}
标签:小希,java,hdu1270,start,int,sum,vis,num 来源: https://blog.csdn.net/sukisabishi/article/details/115426929