網頁

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

2013年8月12日 星期一

c005. 環保獎金、UVA 10300 - Ecological Premium

 #include<stdio.h>
 #include<stdlib.h>
 
 int main(){
        int c, i, j, n  ;
        long long int farm[3] ;
        long long int sum ; 
        while(scanf("%d",&c)==1){
                for(i=0 ; i<c ; ++i){
                        sum = 0 ;
                        scanf("%d",&n) ;
                        for(j=0 ; j<n ; ++j){
                                scanf("%lld %lld %lld",&farm[0],&farm[1],&farm[2]) ; 
                                sum += farm[0]*farm[2] ;
                        }
                        printf("%lld\n",sum) ; 
                }
        }
        return 0 ;
 }

c004. Beat the Spread!、uva - 10812

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

int main(){
    int c, a, b, i, sum;
    while(scanf("%d",&c)==1){
        for(i=0 ; i<c ; ++i){
            scanf("%d %d",&a,&b) ;
            sum = a+b ;
            if(a<0 || b<0 || a<b || sum%2){ puts("impossible") ; continue ; }
            else printf("%d %d\n",sum/2,a-sum/2) ;
        }
    }
    return 0 ;
}

2013年8月5日 星期一

emacs 簡繁轉換 -- 使用新同文堂python

需要環境

python2.7

請上官網下載安裝,並加入系統變數裡裡

unicad.el - emacs 識別編碼用

下載並安裝unicad.el

unicad-1.1.4.tar.gz
wget "http://unicad.googlecode.com/files/unicad-1.1.4.tar.gz"
tar -zxv -f unicad-1.1.4.tar.gz

在.emcac加入下面這兩行

(add-to-list 'load-path "/path/to/unicad_folder") ;這行要指定路徑
(require 'unicad)

安裝教學

git clone "https://github.com/monkey413/tongwen-emacs.git

在.emcac加入下面這兩行

(add-to-list 'load-path "/path/to/tongwen-emacs") ;這行要指定路徑
(require 'hanconvert)

使用教學

M-x hanconvert-to

2013年8月3日 星期六

d710. parking lot

#include<iostream>
#include<string>

using namespace std;

int main(){
    string db[21][2] ;
    string s, s1 ;
    int c, q, j=0;
    while(cin>>c>>q){
        if(j++) cout << endl ;
        for(int i=0 ; i<c ; ++i)
            cin >> db[i][0] >> db[i][1] ;

        for(int i=0 ; i<q ; ++i){
            cin >> s >> s1 ;
            if(s=="brand"){
                for(int i=0 ; i<c ; ++i)
                    if(db[i][0]==s1)
                        cout << db[i][0] << " " << db[i][1] << endl ;
            }
            else{
                for(int i=0 ; i<c ; ++i)
                    if(db[i][1]==s1)
                        cout << db[i][0] << " " << db[i][1] << endl ;
            }
        }
    }
    return 0 ;
}

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

c001: 最長共同字串(LCS)、UVA 10405 - Longest Common Subsequence

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#define MAX 1001
using namespace std;

int main(){
    string s1, s2 ;
    int LCS_table[MAX][MAX] ;
    while(cin>>s1>>s2){
        memset(LCS_table,0,sizeof(LCS_table));
        for(int i=1 ; i<s1.length()+1 ; ++i){
            for(int j=1 ; j<s2.length()+1 ; ++j){
                if(s1[i-1] == s2[j-1]){
                    LCS_table[i][j] = LCS_table[i-1][j-1]+1 ;
                }
                else{
                    LCS_table[i][j] = max(LCS_table[i-1][j],LCS_table[i][j-1]) ;
                }
            }
        }
        cout << LCS_table[s1.length()][s2.length()] << endl ;
    }
    return 0 ;
}