如何找到数组中两个数字的差? [英] How to find the difference of two numbers in arrays ?

查看:122
本文介绍了如何找到数组中两个数字的差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数类型的数组,我想找到其中两个值之间的差.

例如:

如果score [0] = -10,并且score [1] = 15

然后

分数[0]和分数[1]之差应以"25"作为答案.

并且score [1]和score [0]之间的差异也应给出相同的答案.

这有点像x-y图中的计算.

我只是一个业余爱好者,我不知道C#.NET中是否存在可以轻松解决此问题的预定义方法.

任何帮助将非常感激.谢谢

I have an arrays of integer type and i would like to find the difference between two values in it.

For eg:

if score[0] = -10, and score[1] = 15

Then

The difference between score[0] and score[1] should give "25" as an answer.

and The difference between score[1] and score[0] should give the same answer too.

This is somewhat like the calculation in a x-y graph.

I am just an amateur and i don''t know whether there exists any pre-defined methods in C#.NET that can solve this problem easily.

Any help would be much appreciated. Thanks

推荐答案

您正在寻找Math.Abs​​函数.这将使您在两个数组元素之间的绝对术语有所不同.

HTH
You are looking for the Math.Abs function. This will give you the difference in absolute terms between the two array elements.

HTH


以下代码行将产生相同的结果:

The following lines of code will give the same result:

int diff = Math.Abs(score[0] - score[1]);
int diff2 = Math.Abs(score[1] - score[0]);



诚实的看法-您在问太多基本问题.了解如何使用Google并节省一些时间.



Honest opinion - you''re asking too many basic questions. Learn how to use google and save yourself some time.


int diff = (score[0] > score[1]) ? (score[0] - score[1]) : (score[1] - score[0]);


这篇关于如何找到数组中两个数字的差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