首页 文章

非法的表达开始

提问于
浏览
0

** 问题陈述:

您将获得参加ACM-ICPC世界总决赛的N人名单 . 他们每个人都精通某个主题,或者他们不是 . 找出2人团队可以了解的最大主题数 . 并且还了解有多少团队可以知道最大数量的主题 .

注意假设a,b和c是三个不同的人,则(a,b)和(b,c)被计为两个不同的团队 .

输入格式

第一行包含两个整数,N和M,由单个空格分隔,其中N表示人数,M表示主题数 . N行跟随 . 每行包含一个长度为M的二进制字符串 . 如果第i行的第j个字符为1,则第i个人知道第j个主题;否则,他不知道这个话题 .

约束2≤N≤5001≤M≤500

输出格式

在第一行,打印2人团队可以知道的最大主题数 . 在第二行,打印可以知道最大主题数的2人团队的数量 .

**问题:当我想将2个数字maxTopic和numTeam声明为:时,我有2个错误(“非法开始表达”和“';'预期”):

public static int maxTopic = 0; public static int numTeam = 0;

**代码:

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        int ppl = in.nextInt(); // Number of people
        int topic = in.nextInt(); // Number of topics
        
        int A[]; // Array A to store information about topics known by everyone
        public static int maxTopic = 0; // Maximum number of topics known
        public static int numTeam = 0; // Maximum number of teams that know maxTopic

        
        /* Read the information about topics 
           and save it to the array A */
        for (int i = 0; i < ppl; i++)
            A[i] = in.nextInt();
        
        /* Now call the method addCheck() to check each pair of people */
        for (int i = 0; i < ppl; i++) 
            for (int j = i + 1; j < ppl; j++) 
                Solution.addCheck(A[i], A[j], topic);
        
            
        System.out.println(maxTopic);
        System.out.println(numTeam);
    }
    
    /** 
     * Method used to add up 2 given numbers, check their sum,
     * and update the values of maxTopic and numTeam (if possible)
     * @param a First number
     * @param b Second number
     * @param digit Number of digits for a and b
     */
    
    public void addCheck(int a, int b, int digit) {
        int sum = a + b; // Calculate the sum of a and b
        int numTopic = 0; // Number of topics known for a and b
        boolean update = false; // True if the current pair has been used to update numTeam
        
        for (int i = 1; i <= digit; i++) {
            if (Solution.getNthDigit(sum, i) != 0) 
                numTopic++;
                if (numTopic > maxTopic) 
                    maxTopic = numTopic;
                if ((update == false) && (maxTopic == numTopic)) {
                    numTeam++;
                    update = true;
                }
        }
    }
          
        
   /** 
    * Get the nth digit of an integer
    * @param number The number being considered
    * @param n The digit (starting from 1, counted from right to left)
    * @return int The value of the nth digit
    * Example: getNthDigit(123, 10, 1) produces 3
    */
   public int getNthDigit(int number, int n) {    
       return (int) ((number / Math.pow(10, n - 1)) % 10);
   }  
}

1 回答

  • 1

    您不能在方法中使用公共静态修改器 . 它们仅用于类级声明 . 由于您在其他方法中使用变量,因此请在类中声明它们 . 注意,你的addCheck方法 should 被声明为static .

相关问题