PAT基础编程题目-6-9 统计个位数字

PAT基础编程题目-6-9 统计个位数字

题目详情

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

解答

C语言版

#include <stdio.h>

int Count_Digit(const int N, const int D);

int main()
{
   
	int N, D;

	scanf("%d %d", &N, &D);
	printf("%d\n", Count_Digit(N, D));
	return 0;
}

int Count_Digit(const int N, const int D) {
   
	int n = N;
	int number[10];
	int size = 0;
	int count = 0;
	for (int i = 0; i < 10; i++)
	{
   
		number[i] = n % 10;
		printf("number[%d]=%d\n", i, number[i]);
		n = n / 10;
		size++;
		if (n == 0) {
   
			break;
		}
	}
	for (int j = 0; j < size; j++) {
   
		if ((D + number[j] == 0) || (D - number[j]== 0)) {
   
			count++;
		}
	}
	return count;
}

C++版

#include<iostream>
using namespace std;
int Count_Digit(const int N, const int D);

int main()
{
   
	int N, D;

	cin >> N >> D;
	cout<<Count_Digit(N, D);
	return 0;
}
int Count_Digit(const int N, const int D) {
   
	int n = N;
	int number[10];
	int size = 0;
	int count = 0;
	for (int i = 0; i < 10; i++)
	{
   
		number[i] = n % 10;
		n = n / 10;
		size++;
		if (n == 0) {
   
			break;
		}
	}
	for (int j = 0; j < size; j++) {
   
		if ((D + number[j] == 0) || (D - number[j] == 0)) {
   
			count++;
		}
	}
	return count;
}

Java版

public class Main{
   

	private static int Count_Digit(final int N, final int D) {
   
		int n = N;
		int [] number = new int[10];
		int size = 0;
		int count = 0;
		for (int i = 0; i < 10; i++)
		{
   
			number[i] = n % 10;
			n = n / 10;
			size++;
			if (n == 0) {
   
				break;
			}
		}
		for (int j = 0; j < size; j++) {
   
			if ((D + number[j] == 0) || (D - number[j] == 0)) {
   
				count++;
			}
		}
		return count;
	}
	
	public static void main(String[] args) {
   
		int N=0,D=0;
		Scanner scanner = new Scanner(System.in);
		if(scanner.hasNext()) {
   
			N = scanner.nextInt();
			D = scanner.nextInt();
		}
		scanner.close();
		System.out.println(Count_Digit(N, D));

	}

}

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

全部评论

相关推荐

10-28 14:42
门头沟学院 Java
watermelon1124:因为嵌入式炸了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务