PAT基础编程题目-6-9 统计个位数字
PAT基础编程题目-6-9 统计个位数字
题目详情
解答
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));
}
}
创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!