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...

相关推荐
颜酱6 分钟前
# 02 | 搭骨架:用 LangGraph 编排 12 步工作流(思路)
前端·人工智能·后端
颜酱7 分钟前
02 | 搭骨架:用 LangGraph 编排 12 步工作流
前端·人工智能·后端
嘉伟桑1 小时前
Python调用电价API返回JSON示例:分时电价、现货电价和字段解析
后端
武子康1 小时前
Shippy:确定性工具、会话级 Sandbox 与 Live-Data Eval(4 类收敛 + 7 步实现方案 + 6 类评测指标)
前端·人工智能·后端
我叫黑大帅2 小时前
我为什么单一消费者的场景下,要用 Redis List 当消息队列?
redis·后端·面试
AskHarries2 小时前
文件上传系统
后端
止语Lab3 小时前
好的 DX 不等于少写代码——三种语言的摩擦力设计课
后端
吃饱了得干活3 小时前
别再手动解析 LLM 输出了!LangChain 四种结构化输出方案对比
后端·python·langchain
程序员天天困3 小时前
Arthas trace 命令怎么用?一行定位最慢那行代码
jvm·后端
Huiturn3 小时前
GPT 5.6 连续编码 10 小时,纯 Python 啃下 Word 二进制格式——doc2docx 实现拆解
后端