WOJ1020-Adjacent Difference

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

1
5
1 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];
			else
			out[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");
	}
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值