当前位置: 首页 > news >正文

Java课后习题(编程题)

第一题:


import java.util.Scanner;class Test {public static void main(String args[]) {Scanner sc=new Scanner(System.in);System.out.println("请输入一个x值:");int x=sc.nextInt();int y=0;if(x>0){y=3*x-1;}else if(x<0){y=2*x-1;}else{y=-1;}System.out.println("y的值为:"+y);
}
}

第二题:

class Test {public static void main(String args[]) {int i=1;int sum=0;while(i<=1000){if(i%3==0&&i%7==0){sum+=i;}i++;}System.out.println("sum= "+sum);
}
}

第三题:

方法思路
  1. 观察数列规律

    • 第1项:8

    • 第2项:88 = 8 × 10 + 8

    • 第3项:888 = 88 × 10 + 8

    • 第n项:前一项 × 10 + 8

  2. 使用 for 循环

    • 初始化 currentTerm = 8(第一项)和 sum = 0

    • 循环10次,每次计算当前项并累加到 sum,然后更新 currentTerm 为下一项

    • 
      class Test {public static void main(String args[]) {long sum=0;long current=8;for(int i=1;i<=10;i++){sum+=current;current=10*current+8;System.out.println("sum="+sum);}
      }
      }

第四题:


class Test {public static void main(String args[]) {double sum=0;for(double i=1;i<=10000;i++){sum+=Math.pow(-1,i+1)*(1/(i*2-1));System.out.println("sum="+sum);}
}
}

 第五题:

方法思路
  1. 问题分析

    • 计算从 100! 到 200! 的阶乘之和。

    • 直接计算大数的阶乘会超出 long 甚至 double 的范围,因此必须使用 BigInteger(Java 提供的任意精度整数类)。

  2. 算法设计

    • 使用 BigInteger 存储阶乘和累加结果。

    • 利用 迭代计算阶乘,避免重复计算(如 101! = 100! * 101

      import java.math.BigInteger;public class FactorialSum {public static void main(String[] args) {BigInteger sum = BigInteger.ZERO; // 初始化总和为0BigInteger factorial = BigInteger.ONE; // 初始化阶乘为1(用于迭代计算)// 先计算 100!(因为从100开始累加)for (int i = 1; i <= 100; i++) {factorial = factorial.multiply(BigInteger.valueOf(i));}// 从100! 累加到200!for (int i = 100; i <= 200; i++) {sum = sum.add(factorial); // 累加当前阶乘if (i < 200) {factorial = factorial.multiply(BigInteger.valueOf(i + 1)); // 计算下一个阶乘:(i+1)!}}System.out.println("100! + 101! + ... + 200! 的和 = " + sum);}
      }

      关键点说明

    • 为什么用 BigInteger

      • 100! 的值已经是 9.3326 × 10¹⁵⁷,远超 long(最大值 ~9.2 × 10¹⁸)和 double 的范围。

      • BigInteger 可以处理任意大小的整数,但计算速度较慢。

    • 优化计算

      • 迭代计算阶乘:利用 (n+1)! = n! * (n+1),避免重复计算。

      • 从 100! 开始:直接计算 100!,然后基于它计算 101!102!...,而不是对每个数单独求阶乘。

    • 内存和性能

      • 计算 200! 会生成一个非常大的数(约374位),但 BigInteger 能正确存储。


    • 验证代码正确性

    • 测试小范围:比如计算 1! + 2! + 3!,验证结果是否为 1 + 2 + 6 = 9


    • 总结

    • 适用场景:需要计算极大整数阶乘的和(如密码学、组合数学)。

    • 注意事项

      • BigInteger 运算比基本数据类型慢,但对大数必不可少。

      • 如果仅需近似值,可用 double + 对数变换,但精度会损失。

    • 输出中间值:可以在循环中打印 i! 和部分和,观察是否按预期增长。


文章转载自:
http://berserkly.hfytgp.cn
http://abolish.hfytgp.cn
http://agrology.hfytgp.cn
http://beat.hfytgp.cn
http://achiote.hfytgp.cn
http://ceramist.hfytgp.cn
http://allegiant.hfytgp.cn
http://cantharides.hfytgp.cn
http://cabbageworm.hfytgp.cn
http://argal.hfytgp.cn
http://cameralism.hfytgp.cn
http://acrotism.hfytgp.cn
http://affect.hfytgp.cn
http://aquarelle.hfytgp.cn
http://alkaloid.hfytgp.cn
http://anguished.hfytgp.cn
http://banderilla.hfytgp.cn
http://boarfish.hfytgp.cn
http://bountiful.hfytgp.cn
http://buhl.hfytgp.cn
http://angelological.hfytgp.cn
http://apheresis.hfytgp.cn
http://amphitheatrical.hfytgp.cn
http://catalysis.hfytgp.cn
http://adaption.hfytgp.cn
http://bradypepsia.hfytgp.cn
http://buoy.hfytgp.cn
http://antidromic.hfytgp.cn
http://aretine.hfytgp.cn
http://acrita.hfytgp.cn
http://www.dtcms.com/a/259464.html

相关文章:

  • 空间理解模型 SpatialLM 正式发布首份技术报告
  • Spring Web MVC ①
  • 深入剖析 Spring AOP
  • 【机器人编程基础】Python模块的定义和导入
  • Spring Boot 系统开发:打造高效、稳定、可扩展的企业级应用
  • 【AI论文】拖拽式大型语言模型:零样本提示到权重的生成
  • 机器学习基础 线性回归与 Softmax 回归
  • 【EI会议征稿】东北大学主办第三届机器视觉、图像处理与影像技术国际会议(MVIPIT 2025)
  • 惯性导航——陀螺仪
  • 移除wordpress后台“评论”菜单的三种方法
  • 云计算-Azure Functions :构建事件驱动的云原生应用报告
  • 深入理解提示词工程:原理、分类与实战应用
  • 远程控制软件哪个好用跨国安全
  • AI目前应用方向和落地的解决方案
  • 自动化测试--Appium和ADB及常用指令
  • 【android bluetooth 协议分析 10】【AVRCP详解1】【PlaybackStateCompat类如何查看】
  • C++ 多线程深度解析:掌握并行编程的艺术与实践
  • AES加密:为你的PDF文档加上一道钢铁防线
  • 【Orange Pi Zero 3】-usb摄像头项目
  • 成都芯谷金融中心·文化科技园打造文化科技高地
  • JS学习--第十章
  • 南北差异之——跨端理解能力
  • 深入理解 Spring 框架的 Bean 管理与 IOC​
  • 科学饮食助力前行:进行性核上性麻痹的饮食养护方案
  • 时光深处,爱自有答案
  • 【Unity】MiniGame编辑器小游戏(六)飞机大战【AirplaneWars】
  • Linux远程机器无法连接-------解决方案
  • 【GPU RAM】实时监控GPU内存分配(一)
  • 八股文——JAVA基础:说一下C++与java的区别
  • 工业级3D设计理念:如何平衡功能性与美学的矛盾点?