2014/07/10

[Java] 正規表達式筆記


基本表達

abc – exactly this sequence of three letters
[abc] – any one of the letters a, b, or c
[^abc]– any character except one of the letters a, b, or c
[ab^c] – a,b,^ or c. (^擺在[後表否定,擺在中間表字元)
[a-z] – any one character from a to z
[a-zA-Z0-9] – any one letter or digit

[Java] HashMap簡易使用方法及如何排序


[Java] 正規表達式範例



2014/03/03

[UVa] 11321 Sort! Sort!! And Sort!!!


solution:自行修改cmp function
             對於理解qsort滿有幫助的一題



http://uva.onlinejudge.org/external/113/11321.html
#include<stdio.h>
#include <stdlib.h>

typedef struct data{
    int num;
    int remainder;
}Data;
int cmp(const void *a,const void *b);


int main(void){
    int n,m,i;
    while (scanf("%d%d",&n,&m)==2 &&n&&m) {
        Data data[n];
        for (i = 0; i < n; i++) {
            scanf("%d",&data[i].num);
            data[i].remainder = data[i].num % m;
        }
        qsort(data, n, sizeof(data[0]), cmp);
        printf("%d %d\n",n,m);
        for (i=0;i<n; i++) {
            printf("%d\n",data[i].num);
        }
    }
    printf("0 0\n");
    return 0;
}

int cmp(const void *a,const void *b){
    Data c,d;
    c = *(Data*)a;
    d = *(Data*)b;
    if (c.remainder != d.remainder)
        return c.remainder>d.remainder?1:-1;
    else{
        if(c.num%2==0 && d.num%2==0){  
            if (c.num != d.num)
                return c.num>d.num?1:-1;  //若都是偶數,較小的排在前面,則回傳值依舊
            return 0;
        }
        else if ((c.num%2==1||c.num%2==-1) && (d.num%2==1||d.num%2==-1)){
            if (d.num != c.num)
                return c.num>d.num?-1:1; //若都是奇數,較大的排在前面,則回傳值顛倒
            return 0;
        } //以下一奇一偶 奇數排在前面
        else if ((c.num%2==1||c.num%2==-1) && d.num%2==0){
            return -1;  //c為奇數d為偶數,回傳-1維持cd順序
        }
        else
            return 1;   //d為奇數c為偶數,回傳1順序變dc
    }
}

[C] qsort用法



2014/02/25

[UVa] 10763 Foreign Exchange

Note:
     用到qsort然後兩兩對消


http://uva.onlinejudge.org/external/107/10763.html
#include<stdio.h>
#include <stdlib.h>

int cmp(const void *s1,const void *s2){
    return *(int*)s1-*(int*)s2;
}

int main(void){
    int n,i,count;
    while (scanf("%d",&n)!=EOF && n) {
        count = 0;
        int array[2*n];
        for (i = 0;i < 2*n;i++)
            scanf("%d",&array[i]);
        qsort(array, 2*n, sizeof(int), cmp);
        for (i = 0; i < 2*n; i+=2) {
            if (array[i]!=array[i+1])
                count++;
        }
        if (count==0)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

2014/02/24

Android 得到GPS位址


Reference tutorial

注意幾點

  • 先在manifest.xml 加入user-permission
  • GPS manager class:     

public class GPSTracker extends Service implements LocationListener


UVa 10327 Flip Sort

http://uva.onlinejudge.org/external/103/10327.html
#include<stdio.h>


int main(void){
    int n,i,j,count=0;
    int array[1000];
    while (scanf("%d",&n) != EOF) {
        count = 0;
        for (i = 0; i < n; i++) {
            scanf("%d",&array[i]);
        }
        for (i = 0; i < n; i++) {
            for (j=i+1; j<n; j++) {
                if (array[i]>array[j]) {
                    count++;
                }
            }
        }
        printf("Minimum exchange operations : %d\n",count);
    }
    
    return 0;
}

UVa 11462 Age Sort

Note:
Use qsort in <stdlib.h>

http://uva.onlinejudge.org/external/114/11462.html
#include<stdio.h>
#include <stdlib.h>

int cmp(const void *s1, const void *s2);

int main(void){
    int n = 0,i;
    int array[2000005];
    while (scanf("%d",&n)!=EOF && n!=0) {
        for (i = 0; i < n; i++)
            scanf("%d",&array[i]);
        
        qsort(array, n, sizeof(int), cmp);
        printf("%d",array[0]);
        for (i = 1; i < n; i++) {
            printf(" %d",array[i]);
        }
        printf("\n");
    }
    
    return 0;
}

int cmp(const void *s1, const void *s2){
    return *(int *)s1 - *(int *)s2;
}

2014/02/23

UVa 494 Kindergarten Counting Game

http://acm.cs.nthu.edu.tw/problem.php?pid=7073
#include<stdio.h>
#include<string.h>

int main(void){
    char string[10000];
    int i, flag, count, k;
    while(gets(string)!= NULL){
        flag = 0;
        count = 0;
        k = strlen(string);
        for(i = 0; i < k;i++){
            if(string[i]>=65 && string[i]<=90||string[i]>=97&&string[i]<=122)
                flag = 1;
            else{
                count = count + flag;
                flag = 0;
            }
        }
        count = count + flag;/*判斷最後一個字*/
        printf("%d\n", count);
    }
    return 0;
}

UVa 10041 Vito's family

http://acm.cs.nthu.edu.tw/problem.php?pid=7074
#include <stdlib.h>
#include <stdio.h>

int main(int argc, const char * argv[])
{
    int num,people,i,j,x,tmp,half,newadd;
    int total = 0;
    scanf("%d",&num);
    for (i = 0; i < num; i++) {
        scanf("%d",&people);
        int address[30000];
        half = people/2;
        for (j = 0; j < people; j++) {
            scanf("%d",&address[j]);

        }
        for (j = 0; j < people; j++) {
            for (x = j+1; x < people; x++) {
                if (address[j]>address[x]) {
                    tmp = address[j];
                    address[j] = address[x];
                    address[x] = tmp;
                }
            }
        }
        newadd = address[half];
        for (j = 0; j < people; j++) {
            total = total + abs(newadd-address[j]);
        }
        printf("%d\n",total);
        total = 0;
        
    }
    return 0;
}

UVa 428 Permutation Arrays

http://acm.cs.nthu.edu.tw/problem.php?pid=7078
#include<stdio.h>
#define len 100000
char a[len][50];
int main(){
 int n,i,j,k;
 char ch;
 int jj=0;
 scanf("%d",&n);
 while(n--){
  int order[len];
  if(jj++)
     puts("");
     for( i=0 ; ; i++ ){
      scanf("%d",&order[i]);
   ch=getchar();
   if(ch=='\n')
      break;
   }
   for( j=0 ; j<=i ; j++ )
    scanf("%s",&a[ order[j]-1 ]);
   for( j=0 ; j<=i ; j++ )
    printf("%s\n",a[j]);
 }

 return 0;
}

UVa 438 The Circumference of the Circle

http://acm.cs.nthu.edu.tw/problem.php?pid=7077
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793

int main(int argc, const char * argv[])
{

    double x1,x2,x3,y1,y2,y3,a,b,c,s,area,total,r;
    while (scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3) !=EOF) {
        a = sqrt(pow((x1-x2),2)+pow((y1-y2), 2));
        b = sqrt(pow((x1-x3),2)+pow((y1-y3), 2));
        c = sqrt(pow((x2-x3),2)+pow((y2-y3), 2));
        s = (a+b+c)/2;
        area = sqrt(s*(s-a)*(s-b)*(s-c));
        r = (a*b*c)/(4*area);
        total = 2*PI*r;
        printf("%.2lf\n",total);
    }
    return 0;
}