網頁

顯示具有 math 標籤的文章。 顯示所有文章
顯示具有 math 標籤的文章。 顯示所有文章

2013年12月4日 星期三

c048: Ant on a Chessboard、UVA 10161

 (defun calc-coor (sec) 
   "計算座標函式"
   (if (equal sec 0) 
       (return-from calc-coor))
   ;; 把方格從0秒開始計算
   ;; 再以斜線上的點作為基準(base) 
   ;; 最後計算 基準+位移 即為所求
   (let* (( sec (- sec 1))
         (layer (+ (floor (sqrt sec)) 1))
         (base (* layer (- layer 1)))
         (displace (- sec base))
         (coor (list layer layer)))
     (if (equal (rem layer 2) 1)
        (if (> displace 0) 
            (decf (first coor) displace) 
            (incf (second coor) displace))
         (if (> displace 0) 
            (decf (second coor) displace)
            (incf (first coor) displace)))
     coor
     ))
  
 (defun chess-board ()
   (do ((sec (read) (read))) 
       ((<= sec 0) )
     (format t "~A~%" (calc-coor sec))))  
 
 (chess-board) 
 #include <stdio.h>
 #include <math.h> 
 
 int main(int argc, char *argv[])
 {
        int N ; 
        int layer ; 
        int base ; 
        int displace ; 
        int coor[2] ; 
        while(scanf("%d",&N) && N){
                N-- ; 
                layer = floor(sqrt(N))+1; 
                base = layer*(layer-1) ; 
                displace = N-base ; 
                coor[0] = coor[1] = layer ; 
 
                if(layer%2 == 1) 
                        if(displace > 0)
                                coor[0] -= displace ; 
                        else
                                coor[1] += displace ;
                else
                        if(displace > 0) 
                                coor[1] -= displace ; 
                        else
                                coor[0] += displace ; 
                printf("%d %d\n", coor[0], coor[1]) ;  
        }
        return 0;
 }
 
題目連結:http://zerojudge.tw/ShowProblem?problemid=c048

2013年9月14日 星期六

c033: Prime Cuts、UVA 406

#include<stdio.h>

int primes[] = {1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997} ;

int main(){
    int N, C ;
    int i ;

    while(scanf("%d %d", &N, &C)==2){
        int count = 0 ;
        for(i=0 ; i<sizeof(primes)/sizeof(int) ; ++i){
            if(primes[i]<=N) count++ ;
            else break ;
        }
        int C_new = (count%2 ? 2*C-1 : 2*C ) ;
        int low, high ;
        if(C_new>count) {
            low = 0, high = count-1 ;
        }
        else{
            if(count%2){
                int mid = count/2 ;
                low = mid-(C-1) ;
                high = mid+(C-1) ;
            }
            else{
                int mid1 = count/2-1 ;
                int mid2 = count/2 ;
                low = mid1-(C-1) ;
                high = mid2+(C-1) ;
            }
        }
        printf("%d %d:", N, C) ;
        for(i=low ; i<=high ; ++i)
            printf(" %d",primes[i]) ;
        printf("\n\n") ;
    }
    return 0 ;
}

2013年9月11日 星期三

c032. 00382 - Perfection、UVA 382

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int sum_divisor(int n){
    if(n==1) return 0 ;
    else{
        int sum = 1, i ;
        for(i=2 ; i<=sqrt(n) ; ++i){
            if(!(n%i)){
                if(i == n/i) sum += i ;
                else sum += i+n/i ;
            }
        }
        return sum ;
    }
}

int main(){
    int n ;
    int sum ;
    int i ;
    puts("PERFECTION OUTPUT") ;
    while(scanf("%d",&n)==1 && n){
        printf("%5d  ",n) ;
        sum = sum_divisor(n) ;
        if(sum < n) puts("DEFICIENT") ;
        else if(sum > n) puts("ABUNDANT") ;
        else puts("PERFECT") ;
    }
    puts("END OF OUTPUT") ;
    return 0;
}

c031: Count on Cantor、UVA 264

#include<stdio.h>
#include<stdlib.h>
#define swap(a,b){int t; t=a; a=b; b=t;}

int main(){
    int n ;
    int i ;
    while(scanf("%d",&n)==1){
        for(i=1 ; i<4473 ; ++i){
            int low = (i*i-i+2)>>1 ;
            int high = ((i+1)*(i+1)-(i+1)+2)>>1 ;
            if(n>=low && n<high){
                int left=1, right=i ;
                int d=n-low ;
                left += d, right -= d;
                if(i%2) swap(left,right) ;
                printf("TERM %d IS %d/%d\n", n, left, right) ;
            }
        }
    }
    return 0 ;
}

