Wednesday, July 2, 2014

UVa - 401 - Palindromes

#include<iostream>
#include<algorithm>
#include<sstream>
#include<fstream>
#include<utility>
#include<cstdlib>
#include<cstring>
#include<string>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cctype>
#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#define mx 10077
using namespace std;
int main(){
    char memory[mx];
    string s, x, y;
    int i;
    memset(memory, 0, s.size());
    memory['A'] = 'A';
    memory['E'] = '3';
    memory['H'] = 'H';
    memory['I'] = 'I';
    memory['J'] = 'L';
    memory['L'] = 'J';
    memory['M'] = 'M';
    memory['O'] = 'O';
    memory['S'] = '2';
    memory['T'] = 'T';
    memory['U'] = 'U';
    memory['V'] = 'V';
    memory['W'] = 'W';
    memory['X'] = 'X';
    memory['Y'] = 'Y';
    memory['Z'] = '5';
    memory['1'] = '1';
    memory['2'] = 'S';
    memory['3'] = 'E';
    memory['5'] = 'Z';
    memory['8'] = '8';
    while(cin >> s){
            x = "";
            y = "";
        for(i = s.size() - 1; i >= 0; i--){
            x = x + s[i];
            y = y + memory[s[i]];
        }
        if(s == x && s != y)
            cout << s << " -- is a regular palindrome." << endl << endl;
        else if(s != x && s == y)
            cout << s << " -- is a mirrored string." << endl << endl;
        else if(s == x && s == y)
             cout << s << " -- is a mirrored palindrome." << endl << endl;
        else
            cout << s << " -- is not a palindrome." << endl << endl;
    }
 return 0;
}

UVa - 382 - Perfection

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
long long i, t, sum, j, a[100000];
t = 0;
while(t <= 100000)
{
scanf("%lld", &a[t]);
if(a[t] == 0)break;
t++;
}
printf("PERFECTION OUTPUT\n");
for(i = 0; i < t; i++)
    {
    sum = 0;
    printf("%5lld  ", a[i]);
    for(j = 1;j <= a[i] / 2; j++){
        if(a[i] % j==0)
        sum = sum + j;
        }
    if(a[i] == sum)
    printf("PERFECT\n");
    else if(a[i] > sum)
    printf("DEFICIENT\n");
    else if(a[i] < sum)
    printf("ABUNDANT\n");
    }
printf("END OF OUTPUT\n");
return 0;
}

UVa - 374 - Big Mod

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
long long b_mod(long long b, long long p, long long m){
    if(p == 0)return 1;
    if(p % 2 == 0)
        return ((b_mod(b, p / 2, m)) * (b_mod(b, p / 2, m))) % m;
    else
        return ((b % m) * b_mod(b, p - 1, m)) % m;
}
int main(){
    long long b, p, m;
    while(cin >> b >> p >> m){
        long long result = b_mod(b, p, m);
        cout << result << endl;
    }
    return 0;
}

UVa - 272 - TEX Quotes

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
int main(){
    long int i, count = 0;
    char s[100000];
    while(gets(s)){
    for(i = 0; s[i]; i++){
            if(s[i] == '"'){
                count += 1;
                if(count % 2 == 0){
                        printf("''");
                }
                else{
                        printf("``");
                }
                }
                else
                    printf("%c", s[i]);
                }
                printf("\n");
                }
                return 0;
                }

UVa - 136 - Ugly Numbers

#include <iostream>
#include <cstdio>

using namespace std;

int main(){
    printf("The 1500'th ugly number is 859963392.\n");
    return 0;
}

UVa - 102 - Ecological Bin Packing

#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    int a[10];
    int i, sum;
    for(i = 0; i < 9; i++){
        scanf("%d", &a);
    }
        sum = a[0] + a[4] + a[8];
    printf("%d", sum);
    return 0;
}

UVa - 100 - The 3n + 1 problem - recursive solution


  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5. int gen(int n){
  6.     int count = 0;
  7.     if(== 1)
  8.         return count = 1;
  9.     if(% 2 == 0){
  10.         return count = 1 + gen(/ 2);
  11.     }
  12.     else{
  13.         return count = 1 + gen(3 * n + 1);
  14.     }
  15. }
  16. int main(){
  17.     int m, n, x, y;
  18.     while(cin >> x >> y){
  19.         cout << x << " " << y << " ";
  20.         m = max(x, y);
  21.         n = min(x, y);
  22.         int mx = 0;
  23.         for(int i = n; i <= m; i++){
  24.             gen(i);
  25.             mx = max(mx,gen(i));
  26.         }
  27.         cout << mx << endl;
  28.     }
  29.     return 0;
  30. }