news 2026/6/10 18:48:19

【期末复习01】-算法题 ProgramDesign

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【期末复习01】-算法题 ProgramDesign

文章目录

  • 文章介绍
  • 项目结构
  • 1.案例Algorithm01
  • 2.案例Algorithm02
  • 3.案例Algorithm03
  • 4.案例Algorithm04
  • 5.案例Algorithm05

文章介绍

期末复习重点案例(算法题)

项目结构

1.案例Algorithm01

要求:使用冒泡排序算法对数组a={9, 7, 4, 6, 3, 1,10},按由小到大的规律排序数组中的元素。

packageProgramDesign;publicclassAlgorithm05{publicstaticvoidmain(String[]args){int[]a={9,7,4,6,3,1,10};System.out.println("排序前的数组:");printArray(a);bubbleSort(a);System.out.println("\n排序后的数组(从小到大):");printArray(a);}publicstaticvoidbubbleSort(int[]arr){intn=arr.length;for(inti=0;i<n-1;i++){booleanswapped=false;for(intj=0;j<n-1-i;j++){if(arr[j]>arr[j+1]){inttemp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;swapped=true;}}if(!swapped){break;}}}publicstaticvoidprintArray(int[]arr){for(intnum:arr){System.out.print(num+" ");}}}

运行结果

2.案例Algorithm02

要求:输出100到300间所有能被7整除且不能被3整除的整数,每行输出5个。

packageProgramDesign;publicclassAlgorithm02{publicstaticvoidmain(String[]args){intcount=0;for(intnum=100;num<=300;num++){if(num%7==0&&num%3!=0){System.out.print(num+"\t");count++;if(count==5){System.out.println();count=0;}}}}}

运行结果

3.案例Algorithm03

要求:计算3到11之间(包括3和11)所有整数的阶乘和。

publicclassAlgorithm03{publicstaticvoidmain(String[]args){longsum=0;for(inti=3;i<=11;i++){longfactorial=1;for(intj=1;j<=i;j++){factorial*=j;}sum+=factorial;System.out.println(i+"! = "+factorial);}System.out.println("\n3到11之间所有整数的阶乘和 = "+sum);}}

运行结果

4.案例Algorithm04

要求:从键盘输入一个整数n,使用while设计程序计算从1到n的和。

packageProgramDesign;importjava.util.Scanner;publicclassAlgorithm04{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.print("请输入一个整数n:");intn=scanner.nextInt();intsum=0;inti=1;if(n<=0){System.out.println("输入的数需大于0,无法计算1到n的和!");}else{while(i<=n){sum+=i;i++;}System.out.println("1到"+n+"的和为:"+sum);}scanner.close();}}

运行结果

5.案例Algorithm05

要求:从键盘输入一个整数n,使用while设计程序计算从1到n的和。

packageProgramDesign;importjava.util.Scanner;publicclassAlgorithm05{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.print("请输入一个4位整数:");intn=scanner.nextInt();if(n<1000||n>9999){System.out.println("输入的数不是4位整数!程序结束。");scanner.close();return;}inttemp=n;intsum=0;intcount=0;while(count<4){intdigit=temp%10;sum+=digit;temp=temp/10;count++;}System.out.println("4位整数"+n+"的各位数字和为:"+sum);scanner.close();}}

运行结果

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 16:24:25

运维系列【仅供参考】:windows自动更新变成了灰色,不能选择的原因

windows自动更新变成了灰色,不能选择的原因windows自动更新变成了灰色,不能选择的原因windows自动更新变成了灰色,不能选择的原因 现象: 发现我的电脑—属性—自动更新里面所有的按钮都已经是灰色的了&#xff0c; 而且每次开机都会自动运行自动更新&#xff0c;关闭进程也无…

作者头像 李华
网站建设 2026/6/10 10:28:27

科研展示革命:用AI工具5分钟生成专业学术海报的完整教程

科研展示革命&#xff1a;用AI工具5分钟生成专业学术海报的完整教程 【免费下载链接】Paper2Poster Open-source Multi-agent Poster Generation from Papers 项目地址: https://gitcode.com/gh_mirrors/pa/Paper2Poster 在当今快节奏的学术环境中&#xff0c;科研人员面…

作者头像 李华
网站建设 2026/6/9 23:52:56

量子电路调试太难?一招教你用VSCode实现图形化追踪

第一章&#xff1a;量子电路的 VSCode 可视化工具在现代量子计算开发中&#xff0c;可视化是理解与调试量子电路的关键环节。VSCode 作为主流开发环境&#xff0c;结合专用扩展可实现高效的量子电路图形化展示与交互操作。安装 Quantum Development Kit 扩展 Visual Studio Cod…

作者头像 李华
网站建设 2026/6/10 17:11:29

【期末复习01】算法题ProgramDesign

文章目录文章介绍项目结构1.案例Algorithm012.案例Algorithm023.案例Algorithm034.案例Algorithm045.案例Algorithm05文章介绍 期末复习重点案例&#xff08;算法题&#xff09; 项目结构 1.案例Algorithm01 要求&#xff1a;使用冒泡排序算法对数组a{9, 7, 4, 6, 3, 1,10}&…

作者头像 李华
网站建设 2026/6/10 13:40:30

Comsol电力变压器有限元仿真:从二维到三维的探索

comsol电力变压器有限元仿真&#xff0c;二维&#xff0c;三维&#xff0c;主要做损耗&#xff0c;短路力&#xff0c;阻抗&#xff0c;温升&#xff0c;电场。在电力系统领域&#xff0c;电力变压器作为关键设备&#xff0c;其性能的准确评估至关重要。Comsol Multiphysics 这…

作者头像 李华