-4
我寫了代碼,它應該告訴P的最小值是什麼,它會使整個sqrt成爲一個自然數。 問題在於,每當它到達P = K的部分時,循環停止並表示這是相同的值。 例如:4號它完美,但對於5它說,他們是平等的,但在45開方是不是自然數在C中尋找一個自然數#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
int k, p = 0;
double final;
Console.WriteLine("Please enter the value of K ");
k = int.Parse(Console.ReadLine());
final = System.Math.Sqrt(4 * k + System.Math.Pow(p, 2));
while (final - (int)(final) != 0&&p!=k)
{
final = System.Math.Sqrt(4 * k + System.Math.Pow(p, 2));
p++;
}
Console.WriteLine("k is {0}, and P is {1}", k, p);
}
}
}
代碼,以什麼語言寫不會做你認爲它*有*做,直到你*讓*它做。 –
代碼是由您編寫的,如果它不起作用... – dcg
更像是第8層上的錯誤 – Fabiano