Java 加法竞争编程

Java 加法竞争编程,java,while-loop,bit-manipulation,Java,While Loop,Bit Manipulation,问题是: import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code

问题是:


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here

         Scanner sc = new Scanner(System.in);
         int testCases = 0;

         if(sc.hasNext())
         {
             //Take no.of test cases
             testCases = sc.nextInt();
         }

         //Count of While Loop
         int iterationCount = 0;


        while(testCases!=0)
        {
            //A and B Variables
            long A = Integer.parseInt(sc.next(), 2);
            long B = Integer.parseInt(sc.next(),2);

            long U,V;

            //While Loop To Get Addition
           while(B > 0)
           {
                U =Math.abs(A) ^ Math.abs(B);
                V = Math.abs(A) & Math.abs(B);
                A = U;
                B = V * 2;

                iterationCount++;

           }
        //Print the Count
        System.out.println(iterationCount);
        iterationCount = 0;
        }
        testCases--;
    }
}

最近,Chef研究了二进制数字系统,发现对非负整数执行and、XOR或位移位等位运算非常简单,而执行算术运算(如加法、乘法或除法)则要复杂得多

在玩了一段时间的二进制运算后,Chef发明了一种有趣的算法,可以将两个非负整数a和B相加:

function add(A, B):
    while B is greater than 0:
        U = A XOR B
        V = A AND B
        A = U
        B = V * 2
    return A
现在Chef想知道这个算法有多快。给定A和B的初始值(以二进制表示),他需要您帮助他计算算法while循环的重复次数

输入:


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here

         Scanner sc = new Scanner(System.in);
         int testCases = 0;

         if(sc.hasNext())
         {
             //Take no.of test cases
             testCases = sc.nextInt();
         }

         //Count of While Loop
         int iterationCount = 0;


        while(testCases!=0)
        {
            //A and B Variables
            long A = Integer.parseInt(sc.next(), 2);
            long B = Integer.parseInt(sc.next(),2);

            long U,V;

            //While Loop To Get Addition
           while(B > 0)
           {
                U =Math.abs(A) ^ Math.abs(B);
                V = Math.abs(A) & Math.abs(B);
                A = U;
                B = V * 2;

                iterationCount++;

           }
        //Print the Count
        System.out.println(iterationCount);
        iterationCount = 0;
        }
        testCases--;
    }
}

  • 输入的第一行包含一个表示测试用例数量的整数T。T测试用例的描述如下
  • 每个测试用例的第一行包含一个字符串a
  • 第二行包含一个字符串B
输出: 对于每个测试用例,打印一行,其中包含一个整数——算法将在给定数字a和B相加期间执行的迭代次数

约束条件: * 1≤T≤105 * 1≤|A |,| B|≤105 *A和B仅包含字符“0”和“1” *所有测试用例的| A |+B |之和不超过106

示例案例:A和B的初始值分别为111002=28和10102=10。第一次迭代后,它们的值分别更改为22和16。在第二次迭代之后,它们将更改为6和32,最后在第三次迭代之后,更改为38和0

我已经尝试解决这个问题,我的解决方案是:


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here

         Scanner sc = new Scanner(System.in);
         int testCases = 0;

         if(sc.hasNext())
         {
             //Take no.of test cases
             testCases = sc.nextInt();
         }

         //Count of While Loop
         int iterationCount = 0;


        while(testCases!=0)
        {
            //A and B Variables
            long A = Integer.parseInt(sc.next(), 2);
            long B = Integer.parseInt(sc.next(),2);

            long U,V;

            //While Loop To Get Addition
           while(B > 0)
           {
                U =Math.abs(A) ^ Math.abs(B);
                V = Math.abs(A) & Math.abs(B);
                A = U;
                B = V * 2;

                iterationCount++;

           }
        //Print the Count
        System.out.println(iterationCount);
        iterationCount = 0;
        }
        testCases--;
    }
}

它在VSCode IDE上运行得很好,但当我试图在CODECHEF上运行它时,我遇到了运行时错误! 运行时错误(NZEC)


我的代码有什么问题?

NZEC代表非零退出代码,在您的情况下,这是因为您的while循环没有终止,这导致程序永远不会终止,并且您不能在Codechef平台上拥有非终止程序

您应该在while循环内部而不是外部递减
testCases
。 应该是这样的:

while (testCases != 0) {
//
// Your code
//
testCases--;
}

我不知道“Codechef”是什么,但可能它需要类是
公共的
?我想这不是问题所在,因为它在VisualStudio代码中运行良好。