[Description]: Make a difference between two adjacent numbers in the sequence, and determine whether the absolute value is 1, 2 ,......, N-1. If yes, It is jolly; otherwise, not jolly.
[Analysis]: At the beginning, I did not look at the question carefully. When I did not see the two adjacent numbers, I thought any two were doing the difference.
After re-analyzing the problem, we solved the problem. We can use a flag array to help us store the absolute value of the difference. Finally, we only need to scan this array to see if it is from 1, 2 ,......, N-1 values correspond to each other.
For details, see the code:
// 324 K 47 Ms # include <iostream> # include <cmath> using namespace STD; int flag [30001]; int main () {int N; int A, B; int TMP; while (CIN> N) {memset (flag, 0, sizeof (FLAG); CIN> A; For (INT I = 1; I <N; I ++) {CIN> B; TMP = ABS (B-A); flag [TMP] = 1; A = B;} Int J; For (j = 1; j <n; j ++) {If (flag [J] = 0) break;} If (j = N) cout <"Jolly" <Endl; else cout <"not jolly" <Endl;} return 0 ;}
Poj 2575 jolly jumpers (simple question)