相同的雪花

相同的雪花
时间限制:1000 ms | 内存限制:65535 KB
难度:4
描述
You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.
输入
The first line of the input will contain a single interger T(0< T< 10),the number of the test cases.
The first line of every test case will contain a single integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed by n lines, each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000), the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise or counterclockwise), but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.
输出
For each test case,if all of the snowflakes are distinct, your program should print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.
样例输入
1
2
1 2 3 4 5 6
4 3 2 1 6 5
样例输出
Twin snowflakes found.

分析:
题目意思是,有n片雪花,每片雪花6边。判断在这n个雪花中是否有相同的雪花。从某边开始顺时针或逆时针度,6边依次相等,则雪花相同。
开始时由于问的事存不存在,就想到了set< string >,想排序后在进行比较,但是后来想了想有同构的现象:123456和345612是一样的,但是和132456不一样,但是排序之后就都一样了。所以比较的话就只能暴力了。但是要是每一个都要和其他的比较,那一定会超时,所以我的方法是想进行排序,排序相同的才可能是一样的,然后就只用比较相邻两个了。

AC代码:

#include<iostream>
#include <ctime>
#include <algorithm>
#include <cstdio>
using namespace std;

int num[100010][12];
bool judge;
int cmp(const void *c,const void *d)
{
    int *p=(int*)c;
    int *q=(int*)d;
    sort(p,p+6);
    sort(q,q+6);
    int i;
    for( i=0;i<6;i++)
        if(p[i]!=q[i])
            return p[i]-q[i];
    if(i==6)
    {
        return 0;
    }
}
bool cmp2(int *p,int *q)
{
    if(p[0]==q[0]&&p[1]==q[1]&&p[2]==q[2]&&p[3]==q[3]&&p[4]==q[4]&&p[5]==q[5])
        return true;
    if(p[0]==q[1]&&p[1]==q[2]&&p[2]==q[3]&&p[3]==q[4]&&p[4]==q[5]&&p[5]==q[0])
        return true;
    if(p[0]==q[2]&&p[1]==q[3]&&p[2]==q[4]&&p[3]==q[5]&&p[4]==q[0]&&p[5]==q[1])
        return true;
    if(p[0]==q[3]&&p[1]==q[4]&&p[2]==q[5]&&p[3]==q[0]&&p[4]==q[1]&&p[5]==q[2])
        return true;
    if(p[0]==q[4]&&p[1]==q[5]&&p[2]==q[0]&&p[3]==q[1]&&p[4]==q[2]&&p[5]==q[3])
        return true;
    if(p[0]==q[5]&&p[1]==q[0]&&p[2]==q[1]&&p[3]==q[2]&&p[4]==q[3]&&p[5]==q[4])
        return true;
    if(p[0]==q[0]&&p[5]==q[1]&&p[4]==q[2]&&p[3]==q[3]&&p[2]==q[4]&&p[1]==q[5])
        return true;
    if(p[0]==q[1]&&p[5]==q[2]&&p[4]==q[3]&&p[3]==q[4]&&p[2]==q[5]&&p[1]==q[0])
        return true;
    if(p[0]==q[2]&&p[5]==q[3]&&p[4]==q[4]&&p[3]==q[5]&&p[2]==q[0]&&p[1]==q[1])
        return true;
    if(p[0]==q[3]&&p[5]==q[4]&&p[4]==q[5]&&p[3]==q[0]&&p[2]==q[1]&&p[1]==q[2])
        return true;
    if(p[0]==q[4]&&p[5]==q[5]&&p[4]==q[0]&&p[3]==q[1]&&p[2]==q[2]&&p[1]==q[3])
        return true;
    if(p[0]==q[5]&&p[5]==q[0]&&p[4]==q[1]&&p[3]==q[2]&&p[2]==q[3]&&p[1]==q[4])
        return true;
    return false;
}
int main()
{
    int k=0,i,j,t,n;
    scanf("%d",&t);
    while(t--)
    {
            scanf("%d",&n);
            judge=false;
            for(j=0;j<n;j++)
                for(i=0;i<6;i++)
                {
                    scanf("%d",&num[j][i]);
                    num[j][i+6]=num[j][i];
                }
            qsort(num,n,sizeof(num[0]),cmp);
            for(j=1;j<n;j++)
            {
                if(cmp2(num[j]+6,num[j-1]+6))
                {
                    judge=true;
                    break;
                }
            }
            if(judge)
                printf("Twin snowflakes found.\n");
            else
                printf("No two snowflakes are alike.\n");
    }
}

