欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PAT基础编程题目-7-3 逆序的三位数

程序员文章站 2022-06-07 11:39:27
...

PAT基础编程题目-7-3 逆序的三位数

题目详情

PAT基础编程题目-7-3 逆序的三位数

题目地址:https://pintia.cn/problem-sets/14/problems/783

解答

C语言版

#include<stdio.h>
int main() {
	int array[3];
	int number;
	scanf("%d", &number);
	for (int i = 0; i < 3; i++)
	{
		array[i] = number % 10;
		number = number / 10;
	}
	for (int j = 0; j < 3; j++)
	{
		if (array[j] || (array[1]==0 && array[0]!=0) )
			printf("%d", array[j]);
	}
	return 0;
}

PAT基础编程题目-7-3 逆序的三位数

C++版

#include<iostream>
using namespace std;
int main() {
	int array[3];
	int number;
	cin >> number;
	for (int i = 0; i < 3; i++)
	{
		array[i] = number % 10;
		number = number / 10;
	}
	for (int j = 0; j < 3; j++)
	{
		if (array[j] || (array[1] == 0 && array[0] != 0))
			cout << array[j];
	}
	return 0;
}

PAT基础编程题目-7-3 逆序的三位数

Java版

import java.util.Scanner;
public class Main{

	public static void main(String[] args) {
		int number = 0;
		int [] array = new int[3];
		Scanner scanner = new Scanner(System.in);
		if(scanner.hasNext()) {
			number = scanner.nextInt();
		}
		scanner.close();
		for (int i = 0; i < array.length; i++) {
			array[i] = number%10;
			number = number/10;
		}
		for (int j = 0; j < array.length; j++) {
			if (array[j]!=0 || (array[1]==0 && array[0]!=0)) {
				System.out.print(array[j]);
			}
		}
	}

}

PAT基础编程题目-7-3 逆序的三位数

创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!