您的位置:首页 > 编程语言 > Java开发

JAVA eclipse实现最大公约数、最小公倍数的逆问题求解:已知正整数a0,a1,b0,b1,设某未知正整数x满足: 1、x和 a0的最大公约数是a1; 2、x和b0的最小公倍数是b1。

2020-04-25 18:34 671 查看

输入数据保证a0能被a1整除,b1能被b0整除。
设计思路
从键盘上获取数据得到要输入几组数据,定义方法判断输入数据是否正确
定义方法求两个数的最大公约数和两个数的最小公倍数
定义方法计算是否有满足条件的数如果有输出符合条件的个数
如果没有没有符合的个数输出0
流程图:

在这里插入代码片

import java.util.Scanner;

public class GongYueShu {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("你想输入几组数据");
int n = sc.nextInt();
int arr[] = new int[1000];
int arr1[] = new int[1000];
for (int i = 0; i < n; i++) {
int a0 = 0, a1 = 0, b0 = 0, b1 = 0;
a0 = sc.nextInt();
a1 = sc.nextInt();
b0 = sc.nextInt();
b1 = sc.nextInt();
panduan(a0, a1, b0, b1);
arr = jisuana(a0, a1);
System.out.println();
arr1 = jisuanb(b0, b1);
int l = bijiao(arr, arr1);
System.out.println("共有"+l+"个");

}
}

public static void panduan(int a, int b, int c, int d) {// 判断输入数据是否正确
if (a % b != 0 || d % c != 0)
System.out.println("你输入的数据有误");

}

public static int gongyue(int x, int y) { // 求最大公约数
return x % y == 0 ? y : gongyue(y, x % y);
}

public static int gongbei(int x, int y) {// 求最小公倍数
int a = x, b = y;
int g = gongyue(a, b);
return x * y / g;
}

public static int[] jisuana(int a, int b) {
int arr[] = new int[10000];
int n = 0;
for (int i = 1; i <= 10000; i++) {
if (i < a) {
int temp;
temp = i;
i = a;
a = temp;
}
int k = gongyue(i, a);
{
int temp;
temp = i;
i = a;
a = temp;
}
{

if (k == b) {
arr
 = i;
n++;
}
}

}

// for (int l = 0; l < arr.length; l++) {
// System.out.print(arr[l] + “\t”);
// }
return arr;
}

public static int[] jisuanb(int x, int y) {
int arr[] = new int[1000];
int n = 0;
for (int i = 1; i <= 10000; i++) {
int k = gongbei(i, x);
{
if (k == y) {
arr
 = i;
n++;
}

}
}

// for (int l = 0; l < arr.length; l++) {
// System.out.print(arr[l] + “\t”);
// }
return arr;
}

public static int bijiao(int[] arr, int[] arr1) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int m = 0; m < arr1.length; m++) {
if (arr[i] == arr1[m] && arr[i] != 0) {
count++;
}

}

}

// System.out.println(“共有” + count + “个”);
return count;
}
}

  • 点赞
  • 收藏
  • 分享
  • 文章举报
李小目啊 发布了2 篇原创文章 · 获赞 0 · 访问量 396 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