Posts Tagged ‘UVA Solution’

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define MX 100000
bool ver[MX]={false};
int list[MX];
void factit(int n)
{
    int lim=(int)sqrt((double)n);
    int i;
    while(!(n&1))
    {
        printf("2");
        n/=2;
        if(n>1)
        {
            printf(" x ");
        }
    }
    for(i=3;i<=lim;i+=2)
    {
        if(n==1) break;
        while(n%i==0)
        {
            printf("%d",i);
            n/=i;
            if(n>1)     printf(" x ");
        }
    }
    if(n>1) printf("%d",n);
    printf("\n");
}
int main()
{
    int n,i,j,b,c,a,m,fl;
    while(scanf("%d",&n)==1 && (m=n))
    {
        fl=0;
        printf("%d = ",m);
        if(n<0)
        {
            printf("-1 x ");
            n=-n;
        }
        factit(n);
    }
    return 0;
}

495-Fibonacci Freeze

Posted: September 16, 2011 in ACM, Fibonacci, JAVA
Tags: , ,


this is my First JAVA UVA solution….. :D

//// 495-Fibonacci Freeze
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
    public static void main(String arg[])
    {
        BigInteger[] fibo=fib(5001);
        Scanner inp= new Scanner(System.in);
        while(inp.hasNext())
        {
            int n= inp.nextInt();
            System.out.printf("The Fibonacci number for %d is %d\n",n,fibo[n]);
        }
    }
    
    public static BigInteger[] fib(int n)
    {
        BigInteger[] fib = new BigInteger[n+1];
        fib[0]=BigInteger.ZERO;
        fib[1]=BigInteger.ONE;
        for(int i=2;i<=n;i++)
        {
            fib[i]=fib[i-1].add(fib[i-2]);
        }
        return fib;
    }
}