把这段代码翻译成java语言(源语言为c++)#include <iostream> #include <cmath> using namespace std; bool isPerfectSquare(int num) { for(int i = 1; i * i <= num; i++) { if(i * i == num) { return true; } } return false; } int main() { int num = 0; for(int i = 1000; i <= 9999; i++) { int t = 0; int p = i; while(p != 0) { int x= p % 10; t = t * 10 + x; p /= 10; } if(isPerfectSquare(abs(t - i))) { num++; } } cout << num; return 0; }
时间: 2024-02-14 20:14:39 浏览: 79
以下是将C++代码翻译成Java的代码:
import java.lang.Math;
public class Main {
public static boolean isPerfectSquare(int num) {
for(int i = 1; i * i <= num; i++) {
if(i * i == num) {
return true;
}
}
return false;
}
public static void main(String[] args) {
int num = 0;
for(int i = 1000; i <= 9999; i++) {
int t = 0;
int p = i;
while(p != 0) {
int x = p % 10;
t = t * 10 + x;
p /= 10;
}
if(isPerfectSquare(Math.abs(t - i))) {
num++;
}
}
System.out.println(num);
}
}
阅读全文