Poj Solution 1045

http://poj.org/problem?id=1045

 import java.util.Scanner;

 public class Main {

     static double vs = 0;
     static double c = 0;
     static double r = 0;
     static double w = 0;
     static int times = 0;

     public static void main(String[] args) {
         Scanner scan = new Scanner(System.in);
         vs = scan.nextDouble();
         r = scan.nextDouble();
         double r2 = r * r;
         double vsr = vs * r;
         c = scan.nextDouble();
         double c2 = c * c;
         times = scan.nextInt();
         for (int i = 0; i < times; i++) {
             w = scan.nextDouble();
             System.out.printf("%.3fn", vsr / Math.sqrt(r2 + 1 / (w * w * c2)));
         }
     } 
}

							
Posted in poj | Leave a comment

Poj Solution 1043

http://poj.org/problem?id=1043

#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;

struct criminal
{
       char name[21];
       int id;
}crim[20];

int cmp(void const * a,void const * b)                                                                                                     //按姓名升序排序
{
    return strcmp(((criminal *)a)->name,((criminal *)b)->name);
}

int Bipartite(bool decryp[][20],int n,int count)                                                                                           //二分图的匈牙利算法,decryp是原始数组,n是用户ID的个数,count是罪犯的个数(列数)
{
    int i,j,x,qs,qe,q[20],prev[20],ncount=0;                                                                                               //匹配的个数
    int vm1[20],vm2[20];
    for(i=0;i<n;i++)
       vm1[i]=-1;                                                                                                                          //vm1[i]表示在已经匹配的集合中,vm1[i]与i相连,vm1表示用户ID点
    for(i=0;i<count;i++)
       vm2[i]=-1;                                                                                                                          //vm2[i]表示在已经匹配的集合中,vm2[i]与i相连,vm2便是name点
    for(i=0;i<n;i++)
    {
        for(j=0;j<count;j++)
          prev[j]=-2;
        qs=qe=0;
        for(j=0;j<count;j++)
         if(decryp[i][j])                                                                                                                 //与i可能匹配的点
         {
             prev[j]=-1;
             q[qe++]=j;                                                                                                                   //与i有关联的点进入队列q中
         }
        while(qs<qe)                                                                                                                      //寻找一条以i为起点的增广路径
        {
             x=q[qs];
             if(vm2[x]==-1)                                                                                                               //如果找到一个未匹配的点,即有一条增广路径,则进入下一步操作
                break;
             qs++;
             for(j=0;j<count;j++)
              if(prev[j]==-2&&decryp[vm2[x]][j])                                                                                          //如果j还未进入队列,即j=-1,并且vm2[x]到j有路径,则把j放入队列
              {
                  prev[j]=x;
                  q[qe++]=j;
              }
        }
        if(qs==qe)                                                                                                                        //如果没有找到则进行下一步操作
           continue;
        while(prev[x]>-1)                                                                                                                 //如果找到了,进行取反操作
        {
            vm1[vm2[prev[x]]]=x;
            vm2[x]=vm2[prev[x]];                                                                                                          //匹配的点交换,实现取反
            x=prev[x];
        }
        vm2[x]=i;                                                                                                                         //一个连接
        vm1[i]=x;
        ncount++;                                                                                                                         //匹配数加1
    }
    return ncount;
}

int main()
{
    int i,j,n,count;
    bool stay[20];                                                                                                                       //标记罪犯逗留在隐匿处
    bool decryp[20][20];                                                                                                                 //用于二分图计算的原始矩阵
    char op,str[21],buffer[30],id[20][21];
    scanf("%d",&n);
    for(i=0;i<n;i++)
      scanf("%s",id[i]);
    count=0;
    memset(decryp,true,sizeof(decryp));
    memset(stay,false,sizeof(stay));
    gets(buffer);                                                                                                                       //读取上一行数据的回车
    while(gets(buffer)&&buffer[0]!='Q')
    {
         sscanf(buffer,"%c %s",&op,str);
         if(op=='E')                                                                                                                    //罪犯藏于隐匿处
         {
             for(i=0;i<count&&strcmp(crim[i].name,str);i++);                                                                            //查找该罪犯是不是新来的
             if(i==count)                                                                                                               //该罪犯不是新来的
             {
                 strcpy(crim[count].name,str);
                 count++;                                                                                                               //计数
             }
             stay[i]=true;                                                                                                              //逗留在隐匿处
         }
         else if(op=='L')                                                                                                               //罪犯离开隐匿处
         {
              for(i=0;i<count&&strcmp(crim[i].name,str);i++);
              stay[i]=false;                                                                                                            //离开隐匿处
         }
         else                                                                                                                           //拦截到信息
         {
             for(i=0;i<n&&strcmp(id[i],str);i++);                                                                                       //查找用户ID的编号
             for(j=0;j<n;j++)                                                                                                           //所有呆在隐匿处的都有可能
               if(!stay[j])
                 decryp[i][j]=false;
         }
    }
    for(i=0;i<count;i++)                                                                                                                //查找罪犯姓名与用户ID之间的对应关系
    {
         crim[i].id=-1;                                                                                                                 //查找每个罪犯,初值为-1
         for(j=0;j<n&&crim[i].id==-1;j++)
         {
             if(decryp[j][i])                                                                                                           //表示i和j有匹配的可能
             {
                 decryp[j][i]=false;                                                                                                    //假设i和j不匹配
                 if(Bipartite(decryp,n,count)<count)                                                                        //调用匈牙利算法,若匹配数少于人数,则说明i和j必须匹配,否则不符合题意
                    crim[i].id=j;
                 decryp[j][i]=true;                                                                                                     //匹配后要还原
             }
         }
    }
    qsort(crim,count,sizeof(criminal),cmp);                                                                                             //按罪犯的姓名排序
    for(i=0;i<n;i++)
    {
         printf("%s:",crim[i].name);
         if(crim[i].id==-1)
            printf("???");
         else
            printf("%s",id[crim[i].id]);
         printf("n");
    }
    system("pause");
    return 0;
}

							
Posted in poj | Leave a comment

Poj Solution 1042

http://poj.org/problem?id=1042

import java.io.BufferedInputStream;   
import java.util.Arrays;   
import java.util.Scanner;   
public class Main {   
  
    public static void main(String[] args) {   
        Scanner scan = new Scanner(new BufferedInputStream(System.in));   
        while (scan.hasNext()) {   
            int n = scan.nextInt();   
            if (n == 0) {   
                break;   
            }   
            int h = scan.nextInt();   
            h = h * 12; 
            int[] fish = new int[n];   
            int[] decrease = new int[n];   
            int[] interval = new int[n];   
            for (int i = 0; i < n; i++) {   
                fish[i] = scan.nextInt();   
            }   
            for (int i = 0; i < n; i++) {   
                decrease[i] = scan.nextInt();   
            }   
            interval[0] = 0; 
            for (int i = 1; i < n; i++) {   
                interval[i] = scan.nextInt() + interval[i - 1];   
            }   
  
            int[] maxResult = new int[n];   
            int max = maxFish(h, Arrays.copyOf(fish, 1), decrease, maxResult);   
            for (int i = 1; i < n; i++) {   
                if (h <= interval[i]) {   
                    break; 
                }   
                int[] result = new int[n];   
                int m = maxFish(h - interval[i], Arrays.copyOf(fish, i + 1), decrease, result);   
                if (m > max) {   
                    max = m;   
                    maxResult = Arrays.copyOf(result, result.length);   
                }   
            }   
            for (int i = 0; i < maxResult.length; i++) {   
                if (i > 0) {   
                    System.out.print(", ");   
                }   
                System.out.print(maxResult[i] * 5);   
            }   
            System.out.println("");   
            System.out.println("Number of fish expected: " + max);   
            System.out.println("");   
        }   
    }   
  
    public static int maxFish(int time, int[] fish, int[] decrease, int[] result) {   
        int maxNumber = 0;   
        while (time > 0) {   
            int max = fish[0];   
            int maxIndex = 0;   
            for (int i = 1; i < fish.length; i++) {   
                if (fish[i] > max) {   
                    max = fish[i];   
                    maxIndex = i;   
                }   
            }   
            fish[maxIndex] -= decrease[maxIndex]; 
            if (fish[maxIndex] < 0) {   
                fish[maxIndex] = 0; 
            }   
            result[maxIndex]++;   
            maxNumber += max;   
            time--;   
        }   
        return maxNumber;   
    }   
}  


							
Posted in poj | Leave a comment

Poj Solution 1039

http://poj.org/problem?id=1039

 
							
Posted in poj | Leave a comment

Poj Solution 1038

http://poj.org/problem?id=1038

//* @author 
import java.util.*;

public class Main {
 static Scanner in = new Scanner(System.in);
 static int[] p = new int[12];
 static {
  p[0] = 1;
  for(int i=1; i!=p.length; ++i)
    p[i] = 3*p[i-1];
  }
  
  static int get(int s, int i) {
    return s/p[i]%3;
  }

  static int set(int s, int i, int v) {
    return s+(v-get(s, i))*p[i];
  }

