hdu 1222 Wolf and Rabbit

http://acm.hdu.edu.cn/showproblem.php?pid=1222

我本来想做到约瑟夫环的题目,百度了一下,说hdu1222是关于约瑟夫环的,这于是我写了个约瑟夫环的循环单链表的代码,结果坑爹了,超时。最后想想,这道题根本不要用循环单链表来求解。只要验证m 与n是否互质就行了。下面是代码,第一个是我利用循环单链表的代码,应该正确,但是超时:

#include <stdio.h>
#include <malloc.h>

typedef struct node
{
        bool ss;
        node *next;
}node;

int main()
{
    int n, m, p;
    while (scanf("%d", &p) != EOF)
    {
          while(p --)
          {
                  scanf("%d%d", &m, &n);          
                  node *head, *p;
                   head = (node*)malloc(sizeof(node));
                   head -> ss = 0; 
                   p = head;
                   int j, w;
                   j = n;
                   w = m;
                   while (j--)
                   {
                         node *temp;
                         temp = (node*)malloc(sizeof(node));
                         temp -> ss = 0;
                         p -> next = temp;
                         p = temp;
                   }
                   p -> next = head -> next;
                   head = head -> next;
                   node *g;
                   g = head;
                   bool b = 0;
                   j = n; 
                   while (j--)
                   {
                         if (g -> ss == 1)
                         {
                                      printf("YES\n");
                                      b = g -> ss; 
                                      break; 
                         }
                         g -> ss = 1;
                         w = m;
                         while (w)
                         {
                               g = g -> next;   
                               w--;                          
                         } 
                         
                   }
                   if (b == 0)  
                   printf("NO\n");
          } 
    }
    system ("pause"); 
    return 0;
}


接下来是我的利用辗转相除法求最大公约数,判断是否互质,最后判断YES or NO的问题

#include <iostream>
using namespace std;
int gcd(int a,int b)
{
    return !b?a:gcd(b,a%b);
}
int main()
{
    int sum;
    cin>>sum;
    while(sum--)
    {
        int a,b;
        cin>>a>>b;
        if(gcd(a,b)==1) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
    system ("pause");
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值