【模拟】XMU 1054 Hacker

64 篇文章 3 订阅
37 篇文章 1 订阅
题目链接:

  http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1054

题目大意

  多组数据,n=0结束。给定n(n<=1 000 000)和n个ip(*.*.*.*)求出现次数超过一半的ip是多少(数据保证有)。

题目思路:

  【模拟】

  暴力模拟能A。

  暴力排序能A。

排序:

//
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 1000004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
struct xxx
{
	int s1,s2,s3,s4;
}a[N];
bool cmp(xxx aa,xxx bb)
{
	if(aa.s1!=bb.s1)return aa.s1<bb.s1;
	if(aa.s2!=bb.s2)return aa.s2<bb.s2;
	if(aa.s3!=bb.s3)return aa.s3<bb.s3;
	return aa.s4<bb.s4;
}
int main()
{
	#ifndef ONLINE_JUDGE
//	freopen("1.txt","r",stdin);
//	freopen("2.txt","w",stdout);
	#endif
	int i,j,k;
//	for(scanf("%d",&cas);cas;cas--)
//	for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
//	while(~scanf("%s",s))
	while(~scanf("%d",&n) && n)
	{
		printf("Case %d:\n",++cas);
		for(i=1;i<=n;i++)
			scanf("%d.%d.%d.%d",&a[i].s1,&a[i].s2,&a[i].s3,&a[i].s4);
		sort(a+1,a+1+n,cmp);
		printf("%d.%d.%d.%d\n",a[(n+1)/2].s1,a[(n+1)/2].s2,a[(n+1)/2].s3,a[(n+1)/2].s4);
	}
	return 0;
}
/*
//

//
*/


模拟:

//
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 256
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int u[4][N];
int a,b,c,d,aa,bb,cc,dd;
int main()
{
	#ifndef ONLINE_JUDGE
//	freopen("1.txt","r",stdin);
//	freopen("2.txt","w",stdout);
	#endif
	int i,j,k;
//	for(scanf("%d",&cas);cas;cas--)
//	for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
//	while(~scanf("%s",s))
	while(~scanf("%d",&n) && n)
	{
		printf("Case %d:\n",++cas);
		memset(u,0,sizeof(u));
		aa=bb=cc=dd=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d.%d.%d.%d",&a,&b,&c,&d);
			u[0][a]++;u[1][b]++;u[2][c]++;u[3][d]++;
			if(u[0][a]>n/2)aa=a;
			if(u[1][b]>n/2)bb=b;
			if(u[2][c]>n/2)cc=c;
			if(u[3][d]>n/2)dd=d;
		}
		printf("%d.%d.%d.%d\n",aa,bb,cc,dd);
	}
	return 0;
}
/*
//

//
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值