PAT基础编程题目-7-16 求符合给定条件的整数集

PAT基础编程题目-7-16 求符合给定条件的整数集

题目详情

PAT基础编程题目-7-16 求符合给定条件的整数集_第1张图片

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

解答

C语言版

#include
int main()
{
	int a;
	scanf("%d", &a);
	int number[4] = {a, a+1, a+2, a+3};
	int row = 0, col = 0;  //统计行数和列数,控制行后面的换行和列后面的空格
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			if (j!=i)
			{
				for (int k = 0; k < 4; k++)
				{
					if (k!=i && k!=j)
					{
						printf("%d%d%d", number[i], number[j], number[k]);
						col++;
						if (col<6)
							printf(" ");
					}
				}
			}
		}
		row++;
		if (row<4)
			printf("\n");
		col = 0;
	}
	return 0;
}

PAT基础编程题目-7-16 求符合给定条件的整数集_第2张图片

C++

#include
using namespace std;
int main() {
	int a;
	cin >> a;
	int number[4] = { a, a + 1, a + 2, a + 3 };
	int row = 0, col = 0;  //统计行数和列数,控制行后面的换行和列后面的空格
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			if (j != i)
			{
				for (int k = 0; k < 4; k++)
				{
					if (k != i && k != j)
					{
						cout << number[i] << number[j] << number[k];
						col++;
						if (col < 6)
							cout << " ";
					}
				}
			}
		}
		row++;
		if (row < 4)
			cout << endl;
		col = 0;
	}
	return 0;
}

PAT基础编程题目-7-16 求符合给定条件的整数集_第3张图片

Java版

import java.util.Scanner;
public class Main{

	public static void main(String[] args) {
		int a = 0;
		Scanner scanner = new Scanner(System.in);
		if (scanner.hasNext()) {
			a = scanner.nextInt();
		}
		scanner.close();
		int [] number = { a, a + 1, a + 2, a + 3 };
		int row = 0, col = 0;  //统计行数和列数,控制行后面的换行和列后面的空格
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (j != i) {
					for (int k = 0; k < 4; k++) {
						if (k != i && k != j) {
							/*System.out.print(number[i]);
							System.out.print(number[j]);
							System.out.print(number[k]);*/
							// 采用一行输出则需要先转成字符串类型
							System.out.print(Integer.toString(number[i])+Integer.toString(number[j])+Integer.toString(number[k]));
							col++;
							if (col < 6)
								System.out.print(" ");
						}
					}
				}
			}
			row++;
			if (row < 4)
				System.out.println();
			col = 0;
		}

	}

}

PAT基础编程题目-7-16 求符合给定条件的整数集_第4张图片

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

你可能感兴趣的:(C,C++,Java,PAT,基础编程题)