  public static void main(String[] args) {
   int t = in.nextInt();
   while(t-->0) {
    int n = in.nextInt();
    int m = in.nextInt();
    if(m>10) while(true);
    boolean[][] map = new boolean[n+1][m+1];
    for(int i=0; i<=n; ++i) map[i][m] = true;
    for(int j=0; j<=m; ++j) map[n][j] = true;
    int k = in.nextInt();
    while(k-->0) map[in.nextInt()-1][in.nextInt()-1] = true;
    int[][] dp = new int[m+1][p[m]];
    dp[0][p[m]-1]=1;
    for(int i=0; i!=n; ++i) {
     for(int j=0; j!=m; ++j) {
      for(int s=0, _s; s!=p[m]; ++s) {
        int x = dp[j][s];
        if(x==0)continue;
        int l = get(s, j);
        _s = set(s, j, Math.min(2, l+1));
        dp[j+1][_s] = Math.max(dp[j+1][_s], x);
        int r = get(s, j+1);
        if(l!=2||r!=2) continue;
        if(map[i+0][j+0]) continue;
        if(map[i+0][j+1]) continue;
        if(map[i+1][j+0]) continue;
        if(map[i+1][j+1]) continue;
        if(!map[i+2][j]&&!map[i+2][j+1]) {
            _s = set(set(s, j, 0), j+1, 0);
            if(j+2>m)while(true);
            dp[j+2][_s] = Math.max(dp[j+2][_s], x+1);
        }
        if(get(s, j+2)==2&&!map[i][j+2]&&!map[i+1][j+2]) {
            _s = set(set(set(s, j, 1), j+1, 1), j+2, 1);
            if(j+3>m)while(true);
            dp[j+3][_s] = Math.max(dp[j+3][_s], x+1);
        }
    }
       }
    int[] tmp = dp[m];
    for(int j=m; j>=1; --j)
      Arrays.fill(dp[j] = dp[j-1], 0);
        dp[0] = tmp;
      }
    int ans = 0;
    for(int s=0; s!=p[m]; ++s)
      ans = Math.max(ans, dp[0][s]);
    System.out.println(ans-1);
  }
 }
}

							
Posted in poj | Leave a comment

Poj Solution 1037

http://poj.org/problem?id=1037

//* @author:
import java.util.*;
public class Main{
 static final int MAX=21;//���20��ľ��
 static long w[][];//w�����еĸ���
 static long m[][];//m�����еĸ���m[x][n]Ϊ��n��Ȳ�ͬ��ľ����ɵ�դ8�У������е�x��ľ��ʼ�ġ�M�������еĸ���
 
 private int n;//ľ��ĸ���
 private long c;//���

 private boolean flag[];
 private boolean print;
 
  static {//��ʼ����̬��
    w=new long[MAX][MAX];
    m=new long[MAX][MAX];
    w[1][1] = 1; m[1][1] = 1;
    w[1][2] = 0; m[1][2] = 1;
    w[2][2] = 1; m[2][2] = 0;
    for (int i = 3; i < MAX; i++)
      for (int j = 1; j < MAX; j++){
        for (int k = 1; k < j; k++) 
          w[j][i] += m[k][i - 1];
        for (int k = j; k < i; k++) 
          m[j][i] += w[k][i - 1];
    }
  }

  public Main(int n,long c){
    this.n=n;
    this.c=c;
    flag=new boolean[MAX];
    print=false;
   }

  private void fand(int k, int m){
    for (int i = 1; i <= m; i++) 
     if (flag[i] && i <= k){
        k++; m++;
     }
    flag[k] = true;
    if (print) System.out.print(" "+k);
    else{
        System.out.print(k);
        print = true;
    }
}

 private void doIt(){
   int k = 1;//��������ǰ���ľ������
        print = false;
        boolean direct = true, first = true;
        while(n!=0){
            if (direct)
                if (c > w[k][n]){//����Ŵ����Ե�k��ľ��ʼ�ġ�w�������еĸ���˵��������������Щ����֮��
                    c -= w[k++][n];//���±�ţ�ͬʱ��һ��ľ��
                    if (first) {direct = false; k--;}//����Dz��ҵ�һ��ľ���Ҫ��"m"������
                }else{
                    fand(k, n--);//�ҵ�k,����ľ�������1
                    first = false;
                    direct = false;
                    k = 1;
                }
            else
                if (c > m[k][n]){//����Ŵ����Ե�k��ľ��ʼ�ġ�m�������еĸ���˵��������������Щ����֮��
                    c -= m[k++][n];//���±�ţ�ͬʱ��һ��ľ��
                    if (first) direct = true;//���ҵ�һ��ľ�����
                }else{
                    fand(k, n--);
                    first = false;
                    direct = true;
                }
        }
        System.out.println();
    }

public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    int t, n;
     long c;
     t=sc.nextInt();;
    while((t--)!=0){
        n=sc.nextInt();
        c=sc.nextLong();
        Main m=new Main(n,c);
        m.doIt();
    }
}

 
}

							
Posted in poj | Leave a comment

Poj Solution 1035

http://poj.org/problem?id=1035

//* @author 
import java.io.*; 
import java.util.*; 

public class Main 
{ 
    static ArrayList< Item> dicts = new ArrayList< Item>(); 

    public static void main(String[] args) throws Exception 
    { 
        readFile(); 
    } 

    static void readFile() throws Exception 
    { 
        BufferedReader br = new BufferedReader( 
            //new FileReader("in.in")); 
            new InputStreamReader(System.in)); 
        StringTokenizer st = null; 
        String temp = null; 
        int iCount = 1; 
        while(!(temp=br.readLine()).equals("#")) 
            dicts.add(new Item(iCount++,temp)); 
        int flag = 0; 
        while(!(temp=br.readLine()).equals("#")) 
        { 
            if(flag!=0) 
                System.out.println(); 
            process(temp); 
            flag++; 
        } 
    } 

    static void process(String str) 
    { 
        ArrayList< Item> result = new ArrayList< Item>(); 
        ArrayList< Item> temp = null; 
        if(isHave(str)) 
        { 
            System.out.print(str+" is correct"); 
            return; 
        } 
        temp = getArrayList(str.length()+1); 
        for(Item it : temp) 
            if(judge(str,it.str)) 
                result.add(it); 
        temp = getArrayList(str.length()-1); 
        for(Item it : temp) 
            if(judge(str,it.str)) 
                result.add(it); 

        temp = getArrayList(str.length()); 
        for(Item it : temp) 
            if(judge1(str,it.str)) 
                result.add(it); 

        Collections.sort(result); 
        System.out.print(str+":"); 
        if(result.size()!=0) 
            System.out.print(" "); 
        int flag = 0; 
        for(Item it : result) 
        { 
            if(flag!=0) 
                System.out.print(" "); 
            System.out.print(it.str); 
            flag++; 
        } 
    } 

    static boolean judge1(String src,String dest) 
    { 
        int i = 0; 
        int count = 0; 
        while(i< src.length()) 
        { 
            if(src.charAt(i)!=dest.charAt(i)) 
            { 
                count++; 
            } 
            if(count>1) 
                return false; 
            i++; 
        } 
        return true; 
    } 

    static boolean judge(String src,String dest) 
    { 
        String min = dest; 
        String max = src; 
        if(src.length()< dest.length()) 
        { 
            min = src; 
            max = dest; 
        } 
        int count = 0; 
        int i = 0; 
        int j = 0; 
        while(i< max.length()&&j< min.length()) 
        { 
            if(max.charAt(i)!=min.charAt(j)) 
            { 
                count++; 
                i++; 
            } 
            else 
            { 
                i++; 
                j++; 
            } 
            if(count>1) 
                return false; 
        } 
        return true; 
    } 

    static boolean isHave(String str) 
    { 
        for(Item it : dicts) 
        { 
            if(it.str.equals(str)) 
                return true; 
        } 
        return false; 
    } 

    static ArrayList< Item> getArrayList(int n) 
    { 
        ArrayList< Item> result = new ArrayList< Item>(); 
        for(Item it : dicts) 
        { 
            if(it.str.length()==n) 
                result.add(it); 
        } 
        return result; 
    } 

    static void display() 
    { 
    } 

    static class Item implements Comparable< Item> 
    { 
        int index; 
        String str; 
        Item(int i,String s) 
        { 
            index = i; 
            str = s; 
        } 
        public int compareTo(Item it) 
        { 
            if(index>it.index) 
                return 1; 
            else if(index< it.index) 
                return -1; 
            else  
                return 0; 
        } 
    } 
}
							
Posted in poj | Leave a comment

Poj Solution 1032

http://poj.org/problem?id=1032

//* @author: 82638882@163.com
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
        int[] u=new int[]{
        1,2,5,9,14,20,27,35,44,54,65,77,90,104,119,135,152,170,189,209,230,252,275,299,324,
        350,377,405,434,464,495,527,560,594,629,665,702,740,779,819,860,902,945,989,1034,1111
        };
        int a=in.nextInt();
        int b=0;
        int k=22;
        int m=0;
        int h=45;
        while(!(a< u[k+1]&&a>=u[k]))
        {
            if(a>=u[k+1])
                {
                    m=k;
                    k=(k+h)/2;
                }
            else if(a< u[k])
            {
                h=k;
                k=(m+k)/2;
            }
        }
        b=a-u[k];
        int e=b/k;
        b=k-b%k;
        for(int i=2+e;i< k+e+2;i++)
        {
            if(b>0)
            System.out.print(i);
            else System.out.print((i+1));
            if(i!=k+e+1)System.out.print(" ");
            b--;
        }
    }
}
							
Posted in poj | Leave a comment

Poj Solution 1028

http://poj.org/problem?id=1028

  import java.util.Scanner;
 import java.util.Stack;

 public class Main {

     String command;
     Stack<String> back = new Stack<String>();
     Stack<String> forward = new Stack<String>();
     String current = "http://www.acm.org/";
     String display;

     public Main() {
         Scanner scan = new Scanner(System.in);
         while (!(command = scan.next()).equals("QUIT")) {
             if (command.equals("BACK")) {
                 doBack();
             } else if (command.equals("FORWARD")) {
                 doForward();
             } else if (command.equals("VISIT")) {
                 doVisit(scan);
             }
             System.out.println(display);
         }
     }

     public void doBack() {
         if (back.isEmpty()) {
             display = "Ignored";
         } else {
             forward.push(current);
             current = back.pop();
             display = current;
         }
     }

     public void doForward() {
         if (forward.isEmpty()) {
             display = "Ignored";
         } else {
             back.push(current);
             current = forward.pop();
             display = current;
         }
     }

     public void doVisit(Scanner scan) {
         if (current != null) {
             back.push(current);
         }
         current = scan.next();
         display = current;
         forward.clear();
     }

     public static void main(String[] args) {
         new Main();
     }

}

							
Posted in poj | Leave a comment

