codeforces 1041a(水题)

http://codeforces.com/problemset/problem/1041/A(题目链接)
There was an electronic store heist last night.

All keyboards which were in the store yesterday were numbered in ascending order from some integer number x For example, if x=4 and there were 3 keyboards in the store, then the devices had indices 4,5,6,if x=10,and there were 7 of them then the keyboards had indices 10,11,12,13,14,15 and 16.。。。。。
测试数据:
input:
4
10 13 12 8
output:
2
input:
5
7 5 6 4 8
output:
0.
做这个题目的时候,总感觉自己想少了,觉得题目没有这么简单,但是提交了一发之后,就过了。啊哈哈,,就是简单的排个序,然后找出相邻的两个数之间的差就好了。代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;

const int maxx=1e3+10;
int a[maxx];
int n;

int main()
{
    while(cin>>n)
    {
        for(int i=0;i<n;i++) cin>>a[i];
        sort(a,a+n);
        ll sum=0;
        for(int i=1;i<n;i++)
        {
            sum+=(a[i]-a[i-1]-1);
        }
        cout<<sum<<endl;
    }
    return 0;
}

每天都加油a题啊!!

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/82732164