CCF 2017121

问题描述

试题编号:201712-1
试题名称:最小差值
时间限制:1.0s
内存限制:256.0MB
问题描述:

问题描述

  给定n个数,请找出其中相差(差的绝对值)最小的两个数,输出它们的差值的绝对值。

输入格式

  输入第一行包含一个整数n
  第二行包含n个正整数,相邻整数之间使用一个空格分隔。

输出格式

  输出一个整数,表示答案。

样例输入

5
1 5 4 8 20

样例输出

1

样例说明

  相差最小的两个数是5和4,它们之间的差值是1。

样例输入

5
9 3 6 1 3

样例输出

0

样例说明

  有两个相同的数3,它们之间的差值是0.

数据规模和约定

  对于所有评测用例,2 ≤ n ≤ 1000,每个给定的整数都是不超过10000的正整数。

 

解答:

#include <cstdio>
#include <algorithm>
using namespace std;
int subs(int a,int b){
    int temp=0;
    temp=abs(temp=a-b);
    return temp;
}

int main()
{
    int tot=0,res=10000;
    int num[1010];
    scanf("%d",&tot);
    for (int i=0;i<tot;i++){
            scanf("%d",&num[i]);
           // printf("%d",num[i]);
    }

    for (int m=0;m<tot;m++){
        for (int n=m+1;n<tot;n++){
            int tempres=1000;
            tempres=subs(num[m],num[n]);
            //printf("subs :%d res :%d\n",tempres,res);
            if (tempres<res){res=tempres;}
        }
        if (res==0) break;
    }
    printf("%d",res);
    return 0;
}

得分100.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值