UVa 11567 – Moliu Number Generator

#include <cstdio>
#include <algorithm>

using namespace std;

unsigned int solve (unsigned int i){

    unsigned int count = 1;
    while (i>1){
        if ((i&1)>0)
            return (count  + min(solve(i+1),solve(i-1)));
        count+=1+(1&i);
        i>>=1;
    }
    return count;
}

int main()
{
    unsigned int n;
    while (scanf("%d",&n)==1){
        if (n)
            printf("%d\n",solve(n));
        else
            printf("0\n");

    }

}

UVa 11059 – Maximum Product

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

int n, i,j,nums[19];
long long p ;
long long maxP = 0 ;

long long search() {
    maxP = 0;
    for (i = 0 ; i < n ; i ++){
        p = 1;
        for (j=i ; j < n && p ; j++){
            p*=nums[j];
            if ( p>maxP)
                maxP = p;
        }
    }
    return maxP;
}

int main()
{
    int t = 0;
    while (scanf("%d", &n)==1){
        for(int i = 0; i < n; ++i) {
            scanf("%d",&nums[i]);
        }
        printf("Case #%d: The maximum product is %lld.\n\n", ++t, search());
    }
}

UVa 11559 – Event Planning


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class UVa11559_eventPlanning {

	static int n,b,h,w;
	static int[] costs ;
	static int [][] hotels ;

	static int solve(){
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (hotels[i][j]>=n && costs[i]*n <=b){
					min = min>costs[i]*n ?costs[i]*n : min ;
				}
			}
		}

		return min ;
	}

	public static void main(String[] args)throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tk ;
		String s ;
		while(true){
			s= br.readLine();
			if(s==null || s.isEmpty())System.exit(0);
			tk = new StringTokenizer(s);
			n = Integer.parseInt(tk.nextToken());
			b = Integer.parseInt(tk.nextToken());
			h = Integer.parseInt(tk.nextToken());
			w = Integer.parseInt(tk.nextToken());
			costs = new int[h];
			hotels = new int [h][w];

			for (int i = 0; i < h; i++) {
				costs[i] = Integer.parseInt(br.readLine());
				tk = new StringTokenizer(br.readLine());
				for (int j = 0; j < w; j++) {
					hotels[i][j] = Integer.parseInt(tk.nextToken());
				}
			}
			int res = solve();
			if (res == Integer.MAX_VALUE)
			System.out.println("stay home");
			else System.out.println(res);

		}
	}

}