11956 – Brainfuck UVA Solution

This is an easy problem from uva judge https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3107

Brute force, parse int to hexadecimal.

How I did it

  • I read the number of cases.
  • I read the instruction, clean the output vector and set an index to 0;
  • I iterate the instruction and follow the the instructions to “<>.+-“.
  • I print the answer.
  • Accepted!!

Hints

  • Keep in mind this sentence from the problem: “display has an array of 100 bytes of circular memory (initialized with zeros) and a pointer to this
    array (initialized to point to the leftmost byte of the array).”.
  • Keep in mind: “Individual bytes are circular as well, so increasing a 255 gives a 0 and vice versa.”.

Critical Input

1

..++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++…

Critical Output

Case 1: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Code

/*
*Author: Fabian Calsina
*Date: 1/8/2016
*/
#include <bits/stdc++.h>
using namespace std;

vector<int>output(110);

int main(){
    int index;
    int cases;
    string instruction;
    char option;
    scanf("%d", &cases);

    for(int x=1; x<=cases; x++){
        cin>>instruction;
        fill(output.begin(), output.end(), 0);
        index = 0;

        for(int y=0; y<instruction.size(); y++){
            option = instruction[y];

            if(option == '>'){
                index++;

                if(index >= 100)
                    index = 0;
            }
            else if(option == '<'){
                index--;

                if(index <=-1)
                    index = 99;
            }
            else if(option == '+'){
                output[index]++;

                if(output[index] > 255)
                    output[index] = 0;
            }

            else if(option == '-'){
                output[index]--;
                if(output[index] < 0)
                    output[index] = 255;
            }
            else if(option == '.')
                continue;
        }

        printf("Case %d: ", x);

        for(int z=0; z<100; z++){
            if(z<99)
                printf("%02X ", output[z]);
            else
                printf("%02X\n", output[z]);
        }
    }
return 0;
}

12555 – Baby Me UVA Solution

This is an easy problem from uva judge http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=4000

A really easy problem. Input parsing, output formatting.

How I did it

  • I read the number of cases .
  • Read the string with the current case.
  • Get the first and second number.
  • Get the result (num1 * 0.5) + (num2 * 0.05).
  • Print the result with cout.

Hints

  • You need to print the most accurate result posible.
  • Cout rounds the double numbers instead of printf(). I love printf(), but in this case cout is more useful.

Code

#include <iostream>
#include <cstdio>
#include <cctype>
#include <cmath>
using namespace std;

int parseInt(string str)
{
    int res;
    for(int z=0; z<str.size(); z++)
    {
        if(z == 0)
           res = str[z] - '0';
        else
            res = (res * 10) + (str[z] - '0');
    }
return res;
}

int main()
{
string str, n1, n2;
int casos, num1, num2;
bool flag;

scanf("%d", &casos);
for(int l=1; l<=casos; l++)
{
    flag = false;
    n1 = n2 = "";
    cin>>str;
    for(int z=0; z<str.size(); z++)
    {
        if(isdigit(str[z]) && !flag)
           n1 += str[z];
        else if(!isdigit(str[z]))
            flag = true;
        else
            n2 += str[z];
    }

    num1 = parseInt(n1);
    if(n2.size() == 0)
       num2 = 0;
    else
        num2 = parseInt(n2);

    double result = (num1 * 0.5) + (num2 * 0.05);
    cout<<"Case "<<l<<": "<<result<<"\n";
}
return 0;
}