helfen Sie bitte dieses grundlegende Problem zu lösen. Ich habe unten eine Frage erhalten.Arbeiten mit Funktion in C++
berechnen Modul von A und B ganze Zahlen zehntausend Millionen sieben mit der Funktion F (A, B, N), wie nachstehend unter Verwendung von C erläutert ++ mit 0 < = N < = 1000000000.
Modulus of F[A, B, 0] is equal to A%1,000,000,007
Modulus of F[A, B, 1] is equal to B%1,000,000,007
Modulus of F[A, B, N] is equal to modulus of F[A, B, N - 1] + F[A, B, N - 2]
Example, Modulus of F[3, 4, 5] is equal to 29. In detail:
Modulus of F[3, 4, 0] is equal to 3%1,000,000,007 which is 3
Modulus of F[3, 4, 1] is equal to 4%1,000,000,007 which is 4
Modulus of F[3, 4, 2] is equal to modulus of F[3, 4, 1] + F[A, B, 0] which is 3 + 4 = 7
Modulus of F[3, 4, 3] is equal to modulus of F[3, 4, 2] + F[A, B, 1] which is 7 + 4 = 11
Modulus of F[3, 4, 4] is equal to modulus of F[3, 4, 3] + F[A, B, 2] which is 11 + 7 = 18
Modulus of F[3, 4, 5] is equal to modulus of F[3, 4, 4] + F[A, B, 3] which is 18 + 11 = 29
I verwendet Xcode und Mac OS X und hier ist mein Code.
#include <iostream>
#include <vector>
int solution(int A, int B, int N)
{
std::vector<int> remainderVector;
if (N < 0 || N > 1000000000)
{
std::cout << "N is out of range." << std::endl;
}
if (N == 0)
{
remainderVector[N] = A%1000000007;
}
if (N == 1)
{
remainderVector[N] = B%1000000007;
}
if (N > 1 && N <= 1000000000)
{
remainderVector[0] = A%1000000007;
remainderVector[1] = B%1000000007;
for (int i = 2; i <= N; i++)
{
remainderVector[i] = remainderVector[i - 1] + remainderVector[i - 2];
}
}
std::cout << "Remainder is: " << remainderVector[N] << std::endl;
return 0;
}
int main()
{
std::cout << "Input A interger: " << std::endl;
int x;
std::cin >> x;
std::cout << "Input B interger: " << std::endl;
int y;
std::cin >> y;
std::cout << "Input Z interger: " << std::endl;
int z;
std::cin >> z;
int solution(int x, int y, int z);
std::cout << "This is a debug message." << std::endl;
return 0;
}
Wenn ich diesen Code ausführen, erschien nur Nachricht: Dies ist eine Debug-Nachricht. Warum habe ich Nummer 29 nicht ausgedruckt, wenn ich ein Beispiel von 3, 4, 5 wie oben beschrieben eingegeben habe.
Vielen Dank für Ihre Hilfe.
Haben Sie Ihr Programm debuggen? –
Ihr Aufruf an die "Lösung" ist eine Prototypdeklaration, kein Funktionsaufruf. Versuchen Sie es mit "Lösung (x, y, z);" Ich bin überrascht, dass das schwierig ist. – Tezirg
Danke, ich änderte zu Lösung (x, y, z) und es funktioniert, wenn ich auch Größe des Vektors zu N + 1 erkläre. Vielen Dank. –