10038 - Jolly Jumpers

题目

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
#include

#define N 3000
bool 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;iscanf("%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;iif(!diff[i]){
                jolly=false;
                break;
            }


        //print results
        if(jolly)
            printf("Jolly\n");
        else
            printf("Not jolly\n");
    }

    return 0;
}


运行截图


这里写图片描述

你可能感兴趣的:(算法,uva,C)