You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在禁用数组、字符串、循环的情况下统计两数间可被第三数整除的数的数量

问题要求
  • 禁止使用数组、String、循环,仅允许使用if条件或switch分支
  • 从用户处获取三个整数a、b、x,统计区间[a, b](包含两端)中满足对x取模结果为0的数字个数,并输出该数量

示例:输入5 12 6,结果为2,因区间5-12中仅6和12对6取模为0。

实现代码
import java.util.Scanner;

public class Finished {
    public static void main(String[] args){

        Scanner one = new Scanner(System.in);
        int a = one.nextInt();
        int b = one.nextInt();
        int x = one.nextInt();

        int res1 = b / x;
        int res2 = b - a;
        int res3 = res2 % x;
        int finalresult = res1 + res3;

        System.out.println(finalresult);
    }
}

内容的提问来源于stack exchange,提问作者Youssef Trying

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:22:32