PAT 1083 whether there is an equal difference (20 minutes)

Given N cards, are written on the front 1,2, ......, N, and all upside down, shuffle, are written on the rear surface 1,2, ......, N. The both sides of each card digital subtraction (large decrease), to give the N non-negative difference, wherein the difference of whether there is an equal?

Input formats:

Input of the first row is given a positive integer N (2  ≤ N  ≤ 10 000), followed by the arrangement of a given row shuffle 1 to N, i-th write a front piece of a rear surface of the digital card i .

Output formats:

According to "the difference between the number of repetitions" format and a descending output difference repeat number of repetitions, it outputs a result of each line.

Problem-solving ideas:

It takes time to understand the topic, then understand not believe would be so simple. . . But some did not bend for the first time noticed, for example: the big difference is reduced, non-negative; the difference occurs when the number 1, not when repeated.

 1 #include<stdio.h>
 2 #include<math.h>
 3 
 4 int a[10000+5]={0};
 5 int main(){
 6     int n,t,f;
 7     
 8     scanf("%d",&n);
 9     for(int i=1;i<=n;i++){
10         scanf("%d",&t);
11         f=abs(t-i);
12         a[f]++;
13     }
14     for(int i=10000;i>=0;i--)
15     if(a[i]>1) 
16         printf("%d %d\n",i,a[i]);
17     return 0;
18 }

Guess you like

Origin www.cnblogs.com/cuphead/p/12123853.html