Please Note: While my class has assigned Kattis problems before, I am completing this program on Kattis for my own practice, not for an assignment.
I am doing this problem: https://open.kattis.com/problems/pot
and have successfully gotten the correct output for all of the example inputs/outputs on the Kattis page. However, when I try submitting it to Kattis it says my answer is wrong. What am I doing wrong? I've looked it over thoroughly but still can't find what I'm doing wrong.
/*
Program Name: pot_v2.cpp
Version: 2
Author: PiggiesGoSqueal
Kattis Link: https://open.kattis.com/problems/pot
Algorithm Steps:
1. Ask for n (the number of numbers that will be input).
2. Ask for first number and assign it to a string variable (userInput).
- Will take last digit in number and assign it to a variable
called "powerStr" then will delete it from userInput.
- Will convert powerStr to power (int var)
- Will convert userInput to an integer variable (userInputInt)
- Will add (userInputInt ^ power) to total variable
3. Repeats step 2 for all numbers.
4. Prints total.
Status: Completed
Difference between V1 and V2:
- Code Cleanup
*/
#include <iostream>
#include <math.h>
#include <string>
int main() {
int n;
std::string userInput, powerStr;
int userInputInt, power, total;
// Asks for how many numbers user will input:
std::cin >> n;
// for as many numbers the user will input:
for (int i = 0; i < n; i++) {
std::cin >> userInput;
// assigns the last char in userInput to lastChar variable
powerStr = userInput.at(userInput.size()-1);
// converts powerStr to an int variable named "power"
power = stoi(powerStr);
// removes last char from string.
userInput.pop_back();
// Adds userInputInt^power to total.
userInputInt = stoi(userInput);
total += pow(userInputInt, power);
}
total -= 1; // subtracts one to get answer
// I have tried both of the following outputs but
// Kattis considers them both to be incorrect:
std::cout << total;
//std::cout << total << '\n';
return 0;
}
@dhayden I subtract one because I noticed when I input my answers, it was always 1 above the correct answer (based on Kattis output examples). So by subtracting one, it made it the correct answer for all 3 sample problems provided by Kattis.