2013年8月25日 星期日

c024: 10079 - Pizza Cutting、UVA 10079

#include<stdio.h>

int main(){
    long long int n ;
    while(scanf("%lld",&n) && n>=0)
        printf("%lld\n",(n*(n+1)>>1)+1) ;
    return 0 ;
}

c022. 10783 - Odd Sum、UVA 10783

#include<stdio.h>
#include<stdlib.h>

int main(){
    int t ;
    int a, b ;
    int sum ;
    int i, j;
    scanf("%d", &t) ;
    for(j=1 ; j<=t ; ++j){
        sum = 0 ;
        scanf("%d %d", &a, &b) ;
        a = a%2?a:a+1 ;
        for(i=a ; i<=b ; i+=2){
            sum += i ;
        }
        printf("Case %d: %d\n", j, sum) ;
    }
    return 0 ;
}

c015. Reverse and Add、UVA 10018

#include<iostream>
#include<string>
#include<sstream>
using namespace std ;

size_t reverseint(size_t num){
    size_t inrev=0 ;
    while(num>0){
        inrev = inrev*10+num%10 ;
        num /= 10 ;
    }
    return inrev ;
}

size_t reva(size_t num){
    return num+reverseint(num) ;
}

int check_pd(unsigned int num){
    stringstream ss ;
    string str ;
    ss << num ;
    ss >> str ;
    for(int i=0 ; i<str.length()>>1 ; ++i)
        if(str[i]!=str[str.length()-i-1])
            return 0 ;
    return 1 ;
}

int main(){
    int t,  counter;
    size_t num ;
    cin >> t ;
    while(t--){
        counter = 0 ;
        cin >> num ;
        do{
            num = reva(num) ;
            counter++ ;
        }while(!check_pd(num)) ;
        cout << counter << " " << num << endl ;
    }
    return 0 ;
}

2013年8月22日 星期四

c014. Primary Arithmetic、UVA 10035

#include<iostream>
#include<string>

using namespace std ;

int main(){
    string num1, num2 ;
    int cb, cs ;
    string fill ;
    while(cin >> num1 >> num2){
        if(num1=="0" && num2=="0") break ;

        if(num1.length() > num2.length()){
            fill.assign(num1.length()-num2.length(),'0') ;
            num2 = fill+num2 ;
        }
        else{
            fill.assign(num2.length()-num1.length(),'0') ;
            num1 = fill+num1 ;
        }

        string::reverse_iterator rit1=num1.rbegin() ;
        string::reverse_iterator rit2=num2.rbegin() ;
        cb = 0, cs = 0;
        for(; rit1!=num1.rend() ; ++rit1, ++rit2){
            if(*rit1-'0'+*rit2-'0'+cb >= 10) cb=1, cs++ ;
            else cb=0;
        }
            if(cs>1)
            cout << cs << " carry operations." << endl ;
        else if(cs==1)
            cout << "1 carry operation." << endl ;
        else
            cout << "No carry operation." << endl ;
    }
    return 0 ;
}

2013年8月3日 星期六

c002: f91、UVA 10696 - f91

#include<stdio.h>
#include<stdlib.h>

int main(){
    int n ;
    while(scanf("%d",&n)==1){
        if(!n) break ;
        if(n>100) printf("f91(%d) = %d\n",n,n-10) ;
        else{
            printf("f91(%d) = 91\n",n);
        }
    }
    return 0 ;
}

2013年5月23日 星期四

Xelatex, beamer for persentation of UVA 846 Steps

%\documentclass{beamer}
%\documentclass[slidestop,compress,mathserif]{beamer}
\documentclass[slidestop,compress,mathserif,xcolor=dvipsnames]{beamer}
\usetheme{Berlin}
\usecolortheme{seahorse}
%\usecolortheme[named=black]{structure}
\setbeamercolor{section in head/foot}{fg=white,bg=gray!20!black}
\setbeamertemplate{items}[ball]
%\setbeamertemplate{blocks}[rounded][shadow=true] 
\setbeamertemplate{navigation symbols}{}
%\setbeamertemplate{headline}{} %<= to suppress the headline otherwise section and subsection will be displayed in the navigation bar
\setbeamertemplate{footline}{}
%\beamersetuncovermixins{\opaqueness<1>{25}}{\opaqueness<2->{15}} 
\let\Tiny=\tiny
\usepackage{fontspec}
   \setmainfont{WenQuanYi Micro Hei}
    \setsansfont{WenQuanYi Micro Hei Mono}
    \setmonofont{WenQuanYi Micro Hei Mono}
    \XeTeXlinebreaklocale "zh"
    \XeTeXlinebreakskip = 0pt plus 1pt
