在我的应用程序中,“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