qsort的用法详解:
qsort 功 能: 使用快速排序例程进行排序   

用 法: void qsort(void base, int nelem, int width, int (*fcmp)(const void ,const void *));   

各参数:1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针

用于确定排序的顺序 排序方法有很多种, 选择排序,冒泡排序,归并排序,快速排序等。 看名字都知道快速排序 是目前公认的一种比较好的排序算法(我没听书速度比这快的了,特殊场合例外),比选择排序,冒泡排序都要快。这是因为他速度很快,所以系统也在库里实现这个算法,便于我们的使用。 这就是qsort。

qsort 要求提供一个 比较函数,是为了做到通用性更好一点。比如你不仅仅的是要排序一个数字而已,可能你要用来排序几个数字 ,比如有一个结构 struct num { int a; int b; }; 然后我有一个num 类型的数组, num dddd[100]; 我想给 dddd这个数组排序,那怎么办? 我想让 a +b 最大的num元素排在数组的最前面,那又怎么办? 这都可以通过定义比较函数来做到的。 比较函数的作用就是给qsort指明 元素的大小是怎么比较的。 像这样的比较函数 inline int MyCmp(const void* a, const void* b) 都是有两个元素 作为参数,返回一个int 值, 如果 比较函数返回大于0,qsort就认为 a>b , 如果比较函数返回等于0 qsort就认为a 和b 这两个元素相等,返回小于零 qsort就认为 ab),你比较函数却返回一个 -1 (小于零的)那么qsort认为a<本文中排序都是采用的从小到大排序>

一、对int类型数组排序

int num[100];
Sample: int cmp ( const void *a , const void *b )
{ return *(int *)a - *(int *)b; }
qsort(num,100,sizeof(num[0]),cmp);

二、对char类型数组排序(同int类型)

char word[100];
Sample: int cmp( const void *a , const void *b )
{ return *(char *)a - *(int *)b; }
qsort(word,100,sizeof(word[0]),cmp);

三、对double类型数组排序(特别要注意)

double in[100];
int cmp( const void *a , const void *b )
{ return *(double *)a > *(double *)b ? 1 : -1; }
qsort(in,100,sizeof(in[0]),cmp);

四、对结构体一级排序

struct In { double data; int other; }s[100]
//按照data的值从小到大将结构体排序,关于结构体内的排序关键数据data的类型可以很多种,
//参考上面的例子写
int cmp( const void *a ,const void *b)
{ return (*(In *)a).data > (*(In *)b).data ? 1 : -1; }
qsort(s,100,sizeof(s[0]),cmp);

五、对结构体二级排序

struct In { int x; int y; }s[100];//按照x从小到大排序,当x相等时按照y从大到小排序
int cmp( const void *a , const void *b )
{
struct In *c = (In *)a; struct In *d = (In *)b;
if(c->x != d->x)
          return c->x - d->x;
else
          return d->y - c->y;
}
qsort(s,100,sizeof(s[0]),cmp);

六、对字符串进行排序

struct In { int data; char str[100]; }s[100];//按照结构体中字符串str的字典顺序排序
int cmp ( const void *a , const void *b )
{
return strcmp( (*(In *)a)->str , (*(In *)b)->str );
}
qsort(s,100,sizeof(s[0]),cmp);

七、计算几何中求凸包的cmp

int cmp(const void *a,const void *b)//重点cmp函数,把除了1点外的所有点,旋转角度排序
{
struct point *c=(point *)a;
struct point *d=(point *)b;
if( calc(*c,*d,p[1]) < 0)
         return 1;
else if( !calc(*c,*d,p[1]) && dis(c->x,c->y,p[1].x,p[1].y) < dis(d->x,d->y,p[1].x,p[1].y))//如果在一条直线上,则把远的放在前面
        return 1;
else return -1;
}

作者:syxChina
出处:http://syxchina.cnblogs.com

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值