在我的應用程序中,「cool」數字是既是正方形又是立方體的數字,例如:64 = 8^2和64 = 4^3。我的應用程序應該在用戶給出的範圍內找到「很酷的數字」的數量。我寫了我的代碼,應用程序運行正常,但它給了我錯誤的答案。你能幫我一下嗎?例如:C#:找到「很酷」數字時的錯誤答案
INPUT
OUTPUT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double a = Convert.ToDouble(Console.ReadLine()); // first number in the range
double b = Convert.ToDouble(Console.ReadLine()); // second number in the range
long x = 0;
for (double i = a; i <= b; i++)
{
double cube = 1.0/3.0;
double cuad = 1.0/2.0;
double crt = Math.Pow(i, cube); // cube root
double sqrt = Math.Pow(i, cuad); // square root
if ((crt * 10) % 10 == 0 || (sqrt * 10) % 10 == 0) // condition to determine if it is a cool number.
x++;
}
Console.WriteLine(x);
Console.ReadLine();
}
}
}
這個例子說明了它的意義是什麼,而不是我得到的錯誤答案。 – user300484 2010-04-03 02:17:13
有沒有機會做這個功課? – 2010-04-03 02:17:55
調試它並自行執行代碼,您會立即看到錯誤。 – 2010-04-03 02:18:14