zoukankan      html  css  js  c++  java
  • poj 3750 链表

    http://poj.org/problem?id=3750小孩报数问题

    好不容易写出了单链表版本,提交竟然out time limited。代码如下:

    #include <iostream>
    #include <string>
    using namespace std;

    typedef struct child
    {
    char name[20];
    struct child* next;
    };
    void child_delete(child* p)
    {
    child* _p = p->next;
    p->next = _p->next;
    free(_p);
    }
    int main()
    {
    int N,_N;
    child* head;
    child* tail;
    head = (child*)malloc(sizeof(child));
    tail = head;
    tail->next = NULL;

    int flag = 1;
    int flag2 = 1;
    cin>>N;
    _N = N;
    while(N--)
    {
    child* stu;
    if(flag ==1)
    {
    stu = head;
    flag = 0;
    }
    else
    {
    stu = (child*)malloc(sizeof(child));
    if(stu == NULL)
    return 0;
    }
    cin>>stu->name;

    stu->next = NULL;
    if(flag2 == 1)
    {
    tail = stu;
    flag2 = 0;
    }
    else
    {
    tail->next = stu;
    tail = stu;
    }
    }
    tail->next = head; //形成一个圈
    child* pp;
    pp = head;
    //while(pp)
    //{
    // cout<<pp->name<<endl;
    // pp = pp->next;
    //}
    int w,s,_s;
    scanf("%d,%d",&w,&s);

    int _w = 1;
    pp = head;
    while(_w<w)
    {
    pp = pp->next;
    _w++;
    }
    _s = s;
    while(_N--)
    {
    s = _s;
    s--;
    s--;
    while(s--)
    {
    pp = pp->next;
    }

    cout<<pp->next->name<<endl;

    //删除pp指向的下一个指针,并处理尾指针
    child_delete(pp);
    pp = pp->next;
    }
    return 0;
    }

     于是在网上搜了下解法,原来,不用链表也可以。这种循环控制的想法真好。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main()
    {
    char a[70][100];
    int s[70];
    int n,i,k;
    int x,y;
    scanf("%d",&n);
    for(i = 1;i<=n;i++)
    {
    scanf("%s",a[i]);
    s[i] = 0;
    }
    scanf("%d,%d",&x,&y);
    k=0;
    int count=0;
    for(i=x;;i++)
    {
    if(i>n) i=1;
    if(count==n)
    break;
    if(s[i]==0)
    k++;
    if(k==y)
    {
    printf("%s\n",a[i]);
    s[i]=1;
    count++;
    k=0;
    }
    }
    return 0;
    }

    还算有收获,记录在此。

  • 相关阅读:
    tomcat修改端口
    JSP_大并发_秒杀
    Nexus刷官方下载的映像_occam
    Nexus杂
    多项式ADT加法乘法——数组实现
    单链表——游标实现
    链表基本操作实现
    二叉查找树
    AVL树
    ORM框架疏理——廖雪峰实战系列(一)
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3092107.html
Copyright © 2011-2022 走看看