Home > Blockchain >  Problem with Pot task and Power of a number in Kattis with C#
Problem with Pot task and Power of a number in Kattis with C#

Time:12-09

I am stuck with the Pot task from Kattis where I should make a program which calculates following:

X = number1^pow1 number2^pow2 … numberN^powN

X = P1 P2 .... PN

For example my program should convert X = 212 1253 to X = 21^2 125^3.

This is Kattis sample input and output:

Sample Input: 2, 212, 1253

Sample Output: 1953566

But my code prints out the wrong answer, it prints out 1954378.

Here is my code:

using System;

namespace Pot
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int total = 0;
            Console.WriteLine(n);

            if (n < 1 || n > 10)
            {
                Console.WriteLine("Wrong amount of lines. Choose a number between 1 and 10");
                n = int.Parse(Console.ReadLine());
            }
            else
            {
                int p = int.Parse(Console.ReadLine());
                int x = (int)Math.Pow(p / 10, p % 10);

                for (int i = 1; i < n; i  )
                {
                    p = int.Parse(Console.ReadLine());
                    x = (int)Math.Pow(p / 10, p % 10);
                    total = x   p;
                }
                Console.WriteLine(total);
            }
        }
    }
}

All help is appreciated. Thanks in advance!

CodePudding user response:

As Already explained in my comment the issue is of value assignment.

using System;

namespace Pot
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int total = 0;
            Console.WriteLine(n);

            if (n < 1 || n > 10)
            {
                Console.WriteLine("Wrong amount of lines. Choose a number between 1 and 10");
                n = int.Parse(Console.ReadLine());
            }
            else
            {
                int p = int.Parse(Console.ReadLine());
                total = (int)Math.Pow(p / 10, p % 10);

                for (int i = 1; i < n; i  )
                {
                    p = int.Parse(Console.ReadLine());
                    int x = (int)Math.Pow(p / 10, p % 10);
                    total = x   total;
                }
                Console.WriteLine(total);
            }
        }
    }
}

This code gets it to work, if you want to squeeze a little more efficiency you can declare x at the beginning of main, as for long inputs that memory would be reallocated again and again, but I leave that upto you.

  • Related