(算法练习)——数糖果

记录下~数组贼方便
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值