Files
PAT/PATBasic/1083.c
T
2018-03-25 19:18:48 +08:00

48 lines
1.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 1083. 是否存在相等的差 (20)
*
* 给定 N 张卡片,正面分别写上 1、2、……、N,然后全部翻面,洗牌,在背面分别写上
* 1、2、……、N。将每张牌的正反两面数字相减(大减小),得到 N 个非负差值,其中是
* 否存在相等的差?
*
* 输入格式:
*
* 输入第一行给出一个正整数 N(2 <= N <= 10000),随后一行给出 1 到 N 的一个洗牌
* 后的排列,第 i 个数表示正面写了 i 的那张卡片背面的数字。
*
* 输出格式:
*
* 按照“差值 重复次数”的格式从大到小输出重复的差值及其重复的次数,每行输出一个
* 结果。
* 输入样例:
*
* 8
* 3 5 8 6 2 1 4 7
*
* 输出样例:
*
* 5 2
* 3 3
* 2 2
**/
#include <stdio.h>
int main()
{
int N, num, diff[10000] = {0};
scanf("%d", &N);
for(int i = 0; i < N; i++)
{ /* The front is i + 1, the back is num */
scanf("%d", &num);
diff[(num > i + 1) ? (num - i - 1) : (i + 1 - num)]++;
}
for(int i = N - 1; i >= 0; i--)
if(diff[i] >= 2)
printf("%d %d\n", i, diff[i]);
return 0;
}