PAT基础编程题目-6-7 统计某类完全平方数
PAT基础编程题目-6-7 统计某类完全平方数
题目详情
解答
C语言版
#include <stdio.h>
#include <math.h>
int IsTheNumber(const int N);
int main()
{
int n1, n2, i, cnt;
scanf("%d %d", &n1, &n2);
cnt = 0;
for (i = n1; i <= n2; i++) {
if (IsTheNumber(i)) {
cnt++;
// printf("i=%d\n", i);
}
}
printf("cnt = %d\n", cnt);
return 0;
}
int IsTheNumber(const int N) {
if (N < 100 || ((int)sqrt(N) * (int)sqrt(N) - N) != 0) {
return 0;
}
int number[10];
int n=N;
int size=0;
for (int i = 0; i < 10; i++)
{
number[i] = n % 10;
n = n / 10;
size++;
for (int j = 0; j < size; j++)
if (i != j && number[i] == number[j])
return 1;
if (n == 0)
return 0;
}
return 1;
}
C++版
#include<iostream>
#include<math.h>
using namespace std;
int IsTheNumber(const int N);
int main() {
int n1, n2, cnt;
cin >> n1 >> n2;
cnt = 0;
for (int i = n1; i <= n2; i++)
{
if (IsTheNumber(i)) {
cnt++;
cout << i << endl;
}
}
cout << cnt;
return 0;
}
int IsTheNumber(const int N) {
if (N < 100 || ((int)sqrt(N) * (int)sqrt(N) - N) != 0) {
return 0;
}
int number[10];
int n = N; // N 不能修改,赋给n就可以修改
int size = 0; // 统计整数的位数
for (int i = 0; i < 10; i++)
{
number[i] = n % 10;
n = n / 10;
size++;
for (int j = 0; j < size; j++)
if (i != j && number[i] == number[j])
return 1;
if (n == 0)
return 0;
}
return 1;
}
Java版
public class Main{
private static int IsTheNumber(final int N) {
if (N < 100 || ((int)Math.sqrt(N) * (int)Math.sqrt(N) - N) != 0) {
return 0;
}
int [] number = new int[10];
int n = N; // N 不能修改,赋给n就可以修改
int size = 0; // 统计整数的位数
for (int i = 0; i < 10; i++)
{
number[i] = n % 10;
n = n / 10;
size++;
for (int j = 0; j < size; j++)
if (i != j && number[i] == number[j])
return 1;
if (n == 0)
return 0;
}
return 1;
}
public static void main(String[] args) {
int n1, n2, cnt;
cnt = 0;
Scanner scanner = new Scanner(System.in);
if(scanner.hasNext()) {
n1 = scanner.nextInt();
n2 = scanner.nextInt();
for (int i = n1; i <= n2; i++) {
if(IsTheNumber(i) == 1) {
cnt++;
System.out.println(i);
}
}
}
scanner.close();
System.out.println(cnt);
}
}
创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!