记录下~数组贼方便
http://icpc.njust.edu.cn/Contest/6738/A/
数组方法:
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn = 10010;
int hhash[maxn] = {0};
int record[maxn][2];
int main(){
for(int i = 0;i<maxn;i++){
for(int j = 0;j <2;j++){
record[i][j] = 0;
}
}
int T,n;
while(scanf("%d",&T) != EOF){
for(int u = 0;u <T;u++){
scanf("%d",&n);//糖果颜色
for(int i = 0;i <n;i++){
int s,p;
scanf("%d %d",&s,&p);
hhash[s] = 1;
record[s][p]++;
}
int yanse = 0;
for(int j = 1;j <=n;j++){
if(hhash[j] == 1){
yanse++; //颜色种类
}
}
printf("%d\n",yanse);
for(int a = 1;a<=n;a++){
if(record[a][0] != 0 || record[a][1] != 0){
printf("%d %d %d\n",a,record[a][0],record[a][1]);
}
}
memset(hhash,0,sizeof(hhash));
for(int i = 0;i<maxn;i++){
for(int j = 0;j <2;j++){
record[i][j] = 0;
}
}
}
}
return 0;
}
结构体排序法:
# include <iostream>
# include <fstream>
# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
const int maxn = 10005;
int f[maxn];
struct Suger
{
int seq, f, q;
}s[maxn];
bool cmp(Suger x, Suger y)
{
return x.seq < y.seq;
}
int main()
{
int T;
// freopen("1.in", "r", stdin);
// freopen("1.out", "w", stdout);
cin >>T;
while(T--)
{
int n, seq, p, cnt;
cin >>n;
cnt = 0;
memset(f, 0, sizeof(f));
for(int i = 1; i <= n; i++)
{
s[i].seq = 0;
s[i].f = 0;
s[i].q = 0;
}
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &seq, &p);
s[seq].seq = seq;
if(!f[seq]) f[seq] = 1, cnt++;
if(!p) s[seq].f++;
else s[seq].q++;
}
sort(s+1, s+n+1, cmp);
printf("%d\n", cnt);
//outfile<<cnt<<endl;
for(int i = 1; i <= n; i++)
{
if(s[i].seq)
{
printf("%d %d %d", s[i].seq, s[i].f, s[i].q);
printf("\n");
}
}
}
return 0;
}