Poj Solution 1026

http://poj.org/problem?id=1026

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.Arrays;

 public class Main {

     public static void main(String[] args) throws NumberFormatException,
             IOException {
         BufferedReader read = new BufferedReader(new InputStreamReader(
                 System.in));
         int len;
         int[] n;
         int[] loop;
         String[] s;
         int times;
         int temp;
         String str;
         char[] result;
         int r;
         while ((len = Integer.parseInt(read.readLine())) != 0) {
             n = new int[len];
             loop = new int[len];
             result = new char[len];
             s = read.readLine().split(" ");
             for (int i = 0; i < len; i++) {
                 n[i] = Integer.parseInt(s[i]);
             }
             findLoop(n, loop);
             str = read.readLine();
             while ((times = Integer.parseInt(str.split(" ")[0])) != 0) {
                 Arrays.fill(result, ' ');
                 str = str.substring(str.indexOf(" ") + 1);
                 for (int i = 0; i < str.length(); i++) {
                     temp = times % loop[i];
                     r = i;
                     if (temp > 0) {
                         r = n[i];
                         for (int k = 1; k < temp; k++) {
                             r = n[r - 1];
                         }
                         result[r - 1] = str.charAt(i);
                     } else {
                         result[r] = str.charAt(i);
                     }
                 }
                 System.out.println(String.valueOf(result));
                 str = read.readLine();
             }
             System.out.println();
         }
     }

     public static void findLoop(int[] n, int[] loop) {
         int temp;
         int c;
         for (int i = 0; i < n.length; i++) {
             temp = 1;
             c = n[n[i] - 1];
             while (n[i] != c) {
                 c = n[c - 1];
                 temp++;
             }
             loop[i] = temp;
         }
     }
}

							
Posted in poj | Leave a comment

Poj Solution 1025

http://poj.org/problem?id=1025

#include <iostream>
class Room {
public:
int fNo , rNo; // rNo表示房间号,fNo表楼层号,其中fNo-2表楼外。rNo=-1表示电梯
char * toString()  
{    // 此函数线程不安全,不过用在此题没问题。千万不能在printf()这类函数内两次调用此函数。
   static char str_room[5];
   sprintf(str_room,"%02d%02d",fNo,rNo);
   return str_room;
}
Room& operator = ( const Room& room ) { fNo=room.fNo; rNo=room.rNo; return *this; }
};
class Time {
public:
char tstr[9];   // Time类自带了用于转换的 char[]。
int h , m , s ;
Time() { h=m=s=0; }
Time(int hh,int mm,int ss) { h=hh; m=mm; s=ss; }
void setTime(char * str)
{
   h = (str[0]-'0')*10 + (str[1]-'0') ;
   m = (str[3]-'0')*10 + (str[4]-'0') ;
   s = (str[6]-'0')*10 + (str[7]-'0') ;
}
char* toString() { sprintf(tstr,"%02d:%02d:%02d",h,m,s); return tstr; }
bool operator == (const Time& t){ return ( h==t.h && m==t.m && s==t.s ); }
Time& operator = (const Time& t){ h=t.h; m=t.m; s=t.s; return *this; }
bool operator < (const Time& t)
{ 
   if(h!=t.h) return h<t.h;
   if(m!=t.m) return m<t.m;
   if(s!=t.s) return s<t.s;
   return false;
}
Time& add(int n)
{
   int temp;
   if( n>=3600 )
   { temp = n/3600; h += temp; n -= temp*3600; }
   if( n>=60 )
   { 
    temp = n/60 ; m += temp; n -= temp*60 ;
    if(m>=60) { m-=60; ++h; }
   }
   if( n<60 ) s += n ;
   if( s>=60) { s-=60; ++m; }
   return *this;
}
};
class Agent {
public:
char code;
Time startWait , nextTime ;
Room roomList[100];   // 最多访问 100 个房间
int lastTime[100] , totalRoom;
char actionList[100][60];   // 假设最多有100个action
int actNum , nextRoom , waitFlag;
Room pos ;
Agent* next;
Agent(char c,char* str)
{ 
   code=c; 
   waitFlag = actNum = nextRoom = 0; 
   next=NULL; 
   nextTime.setTime(str); 
   pos.fNo=-2; 
}
void doAct();     // 此函负责执行一个步骤
void moveTo(Room room);   // 负责Agent的移动和访问room。包括乘电梯操作。
};
class AgentList {
public:
Agent* head ;
AgentList() { head=NULL; }
void add(Agent* ag)
{
   Agent *par , *cur;
   par = cur = head;
   while( cur!=NULL && cur->code < ag->code )
   { par=cur; cur=cur->next; }
   if(cur==head){ head=ag; ag->next=cur; return; }
   par->next = ag;
   ag->next = cur ;
}
void del(Agent *ag) // 只是取下节点,并未删除,这里会内存泄露!不过不想管了。
{
   if(ag==head) { head=head->next; ag->next=NULL; return; } 
   Agent *par , *cur;
   par = cur = head;
   while( cur!=NULL && cur!=ag ) { par=cur; cur=cur->next; }
   par->next = ag->next ;
   ag->next = NULL;
}
};
/* ------------------------- 数据结构定义完毕 ----------------------------------- */

/***************************************************************************************/

Time roomTable[10][10] , elevator[10] , nowTime;
AgentList aglist , result ;

void Agent::moveTo( Room room )   // 此函数用于单步执行,包括访问房间和电梯两大块
{
//------------------------------ 以下是进电梯相关操作 ----------------------
if( pos.rNo!=-1 && room.rNo==-1 ) 
{
   waitFlag = 0;
   nextTime.add(10) ;
   sprintf(actionList[actNum++],
    "%s %s Transfer from room %s to elevatorn",
    nowTime.toString(),nextTime.toString(),pos.toString() );
   pos.rNo = -1; // 进入电梯或电梯等待队列
   return ;
}
if( room.rNo==-1 ) // 如果需要进电梯
{
   if( nowTime.s%5!=0 ) {   // 时间不是 5s 倍数,则等待。
    startWait = nowTime; 
    waitFlag=1;   // 等待标识 waitFlag=1
    nextTime.add( 5 - nextTime.s%5 );
    sprintf(actionList[actNum++],
     "%s %s Waiting in elevator queuen", startWait.toString(), nextTime.toString() );
    pos.rNo = -1;
    return;
   } 
   else 
   {
    if( nowTime < elevator[pos.fNo] )   // 如果优先级不高(电梯已有人),等待。
    {
     if( waitFlag==0 ){ startWait = nowTime; waitFlag=1; }
     else --actNum;
     nextTime.add( 5 );
     sprintf(actionList[actNum++],
      "%s %s Waiting in elevator queuen",startWait.toString(), nextTime.toString() );
     pos.rNo = -1;
     return;
    } 
    else 
    {    // 乘电梯中
     int et = (room.fNo-pos.fNo)*30 ;
     et = ( et<0?(-et):et ) ;
     nextTime.add( et );
     sprintf(actionList[actNum++], "%s %s Stay in elevatorn", nowTime.toString() , nextTime.toString() );
     elevator[pos.fNo] = nowTime.add( 5 ); // 电梯时间加 5
     pos.fNo = room.fNo;   // 楼层更新
     waitFlag = 0 ;
     return;
    }
   }
} 
//----------------------- 以下是 出电梯 ---------------
if( pos.rNo==-1 && room.rNo!=-1 )
{
   waitFlag = 0;
   nextTime.add(10);
   sprintf(actionList[actNum++],
    "%s %s Transfer from elevator to room %sn",
    nowTime.toString() , nextTime.toString() , room.toString());
   pos.rNo = room.rNo ;
   return ;
}
//----------------------- 以下是 同层房间访问 相关操作 -----------------------------
if( pos.rNo!=room.rNo )  
{       // 不同房间,则花 10s 换房间
   waitFlag = 0;
   nextTime.add(10);
   char temp[5];
   strcpy(temp,room.toString());
   sprintf(actionList[actNum++],
    "%s %s Transfer from room %s to room %sn",
    nowTime.toString(), nextTime.toString(), pos.toString(), temp );
   pos.rNo = room.rNo ;
   return ;
}
if( nowTime < roomTable[room.fNo][room.rNo] )   // 进入等待队列
{
   nextTime = roomTable[room.fNo][room.rNo] ;
   if( waitFlag==0 ) { startWait=nowTime; waitFlag=1; }
   else --actNum;
   sprintf(actionList[actNum++],
    "%s %s Waiting in front of room %sn",
    startWait.toString(), nextTime.toString(), room.toString() );
   pos.rNo = room.rNo;
   return ;
}
waitFlag = 0; // 等待标识 清除
nextTime.add( lastTime[nextRoom++] ); // 进入房间,
roomTable[room.fNo][room.rNo] = nextTime;   // 更新房间信息
sprintf(actionList[actNum++],
   "%s %s Stay in room %sn",
   nowTime.toString(),nextTime.toString(),room.toString() );
pos.rNo = room.rNo;
return;
//-------------------------------------------------------
}
void Agent::doAct()   // 执行函数,只执行一步(下面分为 进楼,访问,出楼 三种情况考虑 )
{
Room roomTo;
//----------------- 以下处理出楼 -----------------------
if( nextRoom==totalRoom )
{
   if( pos.fNo==1 )
   {
    nextTime.add(30);
    sprintf(actionList[actNum++],"%s %s Exitn",nowTime.toString() , nextTime.toString() );
    aglist.del( this );   // 从待处理链表 删除此 Agent
    result.add( this );   // 加入结果集。
    return ;
   }
   if( pos.rNo==-1 )
   {
    roomTo.fNo=1; roomTo.rNo=-1;
    moveTo( roomTo );
    return;
   }
   roomTo.fNo = pos.fNo;
   roomTo.rNo = -1;
   moveTo( roomTo );
   return;
}
//----------------- 以下处理进楼 -----------------------
roomTo = roomList[nextRoom];
if( pos.fNo==-2 ) // 进楼
{ 
   nextTime.add(30);
   sprintf(actionList[actNum++],"%s %s Entryn",nowTime.toString() , nextTime.toString() );
   pos.fNo = 1; pos.rNo=-2; // rNo=-2 表示刚进楼,在门口。
   return ;
}
if( pos.rNo==-2 ) // 刚进楼,准备访问。
{
   if( roomTo.fNo==1 )   // 访问目标在一楼
   { 
    pos.rNo = roomTo.rNo ;
    moveTo( roomList[nextRoom] );
   } else {
    pos.rNo = -1 ;
    roomTo.rNo = -1;
    moveTo( roomTo ); // 不在一楼,准备进入电梯
   }
   return ;
}
//---------------- 以下处理 楼内访问 ----------------------
if( pos.fNo!=roomTo.fNo )
{
   roomTo.rNo=-1;
   moveTo( roomTo );
}
else
   moveTo( roomTo );
}

