11683 – Laser Sculpture 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=2730

Ad hoc, linear scan.

How I did it

  • Read the number of rows and columns. If row == 0 and columns = 0 stop the program.
  • Read the final heigth and insert this values inside a vector.
  • Traverse the vector. If is the first time save the diference between the row and current vector position(set current equal to vector current position), otherwise if the current vector position  is less than current variable save the diference between current and the current vector position.
  • Print the total.

Hints

  •  Simulate the process will finish in TLE.
  • To solve thism problem a linear scan is enough.

Code

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int main()
{
int row, col, val, current, tot;

while(scanf("%d %d", &row, &col))
{
    if(!row && !col)
       break;

    vector<int> vec;
    tot = 0;

    for(int x=0; x<col; x++)
    {
        scanf("%d", &val);
        vec.push_back(val);
    }

    for(int y=0; y<vec.size(); y++)
    {
        if(y == 0)
        {
            tot = row - vec[y];
            current = vec[y];
        }
        else
        {
            if(vec[y] < current)
               tot = tot + (current - vec[y]);
        }
        current = vec[y];
    }
    printf("%d\n", tot);
}

return 0;
}

11878 – Homework Checker 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=3000

A really easy problem. Input parsing, linear scan

How I did it

  • I Read the lines until I find an EOF .
  • Parse every line with 2 flags. Flag 1 until I get ‘+’ or ‘-‘. Flag 2 until i found ‘=’. Flag 3 the rest of the string.
  • Parse number 1, number 2 and answer.
  • If the equation is ok, correct answer +1.
  • Print the number of correct answers.

Hints

  • Just read until EOF.

Code

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

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

int main()
{
char sign;
string str;
bool flag1, flag2;
string n1, n2, res;
int tot = 0;

while(cin>>str)
{
    flag1 = flag2 = false;
    n1 = n2 = res = "";

    for(int a=0; a<str.size(); a++)
    {
        if(isdigit(str[a]) || str[a] == '?')
        {
            if(!flag1)
               n1 += str[a];
            else if(!flag2)
                    n2 += str[a];
            else
                res += str[a];
        }
        else if(str[a] == '+' || str[a] == '-')
        {
            sign = str[a];
            flag1 = true;
        }
        else
        {
            flag2 = true;
        }
    }

    if(res != "?")
    {
        int number1 = parseInt(n1);
        int number2 = parseInt(n2);
        int answer  = parseInt(res);

        if(sign == '+')
        {
            if(number1 + number2 == answer)
              tot++;
        }
        else
        {
            if(number1 - number2 == answer)
              tot++;
        }
    }
}
printf("%d\n",tot);
return 0;
}