WOJ1020-Adjacent Difference

来源:互联网 发布:windows syslog服务器 编辑:程序博客网 时间:2024/05/16 14:01

Description

An adjacent difference of a sequence is a new sequence formed by replacing every element with the difference between the element and


the immediately preceding element. The first value in the new sequence remains unchanged. For example, a sequence such as (1, 3, 2, 4, 5)
is transformed into (1, 3-1,2-3, 4-2, 5-4), and in this manner becomes the sequence (1, 2, -1, 2, 1). Then, we want to sort the adjacent
difference of the sequence in non-decreasing order. It?s an easy job for you, isn?t it? So, please solve it quickly.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T(1 <= T <= 50) which is the number of

test cases.
For each test case, the first line contains an integer N(1 <= N <= 1000), representing the size of the sequence. The second line contains N integers (you are ensured that the absolute value of each integer is less than ), representing the elements of this sequence.

Output

Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from

  1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.
    For each test case, output one line containing n elements representing the sorted adjacent difference of the sequence. Elements are separated by one blank space.

Sample Input

151 3 2 4 5

Sample Output

Case 1:-1 1 1 2 2

#include<stdio.h>#include<stdlib.h>int in[1005],out[1005];int comp(const void*a,const void*b){return *(int*)a-*(int*)b;}int main(){int t,n,i,j;scanf("%d",&t);for(i=0;i<t;i++){scanf("%d",&n);for(j=0;j<n;j++){scanf("%d",&in[j]);}for(j=0;j<n;j++){if(j==0)out[0]=in[0];elseout[j]=in[j]-in[j-1];}qsort(out,n,sizeof(int),comp);printf("Case %d:\n",i+1);for(j=0;j<n;j++){printf("%d",out[j]);if(j!=n-1)printf(" "); }if(i!=t-1)printf("\n\n");}}