【算法题分享】小白必备的算法基础题
我的个人主页我的专栏:人工智能领域、java-数据结构、Javase、C语言,MySQL,希望能帮助到大家!!!点赞👍收藏❤
引言:当你作为一个初学Java算法题的小白,可以点进来看看我这些算法基础题,能够很好的帮助你打好算法基本功。打好基础,才能更上一层楼。速速开始学起这些算法题吧!
1:输入宽度

代码详解:
packagedemo5_2;importjava.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-24 * Time:17:29 */publicclassMain10{publicstaticvoidmain(String[] args){Scanner sc=newScanner(System.in);String all=sc.next();System.out.println(String.format("%d %d %d",Integer.parseInt(all.substring(0,3)),Integer.parseInt(all.substring(3,6)),Integer.parseInt(all.substring(6,9)))); sc.close();}}2:%s格式符
输入字符串,然后输出前3个字符,要求占6列,右对齐。

详解代码
packagedemo5_2;importjava.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-24 * Time:17:44 */publicclassMain11{publicstaticvoidmain(String[] args){Scanner sc=newScanner(System.in);String c=sc.nextLine();System.out.println(String.format("%6s",c.substring(0,3)));}}注意:这里使用nextLine(),next()遇到空格或者换行符就会停止;nextLine()遇到换行符才会停止。
3:%f格式符
输入一个实数,第一次按实型输出;第二次保留2位小数输出;第三次保留3位小数但最小列宽8列输出,空格分隔。

代码详解:
packagedemo5_2;importjava.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-24 * Time:17:57 */publicclassMain12{publicstaticvoidmain(String[] args){Scanner sc=newScanner(System.in);double a=sc.nextDouble();System.out.println(String.format("%f",a)+" "+String.format("%.2f",a)+" "+String.format("%8.3f",a));}}4:小数、指数
输出3.1415926、12345678.123456789的小数、指数形式。

代码详解:
packagedemo5_2;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-24 * Time:23:30 */publicclassMain13{publicstaticvoidmain(String[] args){//小数、指数double n=3.1415926;double m=12345678.123456789;System.out.printf("%.6f %.6fe+000\n",n,n);System.out.printf("%.6f %.6fe+007",m,m/10000000);}}5:八、十六进制
输出202、117、70、130的十进制、八进制、十六进制数据形式,结果为0ddddd或0Xddddd。

代码详解:
/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-24 * Time:23:38 */publicclassMain{publicstaticvoidmain(String[] args){//输出202、117、70、130的十进制、八进制、十六进制数据形式,结果为0ddddd或0Xdddddint a=202,b=117,c=70,d=130;System.out.printf("%d 0%o 0X%X\n",a,a,a);System.out.printf("%d 0%o 0X%X\n",b,b,b);System.out.printf("%d 0%o 0X%X\n",c,c,c);System.out.printf("%d 0%o 0X%X\n",d,d,d);}}这里我们的八进制和十六进制有输出要求,所以我们在%o前面加上0,在%X前面加上0X。
6:合并
已知a、b、c是一个十进制数的百位、十位、个位,求这个十进制数。

代码详解:
packagedemo5_2;importjava.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-25 * Time:23:12 */publicclassMain15{publicstaticvoidmain(String[] args){Scanner sc=newScanner(System.in);String c=sc.nextLine().replace(" ","");System.out.println(Integer.valueOf(c)); sc.close();}}7:整数逆序
编写一个程序,要求输入一个两位数的数字,然后逆序输出数字。不考虑不合理的输入或是溢出等特殊情况。

代码详解:
packagedemo5_2;importjava.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-25 * Time:23:22 */publicclassMain16{publicstaticvoidmain(String[] args){Scanner sc=newScanner(System.in);//方法一:// int a=sc.nextInt();// int b=a%10;// int c=a/10;// System.out.print(b);// System.out.print(c);//---------------------方法二---------------StringBuilder stringBuilder=newStringBuilder();String str=sc.next(); stringBuilder.append(str).reverse();System.out.println(stringBuilder);}}8:多项式计算

代码详解:
packagedemo5_2;importjava.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-26 * Time:21:07 */publicclassMain17{publicstaticvoidmain(String[] args){Scanner sc=newScanner(System.in);int x=sc.nextInt();System.out.println((int)(Math.pow(x,6)+3*Math.pow(x,4)-2*Math.pow(x,5)-5*Math.pow(x,2)+6*x+7));}}这使用了Math.pow()方法他是double类型,所以在输出的时候需要强转成int类型
9:和的立方
输入为整数x,y,求x、y之和的立方。不考虑溢出等特殊情况。

代码详解:
packagedemo5_2;importjava.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-26 * Time:21:19 */publicclassMain18{publicstaticvoidmain(String[] args){Scanner sc=newScanner(System.in);int x=sc.nextInt();int y=sc.nextInt();System.out.println((x+y)*(x+y)*(x+y));}}10:绝对值
输入数字a并计算a的绝对值。不考虑不合理的输入或是溢出等特殊情况。

代码详解:
packagedemo5_2;importjava.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-26 * Time:21:23 */publicclassMain19{publicstaticvoidmain(String[] args){Scanner sc=newScanner(System.in);double a=sc.nextDouble();System.out.println(String.format("%.6f",Math.abs(a)));}}11:求圆面积和周长
请编写一个简单程序,输入半径,输出圆面积和周长。(PI是3.1415926)

代码详解:
packagedemo5_2;importjava.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-26 * Time:21:27 */publicclassMain20{publicstaticvoidmain(String[] args){//请编写一个简单程序,输入半径,输出圆面积和周长。(PI是3.1415926)Scanner sc=newScanner(System.in);double r=sc.nextDouble();doublePI=3.1415926;doubleCircumference=2*PI*r;doubleArea=PI*r*r;System.out.println("Area"+"="+String.format("%.6f",Area));System.out.println("Circumference"+"="+String.format("%.6f",Circumference));}}12:求矩形的面积和周长
请编写一个简单程序,输入矩形的长度和宽度,输出矩形的面积和周长。

代码详解:
packagedemo5_2;importjava.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-26 * Time:21:42 */publicclassMain21{publicstaticvoidmain(String[] args){Scanner sc=newScanner(System.in);double x=sc.nextDouble();double y=sc.nextDouble();doubleArea=x*y;doublePerimeter=2*(x+y);System.out.println("Area"+"="+String.format("%.6f",Area));System.out.println("Perimeter"+"="+String.format("%.6f",Perimeter));}}13:椭圆计算
请编写一个简单程序,输入长半轴和短半轴长度,计算输出椭圆的面积。(PI是3.1415926)

代码详解:
packagedemo5_2;importjava.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-26 * Time:21:48 */publicclassMain22{publicstaticvoidmain(String[] args){Scanner sc=newScanner(System.in);double a=sc.nextDouble();double b=sc.nextDouble();doublePI=3.1415926;doubleArea=PI*a*b;System.out.println("Area"+" "+"="+" "+String.format("%.6f",Area));}}今日的Java算法基础题就分享到这里了,我们下篇再见;🫰🫰🫰