gpt4 book ai didi

java - Uva 的 3n+1 问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:04:48 24 4
gpt4 key购买 nike

我正在解决 Uva 的 3n+1 问题,但我不明白为什么法官拒绝我的答案。没有超过时间限制,到目前为止我尝试过的所有测试用例都正确运行。

   import java.io.*;



public class NewClass{

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {

int maxCounter= 0;
int input;

int lowerBound;
int upperBound;
int counter;
int numberOfCycles;
int maxCycles= 0;
int lowerInt;
BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));
String line = consoleInput.readLine();
String [] splitted = line.split(" ");

lowerBound = Integer.parseInt(splitted[0]);
upperBound = Integer.parseInt(splitted[1]);


int [] recentlyused = new int[1000001];



if (lowerBound > upperBound )
{
int h = upperBound;
upperBound = lowerBound;
lowerBound = h;

}
lowerInt = lowerBound;
while (lowerBound <= upperBound)
{
counter = lowerBound;
numberOfCycles = 0;


if (recentlyused[counter] == 0)
{
while ( counter != 1 )
{


if (recentlyused[counter] != 0)
{

numberOfCycles = recentlyused[counter] + numberOfCycles;
counter = 1;
}
else
{
if (counter % 2 == 0)
{
counter = counter /2;
}
else
{
counter = 3*counter + 1;
}
numberOfCycles++;
}

}
}
else
{

numberOfCycles = recentlyused[counter] + numberOfCycles;
counter = 1;
}

recentlyused[lowerBound] = numberOfCycles;



if (numberOfCycles > maxCycles)
{
maxCycles = numberOfCycles;
}

lowerBound++;
}
System.out.println(lowerInt +" "+ upperBound+ " "+ (maxCycles+1));

}


}

最佳答案

您确定接受全部输入吗?看起来您的程序在仅读取一行然后处理一行后终止。您需要能够一次接受整个样本输入。

关于java - Uva 的 3n+1 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1150952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com