YOJ2226: 算法问题(线性表)

#include <stdio.h>
#include <stdlib.h>
typedef struct sql
{
    int num;
    struct sql *next;
    struct sql *before;
}list;
list* creatList()
{
    list *L = (list*)malloc(sizeof(list));
    L->next = NULL;
    L->before = NULL;
    L->num = -1;
    return L;
}
void insertList(list *L,int num)
{
    list *P = (list*)malloc(sizeof(list));
    P->next = NULL;
    P->num = num;
    list *t = L;
    while(t->next!=NULL)
        t = t->next;
    P->before = t;
    t->next = P;

}
int is(list *L)
{
    int res = 1;
    int i=1;
    while(L!=NULL)
    {
        if(L->num!=-1 && i>=3)
        {
            if(L->num!=(i-1)*(i-1)-L->before->num)
            {
                res = 0;
                break;
            }
        }
        L = L->next;
        i++;
    }
    return res;
}
int main()
{
    list *L = creatList();
    int n;
    scanf("%d",&n);
    int i,f;
    for(i=0;i<n;i++)
    {
        scanf("%d",&f);
        insertList(L,f);
    }
    if(is(L)==1)
        printf("true");
    else
        printf("false");

    return 0;
}

Description

(线性表)已知一个单链表中每个结点存放一个整数,并且结点数不少于2,请设计算法以判断该链表中第二项起的每个元素值是否等于其序号的平方减去其前驱的值,若满足则返回ture,否则返回false.

Input

第一行为输入线性表的节点数N;

第二行为N个整数。

Output

true或者false;

Sample Input

7
2 2 7 9 16 20 29

Sample Output

true
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值