【算法题】别再为 Java 算法题犯难,码蹄杯上这些新手题库帮你打好基础
我的个人主页 我的专栏: 人工智能领域、java-数据结构、Javase、C语言,MySQL,希望能帮助到大家!!! 点赞👍收藏❤
前言:
码蹄杯作为编程学习中经典的逻辑训练题型,是提升算法思维与代码实践能力的“磨刀石”。对于初入编程领域的学习者而言,从基础题入手拆解问题逻辑是快速入门的关键。本次分享将围绕码蹄杯基础题型展开,涵盖循环逻辑、条件判断、数组操作等核心知识点,通过典型例题解析与思路拆解,帮助大家掌握从问题建模到代码实现的完整流程。无论你是零基础的编程小白,还是希望巩固基础的学习者,都能在本次分享中收获解题技巧,为挑战更复杂的编程任务夯实基础。
一:实型数运算
题目:请编写一个简单程序,用户输入2个实型数据存储在变量中,并输出他们的乘积与商。(本题不考虑除数为0的情况)
题目详解
package demo5_2;import java.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-11 * Time:11:16 */public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); float a=sc.nextFloat(); float b=sc.nextFloat();// System.out.println(String.format(\"%.6f\",a)+\"*\"+String.format(\"%.6f\",b)+\"=\"+String.format(\"%.6f\",a*b));// System.out.println(String.format(\"%.6f\",a)+\"/\"+String.format(\"%.6f\",b)+\"=\"+String.format(\"%.6f\",a/b)); System.out.printf(\"%.6f*%.6f=%.6f\\n\",a,b,a*b); System.out.printf(\"%.6f/%.6f=%.6f\",a,b,a/b); }}
二:平均分
题目详解
package demo5_2;import java.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-13 * Time:22:27 */public class Main1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); double a=sc.nextDouble(); double b=sc.nextDouble(); double c=sc.nextDouble(); double total=a+b+c; double avg=(a+b+c)/3; System.out.println(String.format(\"%.6f\",total)); System.out.println(String.format(\"%.6f\",avg)); }}
三:圆球等的相关计算
请编写一个简单程序,输入半径和高,输出圆周长,圆面积,球面积,球体积,圆柱体积。(PI = 3.1415926)
代码详解:
package demo5_2;import java.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-15 * Time:23:51 */public class Main2 { public static final double PI = 3.14159; public static void main(String[] args) { // 输出圆周长,圆面积,球面积,球体积,圆柱体积。 Scanner sc=new Scanner(System.in); double r=sc.nextDouble(); double high=sc.nextDouble(); double circleZ=PI*2.0*r; double circleM=PI*r*r; double qiuM=PI*r*r*4.0; double qiuT=PI*r*r*r*4*1.0/3.0; double circleZT=PI*r*r*high; System.out.println(String.format(\"%.2f\",circleZ)); System.out.println(String.format(\"%.2f\",circleM)); System.out.println(String.format(\"%.2f\",qiuM)); System.out.println(String.format(\"%.2f\",qiuT)); System.out.println(String.format(\"%.2f\",circleZT)); }}
四:公式计算
代码详解:
package demo5_2;import java.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-17 * Time:8:58 */public class Main3 { public static void main(String[] args) { //计算公式(1/2)∗(a∗x+(a+x)/(4∗a)) Scanner sc=new Scanner(System.in); int x=sc.nextInt(); int a=sc.nextInt(); double s=(1.0/2)*(a*x+(a+x)*1.0/(4.0*a)); System.out.println(String.format(\"%.2f\",s)); System.out.printf(\"%.2f\\n\",s); }}
五:输入和输出字符型数据
请编写一个简单程序,用户输入2个的字符型数据存储在变量中,并分别以字符形式和整数形式输出在屏幕上。
代码详解:
package demo5_2;import java.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-17 * Time:9:25 */public class Main4 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.next(); String []b=s.split(\",\"); int A=s.charAt(0); int B=s.charAt(2); System.out.println(\"The ASCII code of \"+ b[0] + \" is \" +A); System.out.println(\"The ASCII code of \"+ b[1] + \" is \" +B); }}
六:字符和整数
输出X、65的字符、十进制数据形式。
import java.util.Scanner;import java.util.*;class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); // code here System.out.println(\"X\"+\" \"+(int)\'X\'); System.out.println(\"A\"+\" \"+(int)\'A\'); input.close(); }}
七:各种类型长
请编写一个简单程序,输出int、float、double和char的大小。
详解代码:
/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-17 * Time:9:51 */public class Main { public static void main(String[] args) { int a=Integer.SIZE; int b=Float.SIZE; int c=Double.SIZE; int d=Character.SIZE; System.out.println(\"Size of int: \"+a/8+\" \"+\"bytes\"); System.out.println(\"Size of float: \"+b/8+\" \"+\"bytes\"); System.out.println(\"Size of double: \"+c/8+\" \"+\"bytes\"); System.out.println(\"Size of char: \"+d/16+\" \"+\"byte\"); }}
八:关键字long
请编写一个简单程序,输出int、long int、long long int、double和long double的大小
题目详解代码:
/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-19 * Time:22:17 */public class Main { //请编写一个简单程序,输出int、long int、long long int、double和long double的大小 public static void main(String[] args) { int a=Integer.SIZE; int b=Long.SIZE; int c=Long.SIZE; int d=Double.SIZE; int e=Double.SIZE; System.out.println(\"Size of int =\"+\" \" +a/8+\" bytes\"); System.out.println(\"Size of long int =\"+\" \"+b/8+\" bytes\"); System.out.println(\"Size of long long int =\"+\" \"+c/8+\" bytes \"); System.out.println(\"Size of double =\"+\" \"+d/8 +\" bytes\"); System.out.println(\"Size of long double =\"+\" \"+e*2/8+\" bytes\"); }}
九:输入分隔符
输入“a=22,b=b,c=14,d=d”给变量a、b、c、d,然后再输出他们。
代码详解:
package demo5_2;import java.util.Scanner;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-19 * Time:22:34 */public class Main7 { public static void main(String[] args) {// 输入“a=22,b=b,c=14,d=d”给变量a、b、c、d,然后再输出他们。 Scanner sc=new Scanner(System.in); String[] arr=sc.nextLine().split(\",\"); String avalue=\"\",bvalue=\"\",cvalue=\"\",dvalue=\"\"; for(String x:arr){ String[] parts=x.split(\"=\"); if(parts.length==2){ String key=parts[0].trim(); String value=parts[1].trim(); switch(key){ case\"a\": avalue=value; break; case\"b\":bvalue=value;break; case\"c\":cvalue=value;break; case\"d\":dvalue=value;break; } } } System.out.printf(\"%s %s %s %s%n\",avalue,bvalue,cvalue,dvalue); sc.close(); } }
十:宽度与对齐
代码详解:
package demo5_2;/** * Created with IntelliJ IDEA. * Description: * User:Lenovo * Date:2025-05-20 * Time:22:45 */public class Main8 { //输出455、-123、987654,宽度为5,分别左对齐和右对齐 public static void main(String[] args) { int a=455,b=-123,c=987654; System.out.printf(\"%-5d %5d\\n\",a,a); System.out.printf(\"%-5d %5d\\n\",b,b); System.out.printf(\"%-5d %5d\\n\",c,c); }}
这篇的Java码蹄杯算法题就分享到这里了,我们下篇再见!🫰🫰🫰