import java.util.Scanner; public class Main { public static void main(String args[]){ Scanner s= new Scanner(System.in); int n=s.nextInt(); int a[]=new int [n]; while(--n>=0) a[n]=s.nextInt(); int x=s.nextInt(),count=0; for(int i:a) { if(i==x) count++; } System.out.println(count); } }
审题最重要
C语言 解法
#include<stdio.h> int main() { int n=0,i=0,x,sum=0; scanf("%d",&n); int arr[100]; for (i=0;i<n;i++)// 这里注意一下 < n 并不是 <= n { scanf("%d ",&arr[i]); } scanf("%d",&x); for (i=0;i<n;i++)//写循环 判断 { if(x == arr[i]) sum =sum+1; } printf("%d\n",sum); return 0; }
var num=readline()*1; let arr=readline().split(' ').map(Number); let find_num=readline()*1; console.log(arr.filter(value=>value==find_num).length)
#include <stdio.h> #include <stdlib.h> int main(void) { int i, x, n, count; while (scanf("%d", &n) != EOF && (n >= 1 && n <= 100)) { int *a = (int *)malloc(sizeof(int) * n); if (NULL == a) { fprintf(stderr, "Memory allocation failed!\n"); exit(EXIT_FAILURE); } for (i = 0; i < n; i++) { scanf("%d", a + i); } scanf("%d", &x); for (i = 0, count = 0; i < n; i++) { if (x == a[i]) { count++; } } printf("%d\n", count); free(a); a = NULL; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { unordered_map<int, int>map; int x , z;cin>>x; while(x--){ cin>>z; map[z]++; } cin>>x; cout<<map[x]<<endl; }
#include <stdio.h> #include <stdlib.h> int main() { int n,x; int count = 0; //记录x的出现次数 scanf("%d", &n); int* a = (int*)malloc(sizeof(int)*n); for(int i = 0; i < n; i++) { scanf("%d", &a[i]); } scanf("%d", &x); for(int i = 0; i < n; i++) { if(x == a[i]) count++; } printf("%d", count); return 0; }
#include <stdio.h> int main() { //定义 int n=0; scanf("%d",&n); int count=0; int arr[100]; int i=0; //输入 for(i=0;i<n;i++) { scanf("%d ",&arr[i]); } int k=0; scanf("%d",&k); //判断--遍历一遍如果有和k相等的数,count就加1 for(i=0;i<n;i++) { if(arr[i]==k) { count++; } } //输出 printf("%d\n",count); return 0; }
#include <stdio.h> int main() { int n; scanf("%d",&n); int array[n]; int i; for(i=0;i<n;i++) { scanf("%d",&array[i]); } int x,count=0; scanf("%d",&x); for(i=0;i<n;i++) { if(array[i]==x) { count++; } } printf("%d",count); return 0; }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } int num = in.nextInt(); int count = 0; for (int i = 0; i < n; i++) { if (num == nums[i]) { count++; } } System.out.println(count); } }
import java.util.Scanner; import java.util.ArrayList; // 注意类名必须为 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(); ArrayList<Integer> list =new ArrayList<>(); for(int i=0;i<a;i++) { list.add(in.nextInt()); } int b =in.nextInt(); System.out.println(list.stream().filter(x -> x==b ).count()); } } }