我编写了一个代码,用于检查数字是否为幸福素数。快乐的数字是these types of numbers.
以下是一个幸福号码的示例:
最后我们得到“1”,这意味着它是一个幸福的素数。
发现我的错误,感谢@resueman但仍有一个问题。 它不打印第四个输入的输出“4 1000”
新代码是:
import java.io.*;
public class Contest {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
for(int m =n; m>0; m--)
{
String s = reader.readLine();
String[] sa = s.split(" ");
String number = sa[1];
int squareNumber;
int orignialNum = Integer.parseInt(number);
boolean isPrime = true;
for(int i=2; i<=orignialNum/2;i++)
{
if(orignialNum % i == 0){
isPrime = false;
}
}
if(isPrime)
{
if(orignialNum == 0 || orignialNum == 1 || orignialNum == 2 || orignialNum == 3)
{
System.out.println(sa[0]+" "+orignialNum+" "+"No");
}
else
{
squareNumber = orignialNum;
if(orignialNum<10){
squareNumber = orignialNum*orignialNum;
}
int newNumber = check(squareNumber);
boolean isHappyNumber = isHappy(newNumber);
if(isHappyNumber)
{
System.out.println(sa[0]+" "+orignialNum+" "+"Yes");
}
else
{
System.out.println(sa[0]+" "+orignialNum+" "+"No");
}
}
}
else
{
System.out.println(sa[0]+" "+orignialNum+" "+"No");
}
}
}
public static boolean isHappy(int n)
{
if(n==1)
{
return true;
}
else
{
return false;
}
}
public static int check(int n)
{
if(n>9)
{
String sm = String.valueOf(n);
String[] m = sm.split("");
int[] a = new int[m.length];
for(int i =0; i<a.length; i++)
{
a[i]= Integer.parseInt(m[i]);
}
for(int i =0; i<a.length; i++)
{
a[i]= a[i]*a[i];
}
int sum = 0;
for(int i =0; i<a.length; i++)
{
sum = sum+a[i];
}
return check(sum);
}
else
{
return n;
}
}
}
第一行将获取执行时所需的输入数量。
我提供的输入是:
4
1 1
2 7
3 383
4 1000
我得到的输出:
1 1 No
2 7 Yes
3 383 No
预期的输出是:
1 1 NO
2 7 YES
3 383 YES
4 1000 NO
不知何故,第4个输出没有出现,而“383”则没有出现 显示没有。
请帮忙。
答案 0 :(得分:0)
我稍微重构了你的代码并提出了这个问题。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
int numInputs = 0;
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
numInputs = Integer.parseInt(reader.readLine());
for(; numInputs > 0; numInputs -= 1) {
String line = reader.readLine();
String[] splitLine = line.split(" ");
int number = Integer.parseInt(splitLine[1]);
if(isPrime(number)) {
if(number == 2 || number == 3) {
System.out.format("%s %d No\n", splitLine[0], number);
} else {
if(isHappy(number)) {
System.out.format("%s %d Yes\n", splitLine[0], number);
} else {
System.out.format("%s %d No\n", splitLine[0], number);
}
}
} else {
System.out.format("%s %d No\n", splitLine[0], number);
}
}
}
private static boolean isPrime(int n) {
if (n <= 1) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0 || n % 3 == 0) {
// If multiple of 2 or 3 it's not a prime
return false;
}
// Search for divisors from 5 to only sqrt(n)
// Example: 36: (1*36) (2*18) (3*12) (6*6) (12*3) (18*2) (36*1)
for ( int i = 5; i * i <= n; i += 6 ) {
// Check only odd numbers
if ( n % i == 0 || n % ( i + 2 ) == 0 ) {
// i+4 is not checked cause it's a multiple of 3
// i is increased by 6 (multiple of 3) and the first i+4 = 9
return false;
}
}
return true;
}
private static boolean isHappy(int n) {
do {
int sum = 0;
// While number n has two or more digits
while( n / 10 != 0 ) {
int remainder = n % 10; // last digit
n = n / 10; // n without last digit
sum += remainder * remainder;
}
sum += n * n; // add square of the last digit
n = sum;
} while(n != 1 && n != 4); // n == 1 => :) n == 4 => :(
return n == 1;
}
}