zoukankan      html  css  js  c++  java
  • 一个小的日常实践——距离阵列

    上得厅堂,下得厨房,写得代码,翻得围墙,欢迎来到睿不可挡的每日一小练!


    题目:数组的距离


    内容:已知两个元素从小到大排列数组x[]和y[],请编写一个程序算出两个数组元素彼此之间差的绝度值中最小的一个

    数,此值称作数组的距离。

    比如:x[]有1,3,5,7,9   y[]有2,6,8  那么最短距离就是1。由于x[0]和y[0]、x[1]和y[0]、x[2]和y[1]、x[3]和y[1]还有x[4]和

    y[2]的距离都是1.


    我的解法:上来没多想,打开vs2013就敲了起来,问题果然非常easy。分分钟就超神。。奥。不正确就攻克了!然后发现这个题目有点像某oj上面的题目,只是如今看来还是挺简单滴。。

    嘿嘿。。我又进步了O(∩_∩)O~。


    #include <iostream>
    #include <math.h>
    #define min(x,y) (x < y ? x : y)
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int sLength(int x[], int y[], int x_Length, int y_Length);
    	int x[5] = { 1, 14, 31, 50, 70 };
    	int y[5] = { 7, 23, 40, 60, 75 };
    	int sL = sLength(x, y, 5, 5);
    	cout << "两个数组的最短距离为: " <<  sL  << endl;
    	getchar();
    	return 0;
    }
    
    int sLength(int x[], int y[], int x_Length, int y_Length)
    {
    	int shortLength = TMP_MAX;
    	int x_Index = 0;
    	int y_Index = 0;
    	while (x_Index < x_Length && y_Index < y_Length)
    	{
    		if (x[x_Index] >= y[y_Index])
    		{
    			shortLength = min(shortLength, (x[x_Index] - y[y_Index]));
    			y_Index++;
    		}
    		else
    		{
    			shortLength = min(shortLength, (y[y_Index] - x[x_Index]));
    			x_Index++;
    		}
    	}
    	return shortLength;
    }

    实验结果是:



    欢迎大家增加每日一小练。嘿嘿。

    每天练一练,日久见功夫。加油。


                -End-

    参考:《c语言名题精选百》


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    当算法提升到哲学层面—小议验证码识别
    2014总结
    [脚本无敌2]python获取cocos 2dx项目文件列表
    单幅图构建三维图
    [思考]画个圈圈诅咒你
    Mybatis2
    Mybatis1
    淘淘商城虚拟机启动命令
    Zookeeper集群搭建zookeeper01启动不成功解决方案
    Mybatis的xml文件的相关配置
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4821593.html
Copyright © 2011-2022 走看看