-1
工作中的問題如下: 基本上是一個傢伙是種植橘子和一個種子有隻能X個月後增長2種,一是和後X第二+ y。一旦它長大,他就種植它。我需要計算在N個月內有多少種子會增長,問題出在這個代碼上,我沒有得到所有的答案。我所知道的是,只有少數人才能得到答案,但我不能確定是否有更多的答案,而且我也無法知道哪些答案,而且我也不知道問題出在哪裏。代碼並不適用於所有的可能性
#include<iostream>
long int xKelias(long int N, int x); //x route, counts all oranges assuming the route will be only x till the n from i giving point
long int yKelias(long int N, int y); //same with the y
//doing this just for sake of cleaner code
int main() {
int x, y;
long int N; //months
long long int suma = 0; //total amount of oranges
std::cin >> x >> y >> N;
y = y + x; //simplifying
suma += xKelias(N, x) - 1; // adding the x route
bool yra = true;
while (yra)
{
yra = false;
for (int i = 0; i < xKelias(N, x); i++)
{
if (N - i*x > 0)
{
suma += yKelias(N - i*x, y) - 1;//y route from every other point of x route
yra = true;
}
for (int j = 1; j < yKelias(N, y); j++)
{
if ((N - i*x) - y*j > 0)
{
suma += xKelias((N - i*x) - y*j, x) - 1;// x route from every y route that is from x
yra = true;
}
}
}
N = N - y - x; // lowering N minimum amount and repeating
}
std::cout << suma << std::endl;
return 0;
}
long int xKelias(long int N, int x) {
long int suma = 0;
suma += N/x + 1;
return suma;
}
long int yKelias(long int N, int y) {
long int suma = 0;
suma += N/y + 1;
return suma;
}
使用開發環境隨附的調試軟件逐步執行程序。留意這個程序做你不期望的事情,因爲這可能是一個錯誤。 – user4581301