日期:2014-05-20 浏览次数:20779 次
using System; class Program { static void Main() { foreach (int n in new int[] { 0, 1, 2, 3, 4, 1000, 1001, -1, -2, -3, -4, -1000, -1001, int.MaxValue - 1, int.MinValue+1, int.MaxValue, int.MinValue }) { Console.Write("n = {0,-12} f(n) = {1,-12} f(f(n)) = {2,-12} ", n, f(n), f(f(n))); Console.Write((long)n == -(long)(f(f(n))) ? "OK" : "ERROR"); Console.WriteLine(); } } static long f(long n) { if ((n & 1) == 0) return -n + Math.Sign(n); return n + Math.Sign(n); } } /* 程序输出: n = 0 f(n) = 0 f(f(n)) = 0 OK n = 1 f(n) = 2 f(f(n)) = -1 OK n = 2 f(n) = -1 f(f(n)) = -2 OK n = 3 f(n) = 4 f(f(n)) = -3 OK n = 4 f(n) = -3 f(f(n)) = -4 OK n = 1000 f(n) = -999 f(f(n)) = -1000 OK n = 1001 f(n) = 1002 f(f(n)) = -1001 OK n = -1 f(n) = -2 f(f(n)) = 1 OK n = -2 f(n) = 1 f(f(n)) = 2 OK n = -3 f(n) = -4 f(f(n)) = 3 OK n = -4 f(n) = 3 f(f(n)) = 4 OK n = -1000 f(n) = 999 f(f(n)) = 1000 OK n = -1001 f(n) = -1002 f(f(n)) = 1001 OK n = 2147483646 f(n) = -2147483645 f(f(n)) = -2147483646 OK n = -2147483647 f(n) = -2147483648 f(f(n)) = 2147483647 OK n = 2147483647 f(n) = 2147483648 f(f(n)) = -2147483647 OK n = -2147483648 f(n) = 2147483647 f(f(n)) = 2147483648 OK */
------解决方案--------------------
如果函数原形要求是:int f(int n),也就是自变量和返回值都是int,
则有两个值是错的:int.MinValue 和 int.MaxValue,
不知能不能减少到一个值。
using System; class Program { static void Main() { foreach (int n in new int[] { 0, 1, 2, 3, 4, 1000, 1001, -1, -2, -3, -4, -1000, -1001, int.MaxValue - 1, int.MinValue+1, int.MaxValue, int.MinValue }) { Console.Write("n = {0,-12} f(n) = {1,-12} f(f(n)) = {2,-12} ", n, f(n), f(f(n))); Console.Write((long)n == -(long)(f(f(n))) ? "OK" : "ERROR"); Console.WriteLine(); } } static int f(int n) { if ((n & 1) == 0) return -n + Math.Sign(n); return n + Math.Sign(n); } } /* 程序输出: n = 0 f(n) = 0 f(f(n)) = 0 OK n = 1 f(n) = 2 f(f(n)) = -1 OK n = 2 f(n) = -1 f(f(n)) = -2 OK n = 3 f(n) = 4 f(f(n)) = -3 OK n = 4 f(n) = -3 f(f(n)) = -4 OK n = 1000 f(n) = -999 f(f(n)) = -1000 OK n = 1001 f(n) = 1002 f(f(n)) = -1001 OK n = -1 f(n) = -2 f(f(n)) = 1 OK n = -2 f(n) = 1 f(f(n)) = 2 OK n = -3 f(n) = -4 f(f(n)) = 3 OK n = -4 f(n) = 3 f(f(n)) = 4 OK n = -1000 f(n) = 999 f(f(n)) = 1000 OK n = -1001 f(n) = -1002 f(f(n)) = 1001 OK n = 2147483646 f(n) = -2147483645 f(f(n)) = -2147483646 OK n = -2147483647 f(n) = -2147483648 f(f(n)) = 2147483647 OK n = 2147483647 f(n) = -2147483648 f(f(n)) = 2147483647 ERROR n = -2147483648 f(n) = 2147483647 f(f(n)) = -2147483648 ERROR */