Witam.
Ostatnio zabrałem się za rozwiązywanie zadań ze SPOJ'a.
Przy jednym z pierwszych zadań napotkałem błąd, którego nie jestem w stanie obejść - mimo prawidłowych wyników wyświetla się komunikat o nieprawidłowej odpowiedzi.
http://www.spoj.com/problems/FCTRL2/
Kod do podglądu:
#include <iostream>
#include <algorithm>
#include <string>
#include <array>
#include <vector>
class TextNumber
{
std::string value;
void Add(const std::string& valueToAdd)
{
int rest = 0;
for (size_t valueToAddIndex = 0; valueToAddIndex < valueToAdd.size(); ++valueToAddIndex)
{
if (value.size() > valueToAddIndex)
{
int result = (valueToAdd[valueToAddIndex] - '0') + (value[valueToAddIndex] - '0') + rest;
if (result >= 10)
{
rest = 1;
value[valueToAddIndex] = result - 10 + '0';
}
else
{
rest = 0;
value[valueToAddIndex] = result + '0';
}
}
else
{
value += valueToAdd[valueToAddIndex] + rest;
rest = 0;
}
}
if (rest != 0)
value += rest + '0';
}
public:
TextNumber()
{
}
TextNumber(int value)
{
this->value = std::to_string(value);
std::reverse(this->value.begin(), this->value.end());
}
void Multiply(int modifier)
{
std::vector<std::string> sums;
std::string textModifier = std::to_string(modifier);
std::string baseSum = "";
for (size_t modifierIndex = 0; modifierIndex < textModifier.size(); ++modifierIndex)
{
std::string sum = baseSum;
int rest = 0;
for (size_t valueIndex = 0; valueIndex < value.size(); ++valueIndex)
{
int result = (textModifier[textModifier.size() - modifierIndex - 1] - '0') * (value[valueIndex] - '0') + rest;
if (result >= 10)
{
rest = result / 10;
result -= rest * 10;
}
else
{
rest = 0;
}
sum += result + '0';
int x = 154;
}
if (rest != 0)
sum += rest + '0';
sums.push_back(sum);
baseSum += "0";
}
value = sums[0];
for (size_t index = 1; index < sums.size(); ++index)
Add(sums[index]);
}
std::string ToString()
{
auto result = value;
std::reverse(result.begin(), result.end());
return result;
}
};
int main()
{
int testAmount;
std::cin >> testAmount;
std::vector<TextNumber> factorials;
factorials.push_back(0);
factorials.push_back(1);
for (int index = 0; index < testAmount; ++index)
{
size_t number;
std::cin >> number;
for (size_t index = factorials.size(); index <= number; ++index)
{
factorials.push_back(factorials[index - 1]);
factorials[index].Multiply(index);
}
std::cout << factorials[number].ToString() << '\n';
}
return 0;
}
Kod wysłany do zadania (limit 2KB):
#include <iostream>
#include <algorithm>
#include <string>
#include <array>
#include <vector>
class X
{
std::string x;
void Add(const std::string& y)
{
int z = 0;
for (size_t a = 0; a < y.size(); ++a)
{
if (x.size() > a)
{
int f = (y[a] - '0') + (x[a] - '0') + z;
if (f >= 10)
{
z = 1;
x[a] = f - 10 + '0';
}
else
{
z = 0;
x[a] = f + '0';
}
}
else
{
x += y[a] + z;
z = 0;
}
}
if (z != 0)
x += z + '0';
}
public:
X() { }
X(int x)
{
this->x = std::to_string(x);
std::reverse(this->x.begin(), this->x.end());
}
void Multiply(int b)
{
std::vector<std::string> c;
std::string textModifier = std::to_string(b);
std::string baseSum = "";
for (size_t modifierIndex = 0; modifierIndex < textModifier.size(); ++modifierIndex)
{
std::string sum = baseSum;
int z = 0;
for (size_t valueIndex = 0; valueIndex < x.size(); ++valueIndex)
{
int f = (textModifier[textModifier.size() - modifierIndex - 1] - '0') * (x[valueIndex] - '0') + z;
if (f >= 10)
{
z = f / 10;
f -= z * 10;
}
else
{
z = 0;
}
sum += f + '0';
}
if (z != 0)
sum += z + '0';
c.push_back(sum);
baseSum += "0";
}
x = c[0];
for (size_t d = 1; d < c.size(); ++d)
Add(c[d]);
}
std::string ToString()
{
auto f = x;
std::reverse(f.begin(), f.end());
return f;
}
};
int main()
{
int testAmount;
std::cin >> testAmount;
std::vector<X> factorials;
factorials.push_back(0);
factorials.push_back(1);
for (int d = 0; d < testAmount; ++d)
{
size_t number;
std::cin >> number;
for (size_t e = factorials.size(); e <= number; ++e)
{
factorials.push_back(factorials[e - 1]);
factorials[e].Multiply(e);
}
std::cout << factorials[number].ToString() << '\n';
}
return 0;
}
Zarówno dla 0! = 0, jak i 0! = 1 rozwiązanie jest uznawane za nieprawidłowe.
Oczywiście zdaję sobie sprawę z tego jak bardzo jest to nieefektywny kod (z zakresu 256 liczb korzysta tylko z 10), jednak pod względem poprawności sądzę że jest wszystko prawidłowe (dla n = 100 jest wyświetlany prawidłowy wynik).
Kamil91321000
możesz potraktować jako1
ale0001
to1000
nie1
Kamil9132TextNumber
jak nie klasą dużych liczb całkowitych? Jedyne czego nie ma to przeciążonych operatorów, jednak wywołanie funkcji w pełni mi wystarcza.