首页 > 专题系列 > Java解POJ > POJ 1029 False coin [解题报告] Java
2013
11-08

POJ 1029 False coin [解题报告] Java

False coin

问题描述 :

The “Gold Bar”bank received information from reliable sources that in their last group of N coins exactly one coin is false and differs in weight from other coins (while all other coins are equal in weight). After the economic crisis they have only a simple balance available (like one in the picture). Using this balance, one is able to determine if the weight of objects in the left pan is less than, greater than, or equal to the weight of objects in the right pan.

In order to detect the false coin the bank employees numbered all coins by the integers from 1 to N, thus assigning each coin a unique integer identifier. After that they began to weight various groups of coins by placing equal numbers of coins in the left pan and in the right pan. The identifiers of coins and the results of the weightings were carefully recorded.

You are to write a program that will help the bank employees to determine the identifier of the false coin using the results of these weightings.

输入:

The first line of the input file contains two integers N and K, separated by spaces, where N is the number of coins (2<=N<=1000 ) and K is the number of weightings fulfilled (1<=K<=100). The following 2K lines describe all weightings. Two consecutive lines describe each weighting. The first of them starts with a number Pi (1<=Pi<=N/2), representing the number of coins placed in the left and in the right pans, followed by Pi identifiers of coins placed in the left pan and Pi identifiers of coins placed in the right pan. All numbers are separated by spaces. The second line contains one of the following characters: '<', '>‘, or ‘=’. It represents the result of the weighting:

‘<' means that the weight of coins in the left pan is less than the weight of coins in the right pan,

‘>’ means that the weight of coins in the left pan is greater than the weight of coins in the right pan,

‘=’ means that the weight of coins in the left pan is equal to the weight of coins in the right pan.

输出:

Write to the output file the identifier of the false coin or 0, if it cannot be found by the results of the given weightings.

样例输入:

5 3
2 1 2 3 4
<
1 1 4
=
1 2 5
=

样例输出:

3

解题代码:

//* @author 
import java.util.*;
public class Main{
  static final int  MAX=1005;
 
  public static void main(String args[]){
    Scanner cin = new Scanner(System.in);

    int n,k,p,sum,totle,num;
    /*
     *equal记录在等式中出现过的硬币;
     *t记录在不等式中出现过的硬币;
     *d保存读入的数据,即硬币的编号
     */ 
    int equal[]=new int[MAX];
    int t[]=new int[MAX];
    int d[]=new int[MAX];
    char s;
    totle=0;
    num=0;
    n=cin.nextInt();
    k=cin.nextInt();
   
    for(int i=1;i<=n;++i)
    {
        equal[i]=0;
        t[i]=0;
    }
    while((k--)!=0)
    {
       p = cin.nextInt();
       for (int j = 0; j < 2 * p; j++) {
         d[j] = cin.nextInt();
       }
       s = cin.next().charAt(0);

       
        if(s=='=')
        {
            for(int i=0;i< p*2;++i)
            {
                equal[d[i]]=1;
            }
        }
        else if(s=='>')
        {
            ++totle;
            for(int i=0;i < p;++i)
            {
                ++t[d[i]];
            }
            for(int i=p;i< 2*p;++i)
            {
                --t[d[i]];
            }
        }
        else if(s=='< ')
        {
            ++totle;
            for(int i=0;i< p;++i)
            {
                --t[d[i]];
            }
            for(int i=p;i< 2*p;++i)
            {
                ++t[d[i]];
            }
        }
    }
    /*假币在每个不等式中都应该出现*/
    sum=0;
    for(int i=1;i<=n;++i)
    {
        if(equal[i]==1)
        {
            continue;
        }
        /*找出每次都出现的假币*/
        if(t[i]==totle||t[i]==-totle)
        {
            num=i;
            ++sum;
        }
    }
    if(sum==1)
    {
        System.out.printf("%d\n",num);
    }
    else System.out.printf("0\n");
   }
}

  1. #include <cstdio>

    int main() {
    //answer must be odd
    int n, u, d;
    while(scanf("%d%d%d",&n,&u,&d)==3 && n>0) {
    if(n<=u) { puts("1"); continue; }
    n-=u; u-=d; n+=u-1; n/=u;
    n<<=1, ++n;
    printf("%dn",n);
    }
    return 0;
    }

  2. [email protected]

  3. 这道题目虽然简单,但是小编做的很到位,应该会给很多人启发吧!对于面试当中不给开辟额外空间的问题不是绝对的,实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟,今天看到小编这篇文章相见恨晚。