10038 - Jolly Jumpers

来源:互联网 发布:淘宝咸鱼网手机版 编辑:程序博客网 时间:2024/05/20 15:39

题目

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=30&problem=979&mosmsg=Submission+received+with+ID+17410928

解题思路


1. 模拟题
2. 用hash判断diff(1~n-1)
3. 注意diff可能超过n-1,所以要避免对hash表的越界访问
4. 每行都要输出一个换行符


通过代码

#include<stdio.h>#include<string.h>#define N 3000bool diff[N];int num[N];int main(){#ifdef DEBUG    freopen("in","r",stdin);    freopen("out","w",stdout);#endif    int n;    int tmp;    bool jolly;    while(scanf("%d",&n)!=EOF){        //init        jolly=true;        memset(diff,0,sizeof(diff));        //input        scanf("%d",&num[0]);        for(int i=1;i<n;++i){            scanf("%d",&num[i]);            tmp=num[i]-num[i-1];            if(tmp<0)                tmp=tmp*(-1);            if(tmp<=n-1)                diff[tmp]=true;        }        //check        for(int i=1;i<n;++i)            if(!diff[i]){                jolly=false;                break;            }        //print results        if(jolly)            printf("Jolly\n");        else            printf("Not jolly\n");    }    return 0;}


运行截图


这里写图片描述

0 0
原创粉丝点击