2015年2月13日 星期五

Mac 讀進資料夾下所有檔案名稱

#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

vector<string> GetFile(string dir){
    vector<string> files;

    DIR *dp;
    struct dirent *dirp;
    if((dp = opendir(dir.c_str())) == NULL)
        printf("Error opening\n");

    while((dirp = readdir(dp)) != NULL)
        files.push_back(string(dirp->d_name));

    closedir(dp);

    return files;
}

int main(){
    vector<string> files = GetFile(".");
   
    for(int i = 0; i < files.size(); ++i){
        if(files[i] != "." && files[i] != ".." && files[i] != ".DS_Store"){
            //
            cout << files[i] << endl;
        }
    }

    return 0;
}

2015年2月12日 星期四

TIOJ 1312 家族

#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
int Union[10005];

int FindUnion(int x){
    if(Union[x] == x)
        return x;
    return Union[x] = FindUnion(Union[x]);
}

void SetUnion (int x, int y){
    Union[FindUnion(x)] = FindUnion(y);
}

int main(){
    int n, m, k, x, y;
    while(scanf("%d %d", &n, &m) != EOF){
        for(int i = 1; i <= n; i++)
            Union[i] = i;

        for (int i = 0; i < m; ++i){
            scanf("%d %d", &x, &y);
            SetUnion(x ,y);
        }

        scanf("%d", &k);
        for (int i = 1; i < n; ++i)
            if (FindUnion(i) == FindUnion(k)){
                printf("%d\n", i);
                break;
            }
    }
}

TIOJ 1308 幾組解咧(其一)

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string.h>
using namespace std;
long long c(int x, int y){
    long long ans = 1;
    for (long long i = 1; i <= y; ++i){
        ans *= (long long)x - i + 1;
        ans /= i;
    }
    return ans;
}

int main(){
    int n, m;
    while(true){
        scanf("%d %d", &n, &m);
        if(n == 0)
            break;
        
        printf("%lld\n", c(n + m - 1, m));
    }
} 

TIOJ 1108 樹的三兄弟

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string.h>
using namespace std;

char pre[55], in[55];
vector<char> post;

int search(char* s, char c){
    for (int i = 0; i < strlen(s); ++i)
        if(s[i] == c)
            return i;

    return -1;
}

void FindPost(char* _in, char* _pre, int len){
    int root = search(_in, _pre[0]);

    if(root > 0)
        FindPost(_in, _pre + 1, root);

    if(root < len - 1)
        FindPost(_in + root + 1, _pre + root + 1, len - root - 1);

    post.push_back(_pre[0]);
}

int main(){
    while(scanf("%s", pre) != EOF){
        scanf("%s", in);

        post.clear();
        FindPost(in, pre, strlen(in));

        for (int i = 0; i < post.size(); ++i)
            printf("%c", post[i]);
        printf("\n");
    }
} 

2015年2月4日 星期三

Matlab 筆記

變數:

陣列宣告 A[0:X] 等於A[0 1 2 ... X]

陣列運算

A+B
A-B

A.*B (各元素相乘)
A*B  (矩陣相乘)

A/B
A./B  (右除)

sqrt(A) (對應元素開平方)
sqrtm(A) (矩陣開平方)

畫圖

plot(x, y, c)

x.y -> 大小相同陣列,用相對應的值畫出線條
c -> 線條顏色及虛實,'r-'紅色實線
線條符號
說明
線條顏色
說明
o
小圓
y
黃(yellow)
x
x-標記
m
紫紅(magenta)
+
加號
c
藍綠(cyan)
*
星號
r
紅(red)
-
實線
g
綠(green)
.
細點線
b
藍(blue)
:
粗點線
w
白(white)
-.
虛線加點
k
黑(black)
--
虛線
 
 

subplot(x, y, n)

在x*y大小的figure上,畫在第n個位置