首页 > 试题广场 >

整数的十位

[编程题]整数的十位
  • 热度指数:45159 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}给定一个整数,请计算该整数的十位数字。注意,如果十位上的数字存在,则一定为正数,否则输出 0

输入描述:
\hspace{15pt}在一行中输入一个整数 a \left(0 \leqq a \leqq 10^9\right)


输出描述:
\hspace{15pt}输出一个整数,表示 a 的十位数字。
示例1

输入

114

输出

1

说明


示例2

输入

6

输出

0

备注:
本题已于下方时间节点更新,请注意题解时效性:
1. 2025-11-19 优化题面文本与格式,新增若干组数据。
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();

        int absA = Math.abs(a);

        int divided = absA / 10;

        int res = divided % 10;

        System.out.println(res);

        sc.close();
    }
}
  1. 用 Math.abs(a) 获取整数的绝对值(处理负数情况,如 -514 变为 514)
  2. 用 absA / 10 实现「除以 10 并向下取整」(Java 中整数除法自动向下取整)
  3. 用 divided % 10 对结果取余 10,得到十位数字

  1. 边界情况处理
    • 个位数(如 6、-3):除以 10 后为 0,取余 10 结果为 0
    • 整十数(如 100、-230):100/10=10 → 10%10=0,-230→230/10=23→23%10=3
    • 最大 / 最小值(如 10⁹、-10⁹):整数范围内计算无溢出,结果正确

发表于 2025-10-04 13:00:22 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            System.out.println(((a < 0 ? a * -1 : a) % 100) / 10);
        }
    }
}

发表于 2025-08-29 13:48:21 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a = in.nextInt();
        System.out.print(a%100/10);
    }
}
发表于 2025-08-21 11:07:12 回复(0)
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b =a/10%10;
            System.out.println(b);
        }
    }
}
发表于 2023-08-30 13:42:23 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        System.out.print(n%100/10);
    }
}

发表于 2022-07-24 23:45:22 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        System.out.println(num /10 % 10);
    }
}

发表于 2022-06-23 14:07:12 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println(sc.nextInt()/10%10);
    }
}

发表于 2022-05-28 20:36:27 回复(0)