網頁

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年12月2日 星期一

c045: Rotating Sentences、UVA 490

 #include <stdio.h>
 #include <string.h> 
 #define MAX 101
 char arr[MAX][MAX] ;
 
 int main(int argc, char *argv[])
 {
        memset(arr, '\0', sizeof(arr) ) ; 
        int i, j ;
        int rows = 0; 
        int cols = 0 ; 
        for (i=0; fgets(arr[i], sizeof(arr[i]), stdin) != NULL ; ++i) 
                rows++ ; 
 
        char *pos ; 
        for (i=0 ; arr[i][0]!='\0'; ++i)
        {
                if ((pos=strchr(arr[i], '\n')) != NULL)
                        *pos = '\0';
                if (strlen(arr[i]) > cols)
                        cols = strlen(arr[i]) ; 
        }
        /* 字串由左下開始輸出,先向上輸出,再向右輸出 */ 
        for (i=0; i<cols; ++i)
        {
                for (j=rows-1; j>=0 ; --j) 
                        printf("%c",(arr[j][i]=='\0'?' ':arr[j][i])) ;
                puts("") ; 
        }
 
        return 0;
 }
 (defparameter arr (make-array '(100) :initial-element nil))
 (defparameter rows 0) 
 (defparameter cols 0) 
 
 (defun read-lines () 
 "read lines until eof" 
   (do ((i 0 (incf i) )
        (line (read-line *standard-input* nil 'eof)
             (read-line *standard-input* nil 'eof)))
       ((eql line 'eof) i)
     (setf (aref arr i) line)))
 
 
 (defun max-len-str() 
   (let (( max -1 ))
     (do ((i 0 (incf i)))
        ((>= i rows) max) 
        (if ( > (length (aref arr i)) max ) 
            (setf max (length (aref arr i)))))))
 
 (defun printf() 
   (do ((i 0 (incf i)) ) 
       ((>= i cols)) 
     (do ((j (- rows 1) (decf j))) 
        ((< j 0)) 
       (if (> (length (aref arr j)) i)  
          (princ (aref (aref arr j) i))
          (princ " ") ))
     (princ #\Newline))) 
 
 (defun rotate-string() 
   (setf rows (read-lines)) 
   (setf cols (max-len-str)) 
   (printf))
 
 (rotate-string)  
題目連結:http://zerojudge.tw/ShowProblem?problemid=c045

2013年11月30日 星期六

Balance partion problem

 (defun balpart (lst)
   "balace partion problem " 
   (let* ((sum (apply #'+ lst))
         (halfsum (floor (/ sum 2)) )
         (sol (make-list (+ halfsum 1) :initial-element nil )) 
         )
     (setf (first sol ) t ) 
     (dolist (i lst ) 
       (do ((j halfsum (decf j)))
          ((< j i))
        (if (nth (- j i) sol)
            (setf (nth j sol) t))))
 
     (let ( (halfcloser (do ((i halfsum (decf i) )) ((nth i sol) i ) )))
       (return-from balpart (- sum halfcloser halfcloser)))
     ))
 
 (format t "Please input a number list: ~%")
 (finish-output t ) 
 (format t "The minimum difference is : ~A~%" (balpart (read) )  )

 
詳細過程請參考此連結

2013年11月27日 星期三

ctex simsun.ttc not found 解決方法

 
網路上下載simsun.ttc後, 尋找你的texmf-local資料夾 
將simsun.ttc放入texmf-local/fonts/truetype/simsun.ttc(沒有的資料夾自己建)
最後執行texhash(sudo texhash) 

2013年10月27日 星期日

emacs rainbow-mode and rainbow.el 安裝

 
rainbow-mode 
用途:在特定語句或語法時,會產生顏色
安裝方法:
M-x package install -> rainbow-mode 
結果圖:


rainbow.el 
用途:在對應的delimiters會也相對應的顏色
安裝方法:
MELPA 好像也有這個package,但是好像有點錯誤,不知為何無法下載,感覺像是還沒放到伺服器上。
所以,只好使用老方法下載安裝了~~

git clone https://github.com/jlr/rainbow-delimiters.git
(add-to-list 'load-path "/path/to/rainbow-delimiters/") ;
(require 'rainbow-delimiters)
(global-rainbow-delimiters-mode)

個人感覺,原本的顏色不是很好,因此,透過rainbow-mode,稍微修改了一下rainbow-delimiter.el裡的顏色
;;; Faces:
;; Unmatched delimiter face:
(defface rainbow-delimiters-unmatched-face
  '((((background light)) (:foreground "#ff0000"))
    (((background dark)) (:foreground "#ff0000")))
  "Face to highlight unmatched closing delimiters in."
  :group 'rainbow-delimiters-faces)

;; Faces for highlighting delimiters by nested level:
(defface rainbow-delimiters-depth-1-face
  '((((background light)) (:foreground "#ffa500"))
    (((background dark)) (:foreground "#ffa500")))
  "Nested delimiters face, depth 1 - outermost set."
  :tag "Rainbow Delimiters Depth 1 Face -- OUTERMOST"
  :group 'rainbow-delimiters-faces)

(defface rainbow-delimiters-depth-2-face
  '((((background light)) (:foreground "#ffff00"))
    (((background dark)) (:foreground "#ffff00")))
  "Nested delimiters face, depth 2."
  :group 'rainbow-delimiters-faces)

(defface rainbow-delimiters-depth-3-face
  '((((background light)) (:foreground "#00ff00"))
    (((background dark)) (:foreground "#00ff00")))
  "Nested delimiters face, depth 3."
  :group 'rainbow-delimiters-faces)

(defface rainbow-delimiters-depth-4-face
  '((((background light)) (:foreground "#0000ff"))
    (((background dark)) (:foreground "#0000ff")))
  "Nested delimiters face, depth 4."
  :group 'rainbow-delimiters-faces)

(defface rainbow-delimiters-depth-5-face
  '((((background light)) (:foreground "#5600ef"))
    (((background dark)) (:foreground "#5600ef")))
  "Nested delimiters face, depth 5."
  :group 'rainbow-delimiters-faces)

(defface rainbow-delimiters-depth-6-face
  '((((background light)) (:foreground "#9400f3"))
    (((background dark)) (:foreground "#9400f3")))
  "Nested delimiters face, depth 6."
  :group 'rainbow-delimiters-faces)

(defface rainbow-delimiters-depth-7-face
  '((((background light)) (:foreground "#95ff70"))
    (((background dark)) (:foreground "#95ff70")))
  "Nested delimiters face, depth 7."
  :group 'rainbow-delimiters-faces)

(defface rainbow-delimiters-depth-8-face
  '((((background light)) (:foreground "#f0a880"))
    (((background dark)) (:foreground "#f0a880")))
  "Nested delimiters face, depth 8."
  :group 'rainbow-delimiters-faces)

(defface rainbow-delimiters-depth-9-face
  '((((background light)) (:foreground "#90ffff"))
    (((background dark)) (:foreground "#90ffff")))
  "Nested delimiters face, depth 9."
  :group 'rainbow-delimiters-faces)
結果圖:

2013年10月25日 星期五

mupdf

 
mupdf 非常的輕量級,而且開啟pdf的速度非常快,可惜沒有支援書籤功能,不過有時候在開檔案很大的pdf時還是很好用的。
安裝:
sudo apt-get install mupdf
快捷鍵:
man mupdf 
manual 裡面有介紹

2013年10月20日 星期日

2013年10月10日 星期四

texlive 安裝

 
我使用的是連線安裝版,指令:sudo ./install-tl  -gui text --location ftp://ftp.ccu.edu.tw/pub/tex/systems/texlive/tlnet/ 
texlive 下載頁面
texlive 安裝教學 
 
xelatex font config 
  1. Copy the texlive-fontconfig.conf file to ~/.fonts.conf, where ~ is your home directory.
  2. Run fc-cache -fv.
 

2013年10月3日 星期四

autojump installation and usage

Step 1. autojump install:
        sudo apt-get install 
        or 
        git clone https://github.com/joelthelion/autojump.git 
        sudo ./install.sh

Step 2. autojump usage: 
        j keyword 
        j __n  # n is number, usually push tab can see the all directory  

pushd popd dirs command tutorial

For Zsh:  
    1. Basic Usage
       dirs -v # display numbered list of directory stack
       pushd -  # push and back to last directory  == pushd -0 
       pushd -N # push and back to last Nth directory 
       pushd +N # push and back to Nth diretory of dirs right(0) to left 
       popd -N # pop Nth directory of dirs left to right 
       popd +N # pop Nth directory of dirs right to left

2013年9月27日 星期五

wps for linux 無法啟動

 
安裝完wps後,產生:error while loading shared libraries: libgthread-2.0.so.0: cannot open shared object file: No such file or directory

Step 1. 
因為wps是32bit的系統,所以要安裝32的library
sudo apt-get install ia32-libs

缺少的字體文件:
下載:http://bbs.wps.cn/thread-22355435-1-1.html
之後複製到:~/.fonts 或 /usr/share/fonts/wps-office

參考自:http://blog.sina.com.cn/s/blog_6c9d65a10101adlw.html

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 ;
}

c013. 00488 - Triangle Wave、UVA 488

#include<stdio.h>

int main(){
    int t ;
    int a, f ;
    int plus, counter ;
    int i, j, k;
    scanf("%d",&t) ;
    while(t--){
        scanf("%d %d",&a,&f) ;
        for(k=0 ; k<f ; ++k){
            plus = 1, counter = 1 ;
            for(i=1 ; i<= 2*a-1 ; ++i){
                for(j=0 ; j<counter ; ++j)
                    printf("%d",counter) ;
                puts("") ;

                if(i==a) plus = 0 ;
                (plus) ? counter++ : counter-- ;
            }
            puts("") ;
        }
    }
    return 0 ;
}

2013年8月20日 星期二

oh-my-zsh and auto-fu.zsh

安裝oh-my-zsh

clone the oh-my-zsh.git

git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

備份現有的.zshrc (optional)

cp ~/.zshrc ~/.zshrc.old

複製oh-my-zsh提供的樣本 (optional)

cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc 

完成

安裝auto-fu.zsh

clone the auto-fu.zsh.git

git clone https://github.com/monkey413/auto-fu.git $ZSH_CUSTOM/plugins/auto-fu
cd $ZSH_CUSTOM/plugins/auto-fu

加入下列到~/.zshrc

auto_fu_path="$ZSH_CUSTOM/plugins/auto-fu/auto-fu.zsh"
if [ -f $auto_fu_path ]; then
    source $auto_fu_path
    function zle-line-init () {
        auto-fu-init
    }
    zle -N zle-line-init
    zstyle ':completion:*' completer _oldlist _complete
fi

完成

2013年8月18日 星期日

c012: Tell me the frequencies! 、UVA 10062

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 1005 

void eli_fgets_newline(char *line){
    char *pos;
    if ((pos=strrchr(line, '\n')) != NULL)
            *pos = '\0';
    if ((pos=strrchr(line, '\r')) != NULL)
        *pos = '\0';
    return ;
}

int binary_search(int db[max][2], int db_size, int c){
    int left = 0 ;
    int right = db_size-1;
    while(left<=right){
        int middle = (left+right)/2 ;
        if(db[middle][0] == c)
            return middle ;
        else if(db[middle][0] > c)
            right = middle-1 ;
        else
            left = middle+1 ;
    }
    return -1 ;
}

int compare(const void* a, const void* b){
    if(*((int*)a+1) != *((int*) b+1))
        return *((int*)a+1)-*((int*)b+1) ;
    else
        return *(int*)b-*(int*)a ;
}

int cmp(const void* a, const void* b){
    return *(int*)a-*(int*)b ;
}

void print_db(int db[max][2], int db_size){
    int i ;
    for(i=0 ; i<db_size ; ++i){
        printf("%d %d\n",db[i][0],db[i][1]) ;
    }
    puts("") ;
    return ;
}

int main(){
    char line[max] ;
    int db[max][2] ;
    int i, found ;
    int len = 0, db_size = 0;

    while(fgets(line,sizeof(line),stdin)){
        eli_fgets_newline(line) ;
        db_size = 0, memset(db,0,sizeof(db)) ;

        len = strlen(line) ;
        for(i=0 ; i<len ; ++i){
            qsort(db,db_size,sizeof(db[0]),cmp) ;
            found = binary_search(db,db_size,line[i]) ;
            if(found == -1){
                db[db_size][0] = line[i] ;
                db[db_size][1]++ ;
                db_size++ ;
            }
            else{
                db[found][1]++ ;
            }
        }
        qsort(db, db_size, sizeof(db[0]), compare) ;
        print_db(db,db_size) ;
    }

    return 0 ;
}

2013年8月16日 星期五

c009: Simple Base Conversion、UVA 10473

#include<iostream>
#include<string>
#include<sstream>
#include<cctype>
#include<algorithm>

using namespace std ;

int main(){
    string line ;
    size_t found ;
    stringstream ss ;
    int num ;
    while(cin>>line){
        found = line.find("0x") ;
        if(found == string::npos){
            ss << line ; ss >> dec >> num ; ss.clear() ;
            if(num<0) break ;
            ss << hex << num ; ss >> line ; ss.clear() ;
            transform(line.begin(), line.end(), line.begin(), ::toupper);
            cout << "0x" << line << endl ;
        }
        else{
            ss << hex << line.substr(2) ;
            ss >> num ;
            cout << dec << num << endl ;
        }
        ss.str() = "" ;
        ss.clear() ;
    }
    return 0;
}

c010. What is the Median?、UVA 10107 (insert sort)

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std ;

int main(){
    vector<int>  numbers ;
    int num, i=0 ;
    while(cin >> num){
        if(numbers.size() <= 0){
            numbers.push_back(num) ;
            cout << num << endl ;
            continue ;
        }
        vector<int>::iterator it = numbers.begin() ;
        if(num <= numbers.front())
            numbers.insert(it,num) ;
        else if(num >= numbers.back())
            it=numbers.end(), numbers.insert(it,num) ;
        else{
            for(it=numbers.begin() ; it<numbers.end()-1 ; it++){
                if(*(it+1)>num && *it<num || *it == num){
                    numbers.insert(it+1,num) ;
                    break ;
                }
            }
        }
        /*
        for(it=numbers.begin() ; it<numbers.end() ; it++){
            cout << *it << " ";
        }
        cout << endl ;
        */
        if(numbers.size()%2){
            cout << numbers[(numbers.size()-1)/2] << endl ;
        }
        else{
            cout << (numbers[(numbers.size()-1)/2]+numbers[numbers.size()/2])/2 << endl ;
        }
    }
    return 0 ;
}

c007: TeX Quotes、UVA 272

#include<iostream>
#include<cstdlib>
#include<string>

using namespace std ;

int main(){
    string line ;
    size_t found = 0 ;
    string quote[] = {"``", "''"} ;
    int which = 0 ;
    while(getline(cin,line,'\n')){
        found = line.find("\"") ;
        while(found != string::npos){
            line.replace(found,1,quote[which]) ;
            which ^= 1 ;
            found = line.find("\"",found+2) ;
        }
        cout << line << endl ;
    }
    return 0 ;
}

c006. Combination Lock、UVA 10550

/*順逆要反過來看*/
#include<stdio.h>
#include<stdlib.h>

int ccw(int a, int b){
    int ans = ( a-b>0 ? a-b : a-b+40) ;
    return ans * 9 ;
}

int main(){
    int a, b, c, d;
    while(scanf("%d %d %d %d", &a, &b, &c, &d)==4){
        if(!a && !b && !c && !d) break ;
        int sum = 1080 ;
        sum += ccw(a,b) ;
        sum += 360-ccw(b,c) ;
        sum += ccw(c,d) ;
        printf("%d\n",sum) ;
    }
    return 0 ;
}