void Execute()   // 总调函数,每次选一个触发时间且编号靠前的Agent 执行一步。
{
Agent *p,*cur ;
while( aglist.head!=NULL )
{
   nowTime.setTime("24:60:60"); // nowTime 设为最大值
   p = cur = aglist.head;
   while(cur!=NULL)
   {
    if( cur->nextTime < nowTime ) { nowTime=cur->nextTime; p=cur; }
    cur = cur->next ;
   }
   p->doAct();   // 被选上的 Agent 执行一步。
}
}
int main()
{
char cd , str_t[10];
while( scanf("%c ",&cd) && cd!='.' )
{
   scanf("%s",str_t);
   Agent *ag = new Agent(cd,str_t);
   aglist.add(ag);
   int roomNo , lastTime , i=0;
   while( scanf("%dn",&roomNo) && roomNo )
   {
    scanf("%d",&lastTime);
    ag->roomList[i].fNo = roomNo/100;
    ag->roomList[i].rNo = roomNo%100;
    ag->lastTime[i] = lastTime;
    ++i;
   }
   ag->totalRoom = i;   // 每个 Agent 所需访问的房间总数
}
Execute(); // 执行调度
Agent *ag = result.head;
while( ag!=NULL )
{
   printf("%cn",ag->code);
   for( int i=0; i<ag->actNum ; ++i )
    printf("%s",ag->actionList[i]);
   printf("n");
   ag = ag->next;
}
return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 1024

http://poj.org/problem?id=1024

#include <stdio.h>
#include <string.h>
struct pos
{
int len[2];
int used;
int r;
int u;
}p[20][20];
int num, wallNum, w, h, Dx, Dy, minPath;
void DFS(int x, int y, int len, int flag)
{
if (len >= p[x][y].len[flag] && p[x][y].len[flag]!=0)
   return;
if (x+y!=0 && (x!=Dx||y!=Dy))
   p[x][y].len[flag] = len;
len++;
if (p[x][y].r==0 && x+1<w)
   DFS(x+1, y, len, flag);
if (p[x][y].u==0 && y+1<h)
   DFS(x, y+1, len, flag);
if (x-1>=0 && p[x-1][y].r==0)
   DFS(x-1, y, len, flag);
if (y-1>=0 && p[x][y-1].u==0)
   DFS(x, y-1, len, flag);
} 
int judge()
{
int i, j;
for (i=0; i<w; i++)
   for (j=0; j<h; j++)
   {
    if (p[i][j].used==0 && p[i][j].len[0]+p[i][j].len[1]<=minPath)
     return 0;
    if (p[i][j].r==1 && i+1<w && p[i][j].len[0]+p[i+1][j].len[1]+1>minPath && p[i][j].len[1]+p[i+1][j].len[0]+1>minPath)
     return 0;
    if (p[i][j].u==1 && j+1<h && p[i][j].len[0]+p[i][j+1].len[1]+1>minPath && p[i][j].len[1]+p[i][j+1].len[0]+1>minPath)
     return 0; 
   }
return 1;
}
int main()
{
int x, y, x1, y1, x2, y2;
char c;
scanf("%d", &num);
while (num--)
{ 
   memset(p, 0, sizeof(p));
   scanf("%d %dn", &w, &h);
   p[0][0].used = 1;
   Dx = Dy = minPath = 0;
   while ((c=getchar())!='n' && c!=EOF)
   {
    if (c == 'U')
     p[Dx][++Dy].used = 1;
    else if (c == 'D')
     p[Dx][--Dy].used = 1;
    else if (c == 'L')
     p[--Dx][Dy].used = 1;
    else if (c == 'R')
     p[++Dx][Dy].used = 1;
    minPath++;
   }
   scanf("%d", &wallNum);
   while (wallNum--)
   {
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    x = x1 - x2;
    y = y1 - y2;
    if (x==0 && y==1)
     p[x2][y2].u = 1;
    else if (x==0 && y==-1)
     p[x1][y1].u = 1;
    else if (x==1 && y==0)
     p[x2][y2].r = 1;
    else if (x==-1 && y==0)
     p[x1][y1].r = 1;
   }
   DFS(0, 0, 0, 0);
   DFS(Dx, Dy, 0, 1); 
   if(judge())
    printf("CORRECTn");
   else
    printf("INCORRECTn");
}
return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 1023

http://poj.org/problem?id=1023

/* @author: */
import java.util.Scanner;
public class Main{
  public static void main(String args[]){
   long  n;
   int k,t;
   int ans[]=new int[65];
   char a[]=new char[66];
   Scanner sc=new Scanner(System.in);
   t=sc.nextInt();
   while((t--)!=0){
     k=sc.nextInt();
     a=sc.next().toCharArray();
    n=sc.nextLong();
   
    for(int i=0;i< k;i++){
     if((n&0x1)!=0){
      ans[k-1-i]=1;
      if(a[k-i-1]=='p')
       n=n-1;
      else n=n+1;
     }else
      ans[k-1-i]=0;
      n/=2;
     }
     if(n==0){
      for(int i=0;i< k;i++)
        System.out.printf("%d",ans[i]);
      System.out.printf("n");
    }else System.out.printf("Impossiblen");
   }
  }
 }
							
Posted in poj | Leave a comment

Poj Solution 1022

http://poj.org/problem?id=1022

#include<iostream>
#include"math.h"
using namespace std;
int id[100];int edge[100][100],set[100];
int next[100][8],n,ok;

int find(int d)
{int i;
for(i=0;i<n;i++)
if(id[i]==d)return i;
return -1;
}

int zb[4][2];
/////////////////////////
void search(int i,int axes[4])
{int ax[4],k;
set[i]=1;ok++;

for(k=0;k<4;k++)
{if(axes[k]<zb[k][0])zb[k][0]=axes[k];
 else if(axes[k]>zb[k][1])zb[k][1]=axes[k];
}

int j;
for(j=0;j<n;j++)
if(edge[i][j]!=0&&!set[j])
{for(k=0;k<4;k++)ax[k]=axes[k];

ax[abs(edge[i][j])-1]+=(edge[i][j]>0?1:-1);    
search(j,ax);
}

}
/////////////////
int main()
{int t,i,j,k;int key,ax[4];
cin>>t;
while(t--)
{cin>>n;

for(i=0;i<n;i++)
{cin>>id[i];
for(j=0;j<8;j++)cin>>next[i][j];
}

for(i=0;i<n;i++)
for(j=0;j<n;j++)edge[i][j]=0;
key=1;

for(i=0;i<8&&key;i+=2)
for(j=0;j<n;j++)
{if(next[j][i])
    {k=find(next[j][i]);
    
    if(k<0||id[j]!=next[k][i+1]){key=0;break;}
    else {edge[j][k]=1*(i/2+1);edge[k][j]=-1*(i/2+1);}
    }
}
for(i=0;i<4;i++)zb[i][0]=zb[i][1]=ax[i]=0;

if(key){for(i=0;i<n;i++)set[i]=0;
        ok=0;
        search(0,ax);
        if(ok<n)key=0;
        }
if(key){long v=1;for(i=0;i<4;i++)v*=(zb[i][1]-zb[i][0]+1);
        cout<<v<<endl;}
else cout<<"Inconsistent"<<endl;
}
return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 1021

http://poj.org/problem?id=1021

/* @author: */
import java.util.Scanner;
import java.util.Arrays; 
public class Main{
   static int n_limit=100*100;
   static int width;
   static int height;
    
   static boolean in_map(int x,int y){  
    if (0<=x&&x< width&&0<=y&&y< height) return true;  
    else return false;  
   }  
   
   static int calculate(int x,int y,int[][] map)  
{  
     int i=x-1,j=y-1,i2=x+1,j2=y+1,temp=1;  
     while (in_map(i,y)&&map[i][y]!=0){ i--;temp++;}  
     while (in_map(x,j)&&map[x][j]!=0){ j--;temp++;}  
     while (in_map(i2,y)&&map[i2][y]!=0){ i2++;temp++;}  
     while (in_map(x,j2)&&map[x][j2]!=0) {j2++;temp++;}  
     
     return temp;  
 }  
    
  public static void main(String args[])  
 {  
     int flag,test_n;
     int x1[]=new int[n_limit];
     int x2[]=new int[n_limit];
     int y1[]=new int[n_limit];
     int y2[]=new int[n_limit];
     int value1[]=new int[n_limit];
     int value2[]=new int[n_limit];  


     Scanner sc=new Scanner(System.in);
     test_n=sc.nextInt();
    
     while ((test_n--)!=0)  
     {   
          width=sc.nextInt();
          height=sc.nextInt();
          int n=sc.nextInt();
        
          int[][] map1=new int[width][height];
          int[][] map2=new int[width][height];
          Arrays.fill(value1,0);
          Arrays.fill(value2,0);
         // Arrays.fill(map1,0);
          //Arrays.fill(map2,0);
           for (int i=0;i< n;i++){
            x1[i]=sc.nextInt();
            y1[i]=sc.nextInt();
             map1[x1[i]][y1[i]]=i+1;
         } 
         

         for (int i=0;i< n;i++){
           x2[i]=sc.nextInt();
           y2[i]=sc.nextInt();
           map2[x2[i]][y2[i]]=i+1;
         }
  
         for (int i=0;i< n;i++)  
         {  
             value1[i]=calculate(x1[i],y1[i],map1);  
             value2[i]=calculate(x2[i],y2[i],map2);  
         }  

        Arrays.sort(value1,0,n);  
        Arrays.sort(value2,0,n);  

         flag=1;  
        for (int i=0;i< n;i++)  
         {  
            if (value1[i]!=value2[i])  
             {  
                 flag=0;  
                 break;  
             }  
        }  
         if (flag!=0) System.out.printf("YESn");  
         else System.out.printf("NOn");  
     }  
     
   }
 }
							
Posted in poj | Leave a comment

Poj Solution 1020

http://poj.org/problem?id=1020

/* @author: */
import java.util.Scanner;
import java.util.Arrays;
public class Main{
 static int c[]=new int[11];//c[i]��ű߳�Ϊi��С���εĸ���
 static int d[]=new int[41];//d[i]��ʾ��i������С���κ���������
 static int s,n,sum;
 static boolean  ok;

public static void main(String args[])
{
    int t,it,i,tp;
    Scanner sc=new Scanner(System.in);
    t=sc.nextInt();//���Դ�¦
   
    for(it=1;it<=t;it++)//ѭ������ÿһ�β���
    {
        s=sc.nextInt();//�����ӡ������εı߳�
        n=sc.nextInt();//С���εĸ���
        Arrays.fill(c,0);//ÿ�β���ǰ���
         ok=false;
        for(i=1;i<=40;i++) d[i]=1;//
        sum=0;
        for(i=1;i<=n;i++)//���������4ζ���С���εı߳�
        {
            tp=sc.nextInt();
            c[tp]++;//�߳�Ϊtp��С���εĸ������1
            sum+=tp*tp;//ͳ�����
        }
        if(sum!=s*s) ok=false;//�������С�������֮�Ͳ����ڡ����ӡ����
        else {ok=false;dfs(0);}//�����������
       
        if(ok) System.out.println("KHOOOOB!");
        else System.out.println("HUTUTU!");
    }
   }

  public static void dfs(int a)
{
    int i,j,put,p=0;
    boolean f;
    if(a==n) {//�������е�n��С����������ϡ�
       ok=true;
       return;
    }

    //Ѱ��δ��ĺ������С�ĵ�������
    for(i=1,put=41;i<=s;i++){
        if(d[i]< put) {
          put=d[i];//put:�����
          p=i;//p:�����
       }
    }

     //�Ӻ������С�����п�ʼ���Ӵ�С�����������С����
    for(i=10;i>=1;i--)
        if(c[i]>0 && put+i-1<=s && p+i-1<=s)//�����(put,p)���ܷŵ���С����c[i]
        {  
            for(j=p,f=true;j<=p+i-1;j++)
                if(d[j]>put) {f=false;break;}
            if(f)
            {
                for(j=p;j<=p+i-1;j++) d[j]+=i;//��ǵ�i������С���κ���������
                
                c[i]--;
                dfs(a+1);
                c[i]++;//��˷
                for(j=p;j<=p+i-1;j++) d[j]-=i;
            }
        }
    }
}

							
Posted in poj | Leave a comment

Poj Solution 1019

http://poj.org/problem?id=1019

#include <iostream>
#include <cmath>
 
using namespace std;
 
unsigned int a[31270], s[31270];
 
/* 打表 */
void reset()
{
    int i;
    a[1] = 1;
    s[1] = 1;
    for(i = 2; i < 31270; i++)
    {
        /* 每一组数字都比上一组长 (int)log10((double)i) + 1 */
        a[i] = a[i-1] + (int)log10((double)i) + 1;
        s[i] = s[i-1] + a[i];
    }
}
 
/* 计算 */
int work(int n)
{
    int i = 1;
    int length = 0;
 
    /* 找到 n 所在的组 */
    while (s[i] < n) i++;
 
    /* n 在该组的下标 */
    int pos = n - s[i-1];
 
    /* length: n指向的数字的最后一位的下标 */
    for (i = 1; length < pos; i++)
    {
        length += (int)log10((double)i) + 1;
    }
 
    /* 去掉所求位后面的数字然后取余 */
    /* i: n指向的数字 + 1 */
    return ((i-1) / (int)pow((double)10, length - pos)) % 10;
}
 
int main()
{
    int t;
    unsigned int n;
    reset();
    cin >> t;
    while(t--)
    {
        cin >> n;
        cout << work(n) << endl;
    }
    //system("pause");
    return 0;
}
							
Posted in poj | Leave a comment

Poj Solution 1018

http://poj.org/problem?id=1018

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;

 public class Main {

     double bp = 0;

     public Main() throws NumberFormatException, IOException {
         BufferedReader read = new BufferedReader(new InputStreamReader(
                 System.in));
         int t = Integer.parseInt(read.readLine());
         int num;
         int[][] b;
         int[][] p;
         int[] n;
         int bmin = Integer.MAX_VALUE;
         int[] bmaxs;
         int bmax;
         String[] s;
         int sum;
         int temp;
         for (int i = 0; i < t; i++) {
             num = Integer.parseInt(read.readLine());
             b = new int[num][];
             p = new int[num][];
             n = new int[num];
             bmin = Integer.MAX_VALUE;
             bmaxs = new int[num];
             bmax = Integer.MAX_VALUE;
             for (int j = 0; j < num; j++) {
                 s = read.readLine().split(" ");
                 n[j] = Integer.parseInt(s[0]);
                 b[j] = new int[n[j]];
                 p[j] = new int[n[j]];
                 for (int k = 0; k < n[j]; k++) {
                     b[j][k] = Integer.parseInt(s[k * 2 + 1]);
                     if (b[j][k] > bmaxs[j]) {
                         bmaxs[j] = b[j][k];
                     }
                     if (b[j][k] < bmin) {
                         bmin = b[j][k];
                     }
                     p[j][k] = Integer.parseInt(s[k * 2 + 2]);
                 }
             }
             for (int j = 0; j < num; j++) {
                 if (bmaxs[j] < bmax) {
                     bmax = bmaxs[j];
                 }
             }
             bp = 0;
             for (int j = bmin; j <= bmax; j++) {
                 sum = 0;
                 for (int k = 0; k < num; k++) {
                     temp = Integer.MAX_VALUE;
                     for (int h = 0; h < n[k]; h++) {
                         if (b[k][h] >= j && p[k][h] < temp) {
                             temp = p[k][h];
                         }
                     }
                     sum += temp;
                 }
                 if ((double) j / sum > bp) {
                     bp = (double) j / sum;
                 }
             }
             System.out.printf("%.3fn", bp);
         }
     }

     public static void main(String[] args) throws NumberFormatException,
             IOException {
         new Main();
     } 
}

							
Posted in poj | Leave a comment

Poj Solution 1017

http://poj.org/problem?id=1017

 import java.util.Scanner;

 public class Main {

     int a;
     int b;
     int c;
     int d;
     int e;
     int f;
     int packets;
     int t;

     public Main() {
         Scanner scan = new Scanner(System.in);
         a = scan.nextInt();
         b = scan.nextInt();
         c = scan.nextInt();
         d = scan.nextInt();
         e = scan.nextInt();
         f = scan.nextInt();
         while (a != 0 || b != 0 || c != 0 || d != 0 || e != 0 || f != 0) {
             // 6*6
             packets = f;
             // 5*5
             packets += e;
             a -= 11 * e;
             // 4*4
             packets += d;
             if (b >= 5 * d) {
                 b -= 5 * d;
             } else {
                 t = 5 * d - b;
                 b = 0;
                 a -= 4*t;
             }
             // 3*3
             packets += c / 4;
             t = c % 4;
             if (t != 0) {
                 packets += 1;
                 if (t == 1) {
                     if (b >= 5) {
                         b -= 5;
                         a -= 7;
                     } else if (b >= 0) {
                         a -= (36 - 9 - 4 * b);
                         b = 0;
                     }
                 } else if (t == 2) {
                     if (b >= 3) {
                         b -= 3;
                         a -= 6;
                     } else if (b >= 0) {
                         a -= (36 - 18 - 4 * b);
                         b = 0;
                     }
                 } else if (t == 3) {
                     if (b >= 1) {
                         b -= 1;
                         a -= 5;
                     } else if (b >= 0) {
                         a -= (36 - 27 - 4 * b);
                         b = 0;
                     }
                 }
             }
             // 2*2
             if (b > 0) {
                 packets += b / 9;
                 t = b % 9;
                 if (t != 0) {
                     packets += 1;
                     a -= 36 - 4 * t;
                 }
             }
             // 1*1
             if (a > 0) {
                 packets += a / 36;
                 t = a % 36;
                 if (t != 0) {
                     packets += 1;
                 }
             }
             System.out.println(packets);
             a = scan.nextInt();
             b = scan.nextInt();
             c = scan.nextInt();
             d = scan.nextInt();
             e = scan.nextInt();
             f = scan.nextInt();
         }
     }

     public static void main(String[] args) {
         new Main();
     }
}

							
Posted in poj | Leave a comment

Poj Solution 1015

http://poj.org/problem?id=1015

//* @author: 82638882@163.com
import java.util.Scanner;
public class Main{
 static short[] p,d;
 static short[][][] arr,path;
 static int add,a,b,st,ed;
 public static void main(String[] args)
{
 Scanner in=new Scanner(System.in);
 int cnt=0;
 while(true)
 {
  cnt++;
  a=in.nextInt();
  b=in.nextInt();
  if(a==0&&b==0)break;
   System.out.println("Jury #"+cnt);
   p=new short[a+1];
   d=new short[a+1];
   for(int i=1;i<=a;i++)
   {
    p[i]=in.nextShort();
    d[i]=in.nextShort();
    }
    dp();
    for(int k=0;k<=add;k++)
    {
    if(arr[a][b][add+k]>arr[a][b][add-k])
    {
        System.out.println("Best jury has value "+((arr[a][b][add+k]+k)/2)+
            " for prosecution and value "+((arr[a][b][add+k]-k)/2)+" for defence:");
        print(a,b,k);
        break;
    }
    else if(arr[a][b][add-k]!=-1)
    {
        System.out.println("Best jury has value "+((arr[a][b][add-k]-k)/2)+
            " for prosecution and value "+((arr[a][b][add-k]+k)/2)+" for defence:");
        print(a,b,-k);
        break;
    }
    }
    System.out.println();
    System.out.println();
   }
  }

 static void dp()
 {
  arr=new short[a+1][b+1][40*b+30];
  path=new short[a+1][b+1][40*b+30];
  add=20*b;
  st=-b*20;ed=-st;
  for(int i=0;i<=a;i++)
    for(int j=0;j<=b;j++)
        for(int k=st;k<=ed;k++)
            arr[i][j][k+add]=-1;
   arr[0][0][add]=0;
  for(int i=1;i<=a;i++)
    for(int j=0;j<=b;j++)
        for(int k=st;k<=ed;k++)
        {
          arr[i][j][k+add] = arr[i-1][j][k+add];
          path[i][j][k+add]=1;
          if(j>0&&k-p[i]+d[i]>=-add&&k-p[i]+d[i]<=add&&arr[i-1][j-1][k-p[i]+d[i]+add]!=-1
            &&arr[i-1][j-1][k-p[i]+d[i]+add]+p[i]+d[i]>arr[i][j][k+add])
          {
            arr[i][j][k+add]=(short) (arr[i-1][j-1][k-p[i]+d[i]+add]+p[i]+d[i]);
            path[i][j][k+add]=2;
          }
        }
  }

    static void print(int i,int j,int k)
    {
        if(i == 0 && j == 0 && k == 0) return ;
        if(path[i][j][k+add] == 1)
           print(i-1, j, k);
        else{
           print(i-1, j-1, k-p[i]+d[i]);
           System.out.print(" "+i);
        }
    }
}
							
Posted in poj | Leave a comment

Poj Solution 1014

http://poj.org/problem?id=1014

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class Main { 

    public static void main(String[] args) throws IOException { 
        BufferedReader read = new BufferedReader(new InputStreamReader( 
                System.in)); 
        String[] s; 
        int[] marbles; 
        int sum; 
        int part; 
        int times = 0; 
        while (true) { 
            marbles = new int[6]; 
            sum = 0; 
            s = read.readLine().split(" "); 
            for (int i = 0; i < 6; i++) { 
                marbles[i] = Integer.parseInt(s[i]); 
            } 
            if (marbles[5] > 5) { 
                marbles[5] = 4 + marbles[5] % 2; 
            } 
            if (marbles[4] > 6) { 
                marbles[4] = 6 - marbles[4] % 2; 
            } 
            if (marbles[3] > 5) { 
                marbles[3] = 4 + marbles[3] % 2; 
            } 
            if (marbles[2] > 5) { 
                marbles[2] = 4 + marbles[2] % 2; 
            } 
            if (marbles[1] > 4) { 
                marbles[1] = 4 - marbles[1] % 2; 
            } 
            for (int i = 0; i < 6; i++) { 
                sum += marbles[i] * (i + 1); 
            } 
            if (sum == 0) { 
                break; 
            } 
            times++; 
            System.out.printf("Collection #%d:n", times); 
            if (sum % 2 != 0) { 
                System.out.println("Can't be divided."); 
                System.out.println(); 
                continue; 
            } 
            part = sum / 2; 
            if (search(part, marbles)) { 
                System.out.println("Can be divided."); 
            } else { 
                System.out.println("Can't be divided."); 
            } 
            System.out.println(); 
        } 
    } 

    public static boolean search(int part, int[] marbles) { 
        int[] flg = new int[part + 1]; 
        flg[0] = 1; 
        for (int i = 0; i < 6; i++) { 
            for (int j = 1; j <= marbles[i]; j++) { 
                int base = j * (i + 1); 
                if (base > part) { 
                    break; 
                } 
                for (int k = part - (i + 1); k >= base - i - 1; k--) { 
                    if (flg[k] != 0) { 
                        flg[k + i + 1] = 1; 
                    } 
                    if (flg[part] != 0) { 
                        return true; 
                    } 
                } 
            } 
        } 
        return flg[part] != 0; 
    } 

} 
							
Posted in poj | Leave a comment

Poj Solution 1012

http://poj.org/problem?id=1012

#include<iostream>
using namespace std;

int a;
int table[] = {0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881,13482720};
int main(){
while(cin>>a && a){
   cout<<table[a]<<endl;
}
return 0;
}


							
Posted in poj | Leave a comment

Poj Solution 1011

http://poj.org/problem?id=1011

 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.util.Arrays;
 import java.util.StringTokenizer;

 public class Main {

     static boolean[] used;
     static int len;
     static int[] s;
     static int sum;
     static int max;
     static int parts;

     public static void main(String[] args) throws Exception {
         BufferedReader read = new BufferedReader(new InputStreamReader(
                 System.in));
         while ((len = Integer.parseInt(read.readLine())) != 0) {
             s = new int[len];
             StringTokenizer take = new StringTokenizer(read.readLine());
             int index = 0;
             sum = 0;
             used = new boolean[len];
             while (take.hasMoreTokens()) {
                 s[index] = Integer.parseInt(take.nextToken());
                 sum += s[index++];
             }
             Arrays.sort(s);
             max = s[len - 1];
             for (; max <= sum; max++) {
                 if (sum % max == 0) {
                     parts = sum / max;
                     if (search(0, len - 1, 0)) {
                         System.out.println(max);
                         break;
                     }
                 }
             }
         }
     }

     public static boolean search(int res, int next, int cpl) {
         if (res == max) {
             res = 0;
             next = len - 2;
             cpl++;
         }
         if (cpl == parts) {
             return true;
         }
         while (next >= 0) {
             if (used[next] == false) {
                 if (res + s[next] <= max) {
                     used[next] = true;
                     if (search(res + s[next], next - 1, cpl)) {
                         return true;
                     }
                     used[next] = false;
                     if (res == 0) {
                         break;
                     }
                     if (res + s[next] == max) {
                         break;
                     }
                 }
                 int i = next - 1;
                 while (i >= 0 && s[i] == s[next]) {
                     i--;
                 }
                 next = i;
                 int l_s = 0;
                 for (int j = next; j >= 0; j--) {
                     if (!used[j]) {
                         l_s += s[j];
                     }
                 }
                 if (l_s < max - res) {
                     break;
                 }
                 continue;
             }
             next--;
         }
         return false;
     } 
}

							
Posted in poj | Leave a comment

Poj Solution 1010

http://poj.org/problem?id=1010

//* @author: 82638882@163.com
import java.util.*;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
   while(in.hasNext())
   {
    int[] arr=new int[100];
    int n=0;
    while(true)
    {
        int a=in.nextInt();
        if(a==0)break;
        arr[n++]=a;
    }
    Arrays.sort(arr,0,n);
    while(true)
    {
     int a=in.nextInt();
     if(a==0)break;
     int ser=0,num=0,max=0,a1=0,a2=0,a3=0,a4=0;            
     boolean tie=false;
     for(int i=0;i< n;i++)
     {
      if(arr[i]>a)break;
      if(arr[i]==a)
      {
        if(ser==0)
        {
            ser=1;num=1;max=i;a1=i;tie=false;
        }
        else if(ser==1&&num>1)
        {
            ser=1;num=1;max=i;a1=i;tie=false;
        }
        }
        for(int j=i;j< n;j++)
        {
        if(arr[i]+arr[j]>a)break;
        if(arr[i]+arr[j]==a)
        {
            int ser1=1;
            if(i!=j)ser1++;
            if(ser1>ser)
            {
                ser=ser1;max=j;num=2;a1=i;a2=j;tie=false;
            }
            else if(ser1==ser)
            {
                if(num>2||(num==2&&arr[max]< arr[j]))
                {
                    ser=ser1;max=j;num=2;a1=i;a2=j;tie=false;
                }
                else if(num==2&&arr[max]==arr[j])
                    tie=true;
            }
        }
        for(int k=j;k< n;k++)
        {
            if(arr[i]+arr[j]+arr[k]>a)break;
            if(arr[i]+arr[j]+arr[k]==a)
            {
                int ser1=1;
                if(i!=j)ser1++;if(j!=k)ser1++;
                if(ser1>ser)
                {
                    ser=ser1;max=k;num=3;a1=i;a2=j;a3=k;tie=false;
                }
                else if(ser1==ser)
                {
                    if(num>3||(num==3&&arr[max]< arr[k]))
                    {
                        ser=ser1;max=k;num=3;a1=i;a2=j;a3=k;tie=false;
                    }
                    else if(num==3&&arr[max]==arr[k])tie=true;
                }
            }
            for(int w=k;w< n;w++)
            {
                if(arr[i]+arr[j]+arr[k]+arr[w]>a)break;
                if(arr[i]+arr[j]+arr[k]+arr[w]==a)
                {
                    int ser1=1;
                    if(i!=j)ser1++;if(j!=k)ser1++;if(k!=w)ser1++;
                    if(ser1>ser)
                    {
                        ser=ser1;max=w;num=4;
                        a1=i;a2=j;a3=k;a4=w;
                        tie=false;
                    }
                    else if(ser1==ser&&num==4)
                    {
                      if(arr[w]>arr[max])
                      {
                        max=w;
                        a1=i;a2=j;a3=k;a4=w;
                        tie=false;
                      }
                      else if(arr[w]==arr[max])
                        tie=true;
                    }
                }    
            }
        }
      }
    }
    if(num==0)System.out.println(a+" ---- none");
    else if(tie)System.out.println(a+" ("+ser+"):"+" tie");
    else {
        System.out.print(a+" ("+ser+"):");
        if(num==1)System.out.println(" "+arr[a1]);
        else if(num==2)System.out.println(" "+arr[a1]+" "+arr[a2]);
        else if(num==3)System.out.println(" "+arr[a1]+" "+arr[a2]+" "+arr[a3]);
        else if(num==4)System.out.println(" "+arr[a1]+" "+arr[a2]+" "+arr[a3]+" "+arr[a4]);
    }
    }
  }    
 }
}
							
Posted in poj | Leave a comment

Poj Solution 1009

http://poj.org/problem?id=1009

//* @author popop0p0popo
import java.util.*;
import java.io.*;

public class Main{
  public static void main(String[] args){
   Scanner scanner=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
   int n,dtf,m,mn,cp,cpl;
   int[][] dt;
   while (true){
    n=scanner.nextInt();
    if (n==0){
    break;
    }
    System.out.println(n);
    dt=new int[2][10000];
    dtf=0;
    while (true){
        dt[0][dtf]=scanner.nextInt();
        dt[1][dtf]=scanner.nextInt();
        if (dt[0][dtf]==0&&dt[1][dtf]==0){
            break;
        }
        dtf++;
    }
    mn=0;
    for (int i=0;i< 10000 ;i++ ){
        if (dt[1][i]==0){
            break;
        }
        mn=mn+dt[1][i];
    }
    m=mn/n;
    cp=getM(dt,0,0,m,n);
    cpl=0;
    int flag=0;
    for (int i=0;i< m ;i++ ){
      if (dt[1][getV(dt,i,0,n)]-flag>4*n&&flag>=n){
      int t=dt[1][getV(dt,i,0,n)]-flag-(dt[1][getV(dt,i,0,n)]-flag)%n-n;
        if (cp==0){
             cpl=cpl+t;
        }
        else{
         System.out.println(cp+" "+cpl);
         cp=0;
         cpl=t;
        }
        flag=flag+t;
        i=i+t/n;
        }
         for (int j=0;j< n ;j++ ){
        if (getM(dt,i,j,m,n)==cp){
         cpl++;
           }
        else{
         System.out.println(cp+" "+cpl);
         cp=getM(dt,i,j,m,n);
         cpl=1;
        }
        flag++;
         if (flag>=dt[1][getV(dt,i,j,n)]){
          flag=0;
        }
        if (getE(dt,i,j,n,m)>=3&&j+getE(dt,i,j,n,m)-2< n){
         if (cp==getM(dt,i,j+1,m,n)){
            cpl=cpl+getE(dt,i,j,n,m)-2;
        }
        else{
            System.out.println(cp+" "+cpl);
            cp=getM(dt,i,j+1,m,n);
            cpl=getE(dt,i,j,n,m)-2;
        }
            flag=flag+getE(dt,i,j,n,m)-2;
            j=j+getE(dt,i,j,n,m)-2;
        }
        }
        }
            System.out.println(cp+" "+cpl);
            System.out.println("0 0");
        }
        System.out.println(0);
    }

    public static int getE(int[][] dt,int ii,int jj,int n,int m){
        if (m==1){
            return getVA(dt,ii,jj,n);
        }
        else if (m==2||ii==0){
            return getMin(getVA(dt,0,jj,n),getVA(dt,1,jj,n));
        }
        else if (ii==m-1){
            return getMin(getVA(dt,m-1,jj,n),getVA(dt,m-2,jj,n));
        }
        else{
return getMin(getMin(getVA(dt,ii,jj,n),getVA(dt,ii+1,jj,n)),getMin(getVA(dt,ii,jj,n),getVA(dt,ii-1,jj,n)));
        }
    }

    public static int getMin(int a,int b){
        if (a< b){
            return a;
        }
        return b;
    }

public static int getM(int[][] dt,int ii,int jj,int m,int n){
  int max=0;
  for (int i=-1;i< 2 ;i++ ){
   for (int j=-1;j< 2;j++ ){
    if (i==0&&j==0){
      continue;
    }
    else if (jj==0&&j==-1){
      continue;
    }
    else if (jj==(n-1)&&j==1){
      continue;
    }
    else if (ii==0&&i==-1){
      continue;
    }
    else if (ii==(m-1)&&i==1){
      continue;
    }
    else{
      if (Math.abs(dt[0][getV(dt,ii,jj,n)]-dt[0][getV(dt,ii+i,jj+j,n)])>max){
    max=Math.abs(dt[0][getV(dt,ii,jj,n)]-dt[0][getV(dt,ii+i,jj+j,n)]);
      }
    }
  }
 }
    return max;
}

   public static int getV(int[][] dt,int ii,int jj,int n){
    int t=ii*n+jj+1;
    int total=0;
    int flag=0;
    while (t>total){
      total=total+dt[1][flag];
      flag++;
    }
    return flag-1;
  }

  public static int getVA(int[][] dt,int ii,int jj,int n){
        int t=ii*n+jj+1;
        int total=0;
        int flag=0;
        while (t>total){
            total=total+dt[1][flag];
            flag++;
        }
        return total-t;
    }
}


							
Posted in poj | Leave a comment

Poj Solution 1008

http://poj.org/problem?id=1008

 import java.util.Scanner;

 public class Main {

     public static void main(String[] args) {
         Scanner scan = new Scanner(System.in);
         int times = scan.nextInt();
         System.out.println(times);
         int day;
         int day2;
         int t_day = 0;
         int t_month;
         String month;
         String month2;
         int year;
         int year2;
         int day_num;
         int day_num2;
         for (int i = 0; i < times; i++) {
             String str_day = scan.next();
             day = Integer.parseInt(str_day.substring(0, str_day.length() - 1));
             month = scan.next();
             year = scan.nextInt();
             t_month = getHaabMonth(month);
             if(t_month<=18){
                 t_day = (t_month-1) * 20;
             }else{
                 t_day = 360;
             }
             day_num = 365 * year  +  t_day + (day + 1);
             year2 = day_num / 260;
             day_num2 = day_num % 260;
             if(day_num2 == 0){
                 year2--;
             }
             day2 = day_num2 % 13;
             if (day2 == 0) {
                 day2 = 13;
             }
             month2 = getTzolkinMonth(day_num2 % 20);
             System.out.println(day2 + " " + month2 + " " + year2);
         }
     }

     public static int getHaabMonth(String month) {
         if (month.equals("pop")) {
             return 1;
         } else if (month.equals("no")) {
             return 2;
         } else if (month.equals("zip")) {
             return 3;
         } else if (month.equals("zotz")) {
             return 4;
         } else if (month.equals("tzec")) {
             return 5;
         } else if (month.equals("xul")) {
             return 6;
         } else if (month.equals("yoxkin")) {
             return 7;
         } else if (month.equals("mol")) {
             return 8;
         } else if (month.equals("chen")) {
             return 9;
         } else if (month.equals("yax")) {
             return 10;
         } else if (month.equals("zac")) {
             return 11;
         } else if (month.equals("ceh")) {
             return 12;
         } else if (month.equals("mac")) {
             return 13;
         } else if (month.equals("kankin")) {
             return 14;
         } else if (month.equals("muan")) {
             return 15;
         } else if (month.equals("pax")) {
             return 16;
         } else if (month.equals("koyab")) {
             return 17;
         } else if (month.equals("cumhu")) {
             return 18;
         } else if (month.equals("uayet")) {
             return 19;
         } else {
             return 0;
         }
     }

     public static String getTzolkinMonth(int month) {
         if (month == 1) {
             return "imix";
         } else if (month == 2) {
             return "ik";
         } else if (month == 3) {
             return "akbal";
         } else if (month == 4) {
             return "kan";
         } else if (month == 5) {
             return "chicchan";
         } else if (month == 6) {
             return "cimi";
         } else if (month == 7) {
             return "manik";
         } else if (month == 8) {
             return "lamat";
         } else if (month == 9) {
             return "muluk";
         } else if (month == 10) {
             return "ok";
         } else if (month == 11) {
             return "chuen";
         } else if (month == 12) {
             return "eb";
         } else if (month == 13) {
             return "ben";
         } else if (month == 14) {
             return "ix";
         } else if (month == 15) {
             return "mem";
         } else if (month == 16) {
             return "cib";
         } else if (month == 17) {
             return "caban";
         } else if (month == 18) {
             return "eznab";
         } else if (month == 19) {
             return "canac";
         } else if (month == 0) {
             return "ahau";
         } else {
             return "";
         }
     } 
}

							
Posted in poj | Leave a comment

Poj Solution 1007

http://poj.org/problem?id=1007

import java.util.*;   
  
class DNA   
{   
    private String str = null;   
    private int sortNum = 0;   
       
    public DNA(String input)   
    {   
        str = input;   
           
        int num = 0;   
        for(int i = 0; i < str.length()-1; i++)   
        {   
            for(int j = i+1; j < str.length(); j++)   
                if(str.charAt(i) > str.charAt(j))   
                    num++;   
        }   
        sortNum = num;   
    }   
       
    public int getSortNum()   
    {   
        return sortNum;   
    }   
       
    public String toString()   
    {   
        return str;   
    }   
}   
  
class DNAComparator implements Comparator   
{   
    public int compare(Object o1, Object o2)   
    {   
        DNA d1 = (DNA)o1;   
        DNA d2 = (DNA)o2;   
           
        if(d1.getSortNum() > d2.getSortNum())   
            return 1;   
        else if(d1.getSortNum() == d2.getSortNum())   
            return 0;   
        else  
            return -1;   
    }   
}   
  
public class Main {   
  
    public static void main(String[] args) {   
        Scanner cin = new Scanner(System.in);   
        String[] str = cin.nextLine().split(" ");   
           
        int col = Integer.valueOf(str[0]).intValue();   
        int row = Integer.valueOf(str[1]).intValue();   
        List list = new ArrayList();   
           
        for(int i = 0; i < row; i++)   
        {   
            DNA dna = new DNA(cin.nextLine());   
            list.add(dna);   
        }   
           
        Collections.sort(list, new DNAComparator());   
        print(list);   
    }   
       
    private static void print(List list)   
    {   
        Iterator iter = list.iterator();   
        while(iter.hasNext())   
        {   
            System.out.println(iter.next());   
        }   
    }   
  
}  


							
Posted in poj | Leave a comment

Poj Solution 1006

http://poj.org/problem?id=1006

import java.util.Scanner;   
  
public class Main {   
  
    public static void main(String[] args) {   
        Scanner scan = new Scanner(System.in);   
        int k = 0;   
        while (scan.hasNext()) {   
            int p = scan.nextInt();   
            int e = scan.nextInt();   
            int i = scan.nextInt();   
            int d = scan.nextInt();   
            if (p == -1 && e == -1 && i == -1 && d == -1) {   
                break;   
            }   
            k++;   
            int days = (5544 * p + 14421 * e + 1288 * i - d) % (21252);   
            if (days <= 0) {   
                days = 23 * 28 * 33 + days;   
            }   
            System.out.println("Case " + k   
                    + ": the next triple peak occurs in " + days   
                    + " days.");   
        }   
  
  
    }   
}  


							
Posted in poj | Leave a comment

Poj Solution 1005

http://poj.org/problem?id=1005



#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M_PI        3.14159265358979323846
#define DEBUG 0

int main(void) {
    if(DEBUG)
        freopen("in","r",stdin);
    int year,n,i=1;
    double x,y,radius,area;
    scanf("%d",&n);
    while(n--){
      scanf("%lf%lf",&x,&y);
      radius=x*x+y*y;
      area=M_PI*radius/2.0;
      year=(int)ceil(area/50);
      printf("Property %d: ",i++);
      printf("This property will begin eroding in year %d.n",year);
    }
    printf("END OF OUTPUT.n");
    return 0;
}

							
Posted in poj | Leave a comment

Poj Solution 1004

http://poj.org/problem?id=1004


#include <iostream>
using namespace std;
int main(){
    double d,t;
    while(cin>>d){
     t+=d;
    }
    cout<<"$"<<t/12;
    return 0;
}

							
Posted in poj | Leave a comment

Poj Solution 1003

http://poj.org/problem?id=1003

#include <stdio.h>
#include <stdlib.h>
#define DEBUG 0
int main(void) {
     if(DEBUG)
     freopen("in","r",stdin);

     float c;
     while(scanf("%f",&c)&&c!=0){
       int i=2;
       float rs=0;
       while(rs<c){
          rs+=1.0/i;
          i++;
       }
       printf("%d card(s)n",i-2);
     }

    return 0;
}

							
Posted in poj | Leave a comment

Poj Solution 1002

http://poj.org/problem?id=1002

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

typedef struct BSTNode
{
    char phoneName[9];
    int times;
    struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;

int count;

void InsertBST(BSTree &t, char name[])//二叉排序树的插入,注意里面的BSTree &t
{
    BSTree p,f;
    p = t;//指向树根
    while(p)
    {
        if(strcmp(name,p->phoneName) == 0)//树中已有name值了,无需插入
        {
            p->times++;
            count=1;
            return;
        }
        f = p;// f保存当前查找的结点
        //若name < p->phoneName ,在左子树上查找,否则在右子树查找.
        p = (strcmp(name,p->phoneName)<0) ? p->lchild : p->rchild;
    }

    p = (BSTree)malloc(sizeof(BSTNode));
    strcpy(p->phoneName,name);
    p->lchild = p->rchild = NULL;
    p->times = 1;

    if(t == NULL) t = p;
    else if(strcmp(name,f->phoneName)<0)
            f->lchild = p;
    else f->rchild = p;    
}

void InOrderTraverse(BSTree t)//中序遍历
{
    if(t!=NULL)
    {
        InOrderTraverse(t->lchild);
        if(t->times > 1)
            printf("%s %dn",t->phoneName,t->times);
        InOrderTraverse(t->rchild);
    }
}

int main()
{
    int n,i,j;
    char str[20];
    char ch;
    BSTree t = NULL;

    scanf("%d",&n);
    ch = getchar();
    count = 0;
    for(i=0;i<n;i++)
    {
        j=0;
        while( (ch=getchar())!='n')
        {
            if(isdigit(ch))//判断是否为数字
               str[j++] = ch;
            else
                if(isupper(ch))//判断是否为大写字母
                {
                    switch(ch)
                    {
                    case'A':case'B':case'C': ch = '2';break;
                    case'D':case'E':case'F': ch = '3';break;
                    case'G':case'H':case'I': ch = '4';break;
                    case'J':case'K':case'L': ch = '5';break;
                    case'M':case'N':case'O': ch = '6';break;
                    case'P':case'R':case'S': ch = '7';break;
                    case'T':case'U':case'V': ch = '8';break;
                    case'W':case'X':case'Y': ch = '9';break;
                    }
                    str[j++] = ch;
                }
                else continue;
            if(j==3) str[j++] = '-';//在第四个位置补上一横;
        }
        str[j]='';

        InsertBST(t,str);
    }

    if(count) InOrderTraverse(t);
    else printf("No duplicates.n");

    return 0;
}

							
Posted in poj | Leave a comment

Poj Solution 1001

http://poj.org/problem?id=1001

#include <iostream>
#include <string.h>
using namespace std ;

char R[10] , Num[160] ;
int n , L , Point , i , j , l , k ;
int f[160] , c[160] , A[160] ;
int x , y , z , LA , Lf , w ;

int main() {
    while ( cin >> R >> n ) {
        L = 0 ;
        Point = 0 ;
        for ( k = 0 ; k < strlen(R) ; k ++ )
            if ( R[k] != '0' ) break ;
        for ( i = k ; i < strlen(R) ; i ++ )
            if ( R[i] == '.' ) break ;
        for ( j = strlen(R)-1 ; i < j ; j -- )
            if ( R[j] != '0' ) break ;
        for ( l = k ; l <= j ; l ++ )
            if ( R[l] != '.' )
                Num[++L] = R[l] ;
            else
                Point = j - l ;
        for ( i = 1 ; i < 160 ; i ++ ) {
            f[i] = 0 ;
            c[i] = 0 ;
            A[i] = 0 ;
        }
        f[1] = 1 ;
        Lf = 1 ;
        LA = 0 ;
        for ( j = L ; j >= 1 ; j -- )
            A[++LA] = Num[j]-'0' ;
        for ( i = 1 ; i <= n ; i ++ ) {
            for ( j = 1 ; j <= Lf ; j ++ )
                c[j] = 0 ;
            for ( j = 1 ; j <= LA ; j ++ )
                for ( k = 1 ; k <= Lf ; k ++ ) {
                    x = A[j] * f[k] ;
                    y = x / 10 ;
                    z = x % 10 ;
                    w = j + k - 1 ;
                    c[w] += z ;
                    c[w+1] += (c[w]/10 + y) ;
                    c[w] %= 10 ;
                }
            Lf += LA ;
            while ( c[Lf] == 0 ) Lf -- ;
            for ( j = 1 ; j <= Lf ; j ++ )
                f[j] = c[j] ;
        }
        if ( Lf < Point * n ) {
            cout << "." ;
            for ( i = Point * n ; i > Lf ; i -- )
                cout << "0" ;
        }
        for ( i = Lf ; i >= 1 ; i -- ) {
            if ( i == Point * n ) cout << "." ;
            cout << f[i] ;
        }
        cout << endl ;
    }
    return 0 ;
}
							
Posted in poj | Leave a comment

Poj Solution 1000

http://poj.org/problem?id=1000

#include<stdio.h>
int main(){
  int a,b;
  scanf("%d%d",&a,&b);
  printf("%dn",a+b);
  return 0;
}
							
Posted in poj | Leave a comment