星辰.Net技术社区论坛

首页 » .NET » 算法/数据结构 » 要求根据给定的离散函数在直角坐标系中的对应点的连线中找出倾角最大的
admin - 2008-7-4 15:32:00
InputThere is an N in the first line. Than N lines follow with the values of the function for the arguments 1, 2, …, N respectively.}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
OutputA pair of integers, which are abscissas of the desired points, should be written into one line of output. The first number must be less then the second one. If it is any ambiguity your program should write the pair with the smallest first number.}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
Sample
inputoutput
3}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
2}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
6}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
4}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
1 2}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
Problem Source: Third Open USTU Collegiate Programming Contest (PhysTech Cup), March 18, 2000 答案如下:}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
1 using System;}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
2 }GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
3 namespace Skyiv.Ben.Timus}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
4 {}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
5  // http://acm.timus.ru/problem.aspx?space=1&num=1010}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
6  sealed class T1010}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
7   {}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
8    static void Main()}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
9     {}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
10      int i0 = 0, n = int.Parse(Console.ReadLine());}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
11      long r0 = 0, v1 = int.Parse(Console.ReadLine());}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
12      for (int i = 0; i < n - 1; i++)}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
13       {}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
14        long v2 = int.Parse(Console.ReadLine());}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
15        long r = Math.Abs(v1 - v2);}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
16         v1 = v2;}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
17        if (r0 < r)}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
18         {}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
19           r0 = r;}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
20           i0 = i;}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
21         }}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
22       }}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
23       Console.WriteLine("{0} {1}", i0 + 1, i0 + 2);}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
24     }}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
25   }}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
26 }}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
这题实际上是很简单的,只要意识到所求倾角最大的连线的两个端点必定是相邻的点就容易了。}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
两点连线的倾角的正切值(tan)等于这两点的纵坐标之差除以横坐标之差。由于这两点是相邻的点,所以横坐标之差为 1,也就是说,直接等于纵坐标之差。}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
题目中说的:“Each value of the function is longint (signed long in C++).”,即:该离散函数的值域是 C++ 中的 signed long 数据类型,实际上相当于 C# 中的 int ( 即: System.Int32 ) 数据类型,取值范围是 -2,147,483,648 到 +2,147,483,647。所以可以用 int.Parse() 方法来读取输入。但是进行计算时必须使用 C# 的 long ( 即: System.Int64 ) 数据类型,相当于 C++ 中的 signed long long 数据类型,取值范围是 -9,223,372,036,854,775,808 到 +9,223,372,036,854,775,807,不然计算时就有可能溢出。}GéQaOŠï3(www.netcsharp.cnÀÀ¬$ ¨µ4p
1
查看完整版本: 要求根据给定的离散函数在直角坐标系中的对应点的连线中找出倾角最大的