首页 > 试题广场 >

分组统计

[编程题]分组统计
  • 热度指数:5924 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
先输入一组数,然后输入其分组,按照分组统计出现次数并输出参见样例。

输入描述:
输入第一行表示样例数m,对于每个样例,第一行为数的个数n,接下来两行分别有n个数,第一行有n个数,第二行的n个数分别对应上一行每个数的分组,n不超过100。


输出描述:
输出m行,格式参见样例,按从小到大排。
示例1

输入

1
7
3 2 3 8 8 2 3
1 2 3 2 1 3 1

输出

1={2=0,3=2,8=1}
2={2=1,3=0,8=1}
3={2=1,3=1,8=0}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNext()) {
			int m = scanner.nextInt();
			for(int i = 0;i < m; i++) {
				
				int n = scanner.nextInt();
				
				//保存数的数组
				int[] arr = new int[n];
				//保存组的数组
				int[] group = new int[n];
			
				//用来统计不同数的情况,排除重复
				List<Integer> arrList = new ArrayList<Integer>();
				//用来统计不同组的情况,排除重复
				List<Integer> groupList = new ArrayList<Integer>();
				
				for (int j = 0; j < n; j++) {
					arr[j] = scanner.nextInt();
					if (!arrList.contains(arr[j])) {
						arrList.add(arr[j]);
					}
				}

				//数按顺序排列
				Collections.sort(arrList);
				
				for (int j = 0; j < n; j++) {
					group[j] = scanner.nextInt();
					if (!groupList.contains(group[j])) {
						groupList.add(group[j]);
					}
				}
				
				//组按顺序排列
				Collections.sort(groupList);
				
				for (int j = 0; j < groupList.size(); j++) {
					
					System.out.print(groupList.get(j)+"={");
					
					for (int k = 0; k < arrList.size(); k++) {
						
						System.out.print(arrList.get(k)+"=");
						
						//用来统计该组中,某数出现的次数
						int sum = 0;
						
						for (int k2 = 0; k2 < n; k2++) {
							
							if (arr[k2] == arrList.get(k) && group[k2] == groupList.get(j)) {
								sum++;
							}
						}
						if(k == arrList.size()-1) {
							//如果是最后一个则后面加"}"
							System.out.println(sum+"}");
						}else {
							//如果不是末尾则后面加","
							System.out.print(sum+",");
						}
					}
					
				}
				
			}
		}
	}
}


编辑于 2024-03-22 15:32:11 回复(0)
import java.util.Arrays;
import java.util.Scanner;
public class Main {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  int n = sc.nextInt();
  for (int i = 0; i < n; i++) {
   int m = sc.nextInt();
   int[] a = new int[m];
   int[] b = new int[m];
   for (int j = 0; j < m; j++) {
    a[j] = sc.nextInt();
   }
   for (int k = 0; k < m; k++) {
    b[k] = sc.nextInt();
   }
   int[][] nb = new int[1010][1010];
   for (int x = 0; x < a.length; x++) {
     nb[b[x]][a[x]]++;
   }
   for (int x = 0; x < nb.length; x++) {
    if(exit(b,x)){
                    System.out.print(x+"={");
        for (int y = 0; y < nb.length; y++) {
         if(exit(a,y)){
                            if(Min(a,y)){
                             System.out.print(y+"="+nb[x][y]);
                            }else{
                             System.out.print(","+y+"="+nb[x][y]);
                            }
         }
     }
                    System.out.println("}");
    }
   }
  }
 }
 public static boolean exit(int [] q,int p){
        for (int z = 0; z < q.length; z++) {
   if(p==q[z]){
    return true;
   }
  }
  return false;
    }
 public static boolean Min(int [] q,int p){
  Arrays.sort(q);
  if(q[0]==p){
   return true;
  }
  return false;
    }
}
 
发表于 2019-03-19 19:14:30 回复(0)