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

UVa 10035 Primary Arithmetic

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

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

    int n1,n2;
    while (scanf("%d%d",&n1,&n2)!=EOF && !(n1==0&&n2==0)) {
        int tmp=0;
        int carry = 0;
        while (n1||n2) {
            tmp = (n1%10)+(n2%10)+tmp;
            tmp/=10;
            if (tmp)
                carry++;
            n1/=10;
            n2/=10;
        }
        if (carry==0)
            printf("No carry operation.\n");
        else if(carry == 1)
            printf("1 carry operation.\n");
        else
            printf("%d carry operations.\n",carry);
    }
    return 0;
}