Hi, I’ve written my code according to the tutorial on the problem and when I’ve run it against the sample and larger numbers ~100 it works. The code isn’t being accepted by Chef, just says WRONG. I’ve checked for OBOB and printing errors but I can’t fathom whats wrong.
Any help would be useful.
Note: some multiplications and underscores seem to have been edited out…it should be multiply_digit, init[index]*next etc
include
include
/*
Helper program to factorial.
Program to print vector of digits of a number
Pre-Condition: number expressed in the vector starts from
least significant digit to most
Post-Condition: console shows expressed number and cursor on
next line
*/
void extractAns(std::vector digits){
for(int i = digits.size()-1; i > 0; --i)
{
printf("%i", digits[i]);
}
printf("%i\n", digits[0]); // Move to next line
}
/*
Helper program to factorial.
Program to multiply a number, expressed as vector of digits
from least significant to most, by a small integer. The vector
increases in size as neccesary and is ammended by the program.
Pre-Condition: number expressed in the vector starts from
least significant digit to most.
*/
void multiply(std::vector &init, int next){
int temp = 0;
int index = 0;
while((index < init.size()) || (temp != 0))
{
int multiply_digit = init[index]*next + temp;
init[index] = multiply_digit % 10;
temp = multiply_digit/10;
if((index == init.size()-1) && (temp != 0))
{
init.push_back(0);
}
++index;
}
}
/*
Program that utilises two helper functions to calculate
the factorial of a small number ~100. This is done by representing
the number as a vector of digits starting from 1 and multiplying all
the numbers up to our input. The answer is printed to the console.
*/
void factorial(int input){
std::vector here({1});
for(int i = 2; i <= input; ++i)
{
multiply(here, i);
}
extractAns(here);
}
int main(){
int no_of_tests;
scanf("%i", &no_of_tests);
int input(0);
for(int i = 0; i < no_of_tests; ++i)
{
scanf("%i", &input);
factorial(input);
}
}
```
