1. ACM UVA-516

    質數

    bool isprime[32769];
    int prime[32769];
    typedef unsigned int  ui;
    int main(int argc, char const* argv[])
    {
    
        int m = 0;
        for(int i = 2;i <= 32768;i++ ){
            if(!isprime[i]){
                prime[m++] = i;
            for(int j = i*i;j <= 32768;j = j+i)
                isprime[j] = true;
            }
        }
        string line;
        while ...

    Read more...


  2. ACM UVA-530

    組合數

    int n,m;
    unsigned long long combination(){
        unsigned long long p = 1;
        if(m>(n/2)){
            m = n-m;
        }
        for(int i = 1;i <= m;i = i+1){
           p = p*(n-i+1)/i;
        }
        return p;
    }
    int main(int argc, char const* argv[])
    {
        while(~scanf("%d %d",&n,&m ...

    Read more...


  3. ACM UVA-541

    Matrix

    int row[105];
    int col[105];
    int main(int argc, char const* argv[])
    {
        string line;
        int n;
        while(~scanf("%d",&n) && n != 0){
            getchar();
            int tmp = n;
            int rowe= 0;
            int cole = 0;
            int rr = 0;
            int ic,ir;
            while(tmp--){
                getline(cin,line);
                istringstream stream(line);
                int bit ...

    Read more...


  4. ACM UVA-583

    質數

    bool prime[65541];
    long long u[33300];
    int main(int argc, char const* argv[])
    {
        prime[1] = true;
        int m = 0;
        for(long long int i = 2;i <= 65540;i++){
            if(!prime[i]){
                u[m++] = i;
                for(long long int j = i*i;j <= 65540;j = j+i){
                    prime[j ...

    Read more...


  5. ACM UVA-591

    平均數

    int main(int argc, char const* argv[])
    {
        int n;
        int i = 0;
        int r[50];
        while(~scanf("%d",&n) && n != 0){
            int sum = 0;
            int p = n;
            int j = 0;
            while(n--){
                int h;
                scanf("%d",&h);
                sum = sum+h;
                r[j++] = h;
            }
            int avg = sum/p;
            int ans ...

    Read more...


  6. ACM UVA-624

    典型的0/1背包問題。

    int main(int argc, char const* argv[])
    {
        int n;
        int m;
        int dp[2000];
        int put[25][2000];
        while(scanf("%d",&n) != EOF){
            vector<int> cd;
            memset(dp,0,sizeof(dp));
            memset(put,0,sizeof(put));
            scanf("%d",&m);
            while(m--){
                int l;
                scanf("%d",&l);
                cd.push_back ...

    Read more...


  7. ACM UVA-627

    這題用BFS即可解出,有一點必須要注意的是,雖然題目要求當經過的routers數量相同,則輸出最小的id,但因為題目的input已經有幫我們做好排序,所以每次找到的自然都會是最小的id。

    //ignore header files
    bool bfs(int*,int,int,vector<int>*);
    void print_path(int *,int ,int);
    int main(int argc, char const* argv[])
    {
        int n;
        char line[1005];
        while(scanf("%d",&n) != EOF){
            getchar();
            vector <int> list[305];
            for(int i = 0;i<n;i ...

    Read more...


  8. ACM UVA-993

    greedy

    int main(int argc, char const* argv[])
    {
        int q[8] = {9,8,7,6,5,4,3,2};
        int t;
        while(~scanf("%d",&t)){
            while(t--){
                long long n;
                scanf("%lld",&n);
                int sum = 0;
                int k = 1;
                int test = 1;
                int tmp = n;
                if(n != 1){
                    for(int ...

    Read more...


  9. Worker Thread Pattern -- 等到工作來,來了就工作

    在 Worker Thread Pattern 中,worker thread 會依序抓一件工作來執行。當沒有工作時,worker thread 會停下來等待新的工作過來。

    worker thread 也有人稱為 Background Thread。也有人把視點放在管理 worker thread 的地方,稱之為 thread pool。

    程式範例:

    所有參與者

    Client

    Client 會建立 Requset,傳給 Channel。例如 ClientThread 類別。

    Channel

    Channel 從 Client 取得 Request,傳給 Worker。例如 Channel 類別。

    Worker

    Worker 會從 Channel ...

    Read more...


« Page 13 / 13