打印出可被其他数字整除的数字的 Java 程序 [英] Java program that prints out numbers that are divisible by other numbers

查看:53
本文介绍了打印出可被其他数字整除的数字的 Java 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以读取两个实数,然后打印出这两个实数之间的所有数字,这些数字可以被 2、3 或 5 整除.该程序运行良好,但是当用户输入两个非常大的数字时(例如例如,1122222123333 和 214123324434434)该程序需要很长时间来计算结果.我想以某种方式修复程序,以便即使对于大数字,结果也会立即打印出来.

I have a program that reads two real numbers and then it prints out all the numbers between these two, that are divisible by 2 or 3 or 5. The program works fine, but when a user enters two really large numbers (for example, 1122222123333 and 214123324434434) the program takes a very long time to calculate the result. I would like to somehow fix the program, so that even for large numbers the result would be printed out instantly.

这是我目前的代码:

import java.util.Scanner;
public class Numbers 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);        
        long x = sc.nextLong();   // first number input 
        long y = sc.nextLong();   // second number input            
        long num = 0;            // new variable num -- means all the numbers between these to given input numbers
        long count = 0;            // loop counter - how many numbers are divided by 2 or 3 or 5                        
        for (num = x; x <= num && num <= y; num++) {                                                    
            if (num % 2 == 0 | num % 3 == 0 | num % 5 == 0) {
                count = count + 1;             // increasing the counter by 1, so that every time the loop repeats, the counter increases...                                
            }               
        }
        System.out.println(count);  // prints out how many numbers are divided by 2 or 3 or 5 ...       
    }
}

推荐答案

好吧,你根本不需要循环.

Well, you don't need a loop at all.

  1. 你知道 x 和 y 之间能被 2 整除的数是 (y-x)/2(加减一).

  1. You know that the number of numbers between x and y that are divisible by 2 is (y-x)/2 (plus minus one).

同样,x 和 y 之间能被 3 整除的数是 (y-x)/3(加减一).

Similarly the number of numbers between x and y that are divisible by 3 is (y-x)/3 (plus minus one).

x 和 y 之间能被 5 整除的数是 (y-x)/5(加减一).

And the number of numbers between x and y that are divisible by 5 is (y-x)/5 (plus minus one).

您只需删除多次计算的数字.

You just have to remove the numbers you counted more than once.

如果您考虑 A、B 和C、分别能被2、3、5整除的数组(在要求的范围内),你的目标是找到:

If you consider groups A, B & C, the groups of numbers divisible by 2, 3 and 5 (in the required range) respectively, your goal is to find :

|A 联合 B 联合 C|= |A|+ |B|+ |C|- |A与B的交点|- |与C的交点|- |B 与 C 的交点|+ |A 与 B 与 C 的交点|

|A union B union C| = |A| + |B| + |C| - |A intersection with B| - |A intersection with C| - |B intersection with C| + |A intersection with B intersection with C|

因此,您必须减去可被 2*3 整除的数、可被 2*5 整除的数和可被 3*5 整除的数.最后,您必须添加可被 2*3*5 整除的数字.

Therefore, you have to subtract the numbers divisible by 2*3, the numbers divisible by 2*5 and the numbers divisible by 3*5. Finally, you have to add the numbers divisible by 2*3*5.

示例:

在 1000 和 2000 之间,大约有 (2000-1000)/2 = 500 个可被 2 整除的数字:1000,1002,1004,...,2000.实际上,计数减少了 1,因为它是 501 而不是 500,但您可以通过添加一些检查范围边缘的逻辑来对此进行调整.

between 1000 and 2000 there are about (2000-1000)/2 = 500 numbers divisible by 2 : 1000,1002,1004,...,2000. Actually, the count is off by 1, since it's 501 and not 500, but you can adjust for that by adding some logic that checks the edges of the range.

类似地,大约有 (2000-1000)/3 = 333 个可被 3 整除的数:1002、1005、1008、...、1998.

similarly, there are about (2000-1000)/3 = 333 numbers divisible by 3 : 1002, 1005, 1008, ..., 1998.

大约 (2000-1000)/5 = 200 个可被 5 整除的数:1000,1005,1010,...,2000.这里计数再次减一.

And about (2000-1000)/5 = 200 numbers divisible by 5 : 1000,1005,1010,...,2000. Here the count is again off by one.

这篇关于打印出可被其他数字整除的数字的 Java 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