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

相关推荐
lang201509281 小时前
Spring Boot 官方文档精解:构建与依赖管理
java·spring boot·后端
why技术2 小时前
从18w到1600w播放量,我的一点思考。
java·前端·后端
间彧2 小时前
Redis Cluster vs Sentinel模式区别
后端
间彧2 小时前
🛡️ 构建高可用缓存架构:Redis集群与Caffeine多级缓存实战
后端
间彧2 小时前
构建本地缓存(如Caffeine)+ 分布式缓存(如Redis集群)的二级缓存架构
后端
程序猿DD4 小时前
Java 25 中的 6 个新特性解读
java·后端
稻草猫.4 小时前
文件 IO
java·笔记·后端·java-ee·idea
掘金码甲哥4 小时前
有关CORS跨域访问,这事没完
后端
码事漫谈5 小时前
从外行到AI指挥官:你必须掌握的五大「程序员思维」
后端
Moonbit5 小时前
MoonBit 开发者激励计划开启|赢取价值 $20 Copilot 月卡权益!
后端