为什么 UVa 在线判断 100 (3n+1) 的方法 A 与 B 相比有缺陷?
Why is method A flawed compared to B for UVa online judge 100 (3n+1)?
只需将Swap Method A 更改为Swap Method B,UVa 判断就从'Wrong Answer' 变为'Accepted'。这是为什么?提前感谢大家的回复。
import java.io.IOException;
import java.util.StringTokenizer;
/**
* @author Coder47
* Another solution to 3n+1 (UVa ID: 100)
*/
class Main {
public static int[] cache = new int[1000000];
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
public static void main(String[] args) {
Main myWork = new Main(); // create a dynamic instance
myWork.Begin(); // the true entry point
}
void Begin() {
String input;
StringTokenizer idata;
// corresponds to swap method A
int a, b, temp, cycle, cycleMax;
// corresponds to swap method B
// int a, b, min, max, cycle, cycleMax;
while ((input = Main.ReadLn(255)) != null)
{
idata = new StringTokenizer(input);
a = Integer.parseInt(idata.nextToken());
b = Integer.parseInt(idata.nextToken());
// swap method A
if (b < a) {
temp = a;
a = b;
b = temp;
}
// swap method B
// min = Math.min(a, b);
// max = Math.max(a, b);
cycleMax = 0;
// corresponds to swap method A
for (int i = a; i <= b; i++)
// corresponds to swap method B
// for ( int i = min; i <= max; i++)
{
cycle = calculateCycle(i);
if (cycle > cycleMax) {
cycleMax = cycle;
}
}
System.out.println (a + " " + b + " " + cycleMax);
}
}
public int calculateCycle(int n) {
return calculateCycleHelper(n, 1);
}
public int calculateCycleHelper(int n, int cycleNum) {
if (n == 1) {
return cycleNum;
} else {
return calculateCycleHelper(next(n), cycleNum + 1);
}
}
public int next(int n) {
if (n % 2 == 0) {
n = n/2;
}
else {
n = 3*n+1;
}
return n;
}
}
请忽略代码的效率和其他相关问题,因为这不是这里的问题。
因为if (a < b)
说的和你想要的相反。如果 b < a
,你需要交换元素,因为在 for 循环中
for (int i = a; i <= b; i++)
你想要 a
小于b
。
编辑
对于你更新的问题,答案是因为法官正在检查输出。如果交换 a
和 b
行的输出
System.out.println (a + " " + b + " " + cycleMax);
改变,所以法官不接受
只需将Swap Method A 更改为Swap Method B,UVa 判断就从'Wrong Answer' 变为'Accepted'。这是为什么?提前感谢大家的回复。
import java.io.IOException;
import java.util.StringTokenizer;
/**
* @author Coder47
* Another solution to 3n+1 (UVa ID: 100)
*/
class Main {
public static int[] cache = new int[1000000];
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
public static void main(String[] args) {
Main myWork = new Main(); // create a dynamic instance
myWork.Begin(); // the true entry point
}
void Begin() {
String input;
StringTokenizer idata;
// corresponds to swap method A
int a, b, temp, cycle, cycleMax;
// corresponds to swap method B
// int a, b, min, max, cycle, cycleMax;
while ((input = Main.ReadLn(255)) != null)
{
idata = new StringTokenizer(input);
a = Integer.parseInt(idata.nextToken());
b = Integer.parseInt(idata.nextToken());
// swap method A
if (b < a) {
temp = a;
a = b;
b = temp;
}
// swap method B
// min = Math.min(a, b);
// max = Math.max(a, b);
cycleMax = 0;
// corresponds to swap method A
for (int i = a; i <= b; i++)
// corresponds to swap method B
// for ( int i = min; i <= max; i++)
{
cycle = calculateCycle(i);
if (cycle > cycleMax) {
cycleMax = cycle;
}
}
System.out.println (a + " " + b + " " + cycleMax);
}
}
public int calculateCycle(int n) {
return calculateCycleHelper(n, 1);
}
public int calculateCycleHelper(int n, int cycleNum) {
if (n == 1) {
return cycleNum;
} else {
return calculateCycleHelper(next(n), cycleNum + 1);
}
}
public int next(int n) {
if (n % 2 == 0) {
n = n/2;
}
else {
n = 3*n+1;
}
return n;
}
}
请忽略代码的效率和其他相关问题,因为这不是这里的问题。
因为if (a < b)
说的和你想要的相反。如果 b < a
,你需要交换元素,因为在 for 循环中
for (int i = a; i <= b; i++)
你想要 a
小于b
。
编辑
对于你更新的问题,答案是因为法官正在检查输出。如果交换 a
和 b
行的输出
System.out.println (a + " " + b + " " + cycleMax);
改变,所以法官不接受