\begin{document}
\setbeamerfont{subsection in sidebar}{size=\tiny}
\setbeamerfont{title in sidebar}{size=\tiny}

\title{\fontsize{28}{35}\selectfont UVA 846\\ \fontsize{14pt}{20pt}\selectfont Steps}
%\author{Author Name}
\institute{}

\frame{\titlepage}

\section*{題意}
\begin{frame}
  \frametitle{題意}
  \uncover<1-> {給你兩個點X、Y\\}
  \uncover<2-> {從X走到Y \\}
  \uncover<3-> {利用下列走法規則 \\}
  \begin{itemize}
        \uncover<4-> {\item 第一步和最後一步的距離一定是1\\}
        \uncover<5-> {\item 下一步走的距離與上一步走的距離相同\\}
        \uncover<6-> {\item 下一步走的距離與上一步走的距離多或少1單位\\}
  \end{itemize}
  \uncover<7-> {找出從X走到Y的最小步數即可\\}
\end{frame}

\section*{Input and Output}
\begin{frame}
  \frametitle{Input and Output}
    Input \\
    3     //The number of test cases \\
    45 48 // 1 1 1 \\
    45 49 // 1 2 1 \\
    45 50 // 1 2 1 1 \\
    Output \\
    3       \\
    3       \\
    4       \\
\end{frame}

\section{思考路線}
\subsection{}
\begin{frame}
  \frametitle{怎樣才可以做到最少步數?}
  \begin{itemize}
    \uncover<1->{\item{一次能走越大的距離越好}}
    \uncover<2->{\item{單邊一直加1?}}
    \uncover<3->{\item{左右邊一直加1?}}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{$n^2$ trick}
  1 + $\cdots$ + n-1 + n + n-1 + $\cdots$ + 1 = n$^{2}$
\end{frame}

\section{解題方法}
\subsection{}
\begin{frame}
  \frametitle{演算法}
  \uncover<1->{d = X-Y  // d 表示X到Y的距離 \\}
  \uncover<2->{n = $\lfloor\sqrt{d}\rfloor$  // n 表示取$\sqrt{d}$的整數部份\\ }
  \vspace{\baselineskip}
  \uncover<3->{if d == n$^2$ \\ \qquad return 2n-1 \\ \vspace{\baselineskip}}
  \uncover<4->{if 0 < d-n$^2$ <= n \\  \qquad return 2n \\ \vspace{\baselineskip}}
    \uncover<5->{if n < d-n$^2$ < 2n+1 \\ \qquad return 2n+1  \\ \vspace{\baselineskip}}
\end{frame}

\begin{frame}
  \frametitle{說明}
    \uncover<1->{第一種情況,當 d == n$^2$ \\
                 \qquad 最短的步數走法必然是1+$\cdots$+(n-1)+n+(n-1)+$\cdots$+1\\
                 \qquad $\therefore$ 最短步數為 2n-1 步\\
                 \vspace{\baselineskip}}
  \uncover<2->{第二種情況,當 d-n$^2$ <= n  \\
               \qquad 先利用第一種情況的解法,走n$^2$單位\\
               \qquad 剩下還有一個小於等於n的值,插入可放置的位置即可\\
               \qquad $\therefore$ 最短步數為 2n 步\\
               \vspace{\baselineskip}}
  \uncover<3->{第三種情況,n < d-n$^2$ < 2n+1 \\
               \qquad 先利用第一種情況的解法,走n$^2$單位,再插入n\\
               \qquad 最後在插入小於等於n的值\\
               \qquad $\therefore$ 最短步數為 2n+1 步\\}
\end{frame}
\subsection{}
\begin{frame}
  \frametitle{小證明}
  \uncover<1->{proof: d-n$^2$ < 2n+1\\}
  \uncover<2->{\qquad    \qquad $0  < n < \sqrt{d} < n+1$\\}
  \uncover<3->{\qquad    \qquad $n^2 < d < n^2+2n+1$ \\ }
    \uncover<4->{\qquad  \qquad $0 < d-n^2 < 2n+1$ \\}
\end{frame}
\end{document}

PDF:UVA 846 Steps(Result)