HDOJ-ACM1017(JAVA)

HDOJ 1017 题目一般是经典的"计算阶乘"的问题或类似题目。

不过为了准确帮你,我先假设题目是:
计算 n 的阶乘(n!)并输出结果,n 可以较大,需要用 Java 的大数处理。


HDOJ 1017(JAVA)示例代码:大数阶乘计算

java 复制代码
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            if (n < 0) break;  // 如果题目有结束条件
            System.out.println(factorial(n));
        }
        
        sc.close();
    }
    
    // 计算阶乘函数,返回字符串
    public static BigInteger factorial(int n) {
        BigInteger res = BigInteger.ONE;
        for (int i = 2; i <= n; i++) {
            res = res.multiply(BigInteger.valueOf(i));
        }
        return res;
    }
}
php
623 Bytes
© 菜鸟-创作你的创作

代码说明:

  • 使用 BigInteger 处理大数阶乘。
  • 循环读入测试数据,输出对应阶乘。
  • 结束条件根据题目设置,比如负数或 EOF。

HDOJ 1017题目原文是"阶乘问题":

给定一个整数 n(0 ≤ n ≤ 100),求 n! 的值。


【题目描述简要】

计算 n 的阶乘,n 最大到 100,结果非常大,需用大数计算。


【JAVA 完整代码实现】

java 复制代码
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            if (n < 0) break;  // 如果题目要求停止输入的条件
            System.out.println(factorial(n));
        }
        sc.close();
    }
    // 计算阶乘
    public static BigInteger factorial(int n) {
        BigInteger result = BigInteger.ONE;
        for (int i = 2; i <= n; i++) {
            result = result.multiply(BigInteger.valueOf(i));
        }
        return result;
    }
}
php
609 Bytes
© 菜鸟-创作你的创作

【运行示例】

输入:

复制代码
5
10
0
php
6 Bytes
© 菜鸟-创作你的创作

输出:

复制代码
120
3628800
1
php
13 Bytes
© 菜鸟-创作你的创作

www.52runoob.com/archives/54...

相关推荐
candyTong10 小时前
Claude Code 的 Edit 工具是怎么工作的
javascript·后端·架构
GetcharZp11 小时前
GitHub 2.4 万 Star!D2 正在重新定义程序员画图方式
后端
zhangxingchao13 小时前
多 Agent 架构到底怎么选?从 Claude Agent Teams、Cognition/Devin 到工程落地原则
前端·人工智能·后端
IT_陈寒13 小时前
SpringBoot那个自动配置的坑,害我排查到凌晨三点
前端·人工智能·后端
ServBay13 小时前
OpenCode 和它的7款必备插件
后端·github·ai编程
ping某13 小时前
逐字节拆解 tcpdump
后端
阿凡98073013 小时前
花 100 dollar,用 Claude 打通 EasyEDA&Fusion 双向同步
后端·程序员
irving同学4623813 小时前
从零搭建生产级 RAG:Embedding、Chunking、Hybrid Search 与 Reranker
前端·后端
她的男孩13 小时前
从零搭一个企业后台,为什么我把能力拆成 Starter 和 Plugin
java·后端·架构
胡志辉13 小时前
本地 AI 编码助手从 0 配起来:先选模型,再接 Ollama、VS Code、Claude Code 和 Codex
前端·后端