ACM 591

文章分類: ACM

#include<stdio.h>
#include<stdlib.h> 
 
int main()
{ 
    int n,h,moves, m[100];
    int i, counter = 0; 
    while(1)
    {
        scanf("%d", &n);
        if(n==0) return 0; 
        h = 0; 
        moves = 0; 
        for(i=0;i<n;i++)
        {
            // every one height 
            scanf("%d", &m[i]);
            // total 
            h+=m[i]; 
        }
 
        // average 
        h /= n;
        for(i=0;i<n;i++)
        {
            if(m[i] > h) moves += m[i]-h; 
        } 
 
        printf("Set #%d\n", ++counter); 
        printf("The minimum number of moves is %d.\n\n", moves); 
    } 
 
    return 0; 
}

ACM 579

文章分類: ACM

#include<stdio.h>
#include<stdlib.h> 
 
int main()
{
    int h,m;
    double degree_h, degree_m, degree; 
    double ans;
 
    while(1) 
    {
        scanf("%d:%d", &h, &m); 
        if(h==0 && m==0) return 0; 
 
        degree_h = 30.0 * (h+m/60.0); 
        degree_m = 6.0*m;
 
        ans = degree_m - degree_h;
        if(ans < 0) ans = -ans;
        if(ans > 180) ans = 360-ans;
 
        printf("%.3f\n", ans); 
    } 
 
    return 0; 
}

ACM 478

文章分類: ACM

/*
兩線段相交參考
http://irw.ncit.edu.tw/peterju/algorithm.html
*/ 
#include<stdio.h>
#include<stdlib.h> 
#include<math.h>
 
#define RECT 1
#define CIRCLE 2
#define TRIANGLE 3
 
typedef struct t_Point
{
    double x;
    double y;
}Point;
 
typedef struct t_Rect
{
    Point top_left;
    Point bottom_right;
}Rect;
 
typedef struct t_Circle
{
    Point center;
    double r;
}Circle;
 
typedef struct t_Triangle
{
    Point p[3];
}Triangle; 
 
double cross(double x1, double y1, double x2, double y2){ 
        return x1 * y2 - y1* x2; 
}; 
 
int main()
{
    int shape[10];
    Rect r[10];
    Circle c[10];
    Triangle t[10]; 
    Point p;
    int i,j,shape_count=0,p_count=0;
    double t_t,t_s; 
    int in=0;
    char command;
    while(1)
    {
        scanf("%c", &command);
        if(command == 'r')
        {
            scanf("%lf %lf %lf %lf", 
                &r[shape_count].top_left.x, 
                &r[shape_count].top_left.y, 
                &r[shape_count].bottom_right.x,
                &r[shape_count].bottom_right.y);
 
            shape[shape_count] = RECT;
            shape_count++;
        }
        else if(command == 'c')
        {
            scanf("%lf %lf %lf",
                &c[shape_count].center.x,
                &c[shape_count].center.y,
                &c[shape_count].r);
 
            shape[shape_count] = CIRCLE;
            shape_count++;
        }
        else if(command == 't')
        {
            scanf("%lf %lf %lf %lf %lf %lf",
                &t[shape_count].p[0].x,
                &t[shape_count].p[0].y,
                &t[shape_count].p[1].x,
                &t[shape_count].p[1].y,
                &t[shape_count].p[2].x,
                &t[shape_count].p[2].y); 
            shape[shape_count] = TRIANGLE;
            shape_count++;
        } 
        else if(command == '*') break;
    }
 
    while(scanf("%lf %lf", &p.x, &p.y) )
    {
        if(p.x == 9999.9 && p.y == 9999.9) break;
        p_count++;
        in = 0;
        for(i=0;i<shape_count;i++)
        {
            if(shape[i] == RECT)
            {
                if(p.x > r[i].top_left.x &&
                   p.x < r[i].bottom_right.x &&
                   p.y < r[i].top_left.y &&
                   p.y > r[i].bottom_right.y)
                {   
                    printf("Point %d is contained in figure %d\n",p_count,i+1);
                    in = 1;
                }
            }else if(shape[i] == CIRCLE)
            {
                if( 
                    (p.x - c[i].center.x) * (p.x - c[i].center.x) +
                    (p.y - c[i].center.y) * (p.y - c[i].center.y)
                      < c[i].r * c[i].r)
                {
                    printf("Point %d is contained in figure %d\n",p_count,i+1);
                    in = 1;
                }
            }else if(shape[i] == TRIANGLE)
            {
                // 假設沒有相交 
                in = 1;                 
                for(j=0;j<3;j++)
                {
                    if( 
                     (
                      (p.x-t[i].p[j].x) * (t[i].p[j].y-t[i].p[(i+1)%3].y) -
        			  (p.y-t[i].p[j]y) * (t[i].p[j]x-t[i].p[(i+1)%3].x)
                     )*
        	 		 (
                      ( wcx-tri[where].x[i] )*( tri[where].y[i]-tri[where].y[(i+1)%3] ) -
         			  ( wcy-tri[where].y[i] )*( tri[where].x[i]-tri[where].x[(i+1)%3] )
                     ) <= 0.0 
                    )
                    t_t = 
                      (
                        (t[i].p[(j+2)%3].y - t[i].p[(j+1)%3].y) * // y4-y3
                        (t[i].p[(j+1)%3].x - p.x)+                // x3-x1 
                        (t[i].p[(j+2)%3].x - t[i].p[(j+1)%3].x) * // x4-x3
                        (p.y - t[i].p[(j+1)%3].y )                // y1-y3 
                      )/
                      (
                        (t[i].p[j].x - p.x) *                      // x2-x1
                        (t[i].p[(j+2)%3].y - t[i].p[(j+1)%3].y) -  // y4-y3 
                        (t[i].p[j].y - p.y) *                      // y2-y1
                        (t[i].p[(j+2)%3].x - t[i].p[(j+1)%3].x)    // x4-x3  
                      ); 
 
                    t_s = 
                      (
                        (t[i].p[j].y - p.y) *                      // y2-y1
                        (t[i].p[(j+1)%3].x - p.x)+                 // x3-x1
                        (t[i].p[j].x - p.x) *                      // x2-x1
                        (p.y - t[i].p[(j+1)%3].y )                 // y1-y3 
                      )/
                      (
                        (t[i].p[j].x - p.x) *                      // x2-x1
                        (t[i].p[(j+2)%3].y - t[i].p[(j+1)%3].y) -  // y4-y3 
                        (t[i].p[j].y - p.y) *                      // y2-y1
                        (t[i].p[(j+2)%3].x - t[i].p[(j+1)%3].x)    // x4-x3  
                      ); 
 
                      if( t_t >= 0 && t_t <= 1)
                        if( t_s >= 0 && t_s <= 1)
                            {in=0; break;} 
                } 
                if(in == 1)
                    printf("Point %d is contained in figure %d\n",p_count,i+1);
            } 
        }
        if(in == 0)
            printf("Point %d is not contained in any figure\n",p_count);
    }
 
    return 0; 
}

ACM 477

文章分類: ACM

#include<stdio.h>
#include<stdlib.h> 
#include<math.h>
 
#define RECT 1
#define CIRCLE 2
 
typedef struct t_Point
{
    double x;
    double y;
}Point;
 
typedef struct t_Rect
{
    Point top_left;
    Point bottom_right;
}Rect;
 
typedef struct t_Circle
{
    Point center;
    double r;
}Circle;
 
int main()
{
    int shape[10];
    Rect r[10];
    Circle c[10];
    Point p;
    int i,shape_count=0,p_count=0;
    int in=0;
    char command;
    while(1)
    {
        scanf("%c", &command);
        if(command == 'r')
        {
            scanf("%lf %lf %lf %lf", 
                &r[shape_count].top_left.x, 
                &r[shape_count].top_left.y, 
                &r[shape_count].bottom_right.x,
                &r[shape_count].bottom_right.y);
 
            shape[shape_count] = RECT;
            shape_count++;
        }
        else if(command == 'c')
        {
            scanf("%lf %lf %lf",
                &c[shape_count].center.x,
                &c[shape_count].center.y,
                &c[shape_count].r);
 
            shape[shape_count] = CIRCLE;
            shape_count++;
        }
        else if(command == '*') break;
    }
 
    while(scanf("%lf %lf", &p.x, &p.y) )
    {
        if(p.x == 9999.9 && p.y == 9999.9) break;
        p_count++;
        in = 0;
        for(i=0;i<shape_count;i++)
        {
            if(shape[i] == RECT)
            {
                if(p.x > r[i].top_left.x &&
                   p.x < r[i].bottom_right.x &&
                   p.y < r[i].top_left.y &&
                   p.y > r[i].bottom_right.y)
                {   
                    printf("Point %d is contained in figure %d\n",p_count,i+1);
                    in = 1;
                }
            }else if(shape[i] == CIRCLE)
            {
                if( 
                    (p.x - c[i].center.x) * (p.x - c[i].center.x) +
                    (p.y - c[i].center.y) * (p.y - c[i].center.y)
                     < c[i].r * c[i].r)
                {
                    printf("Point %d is contained in figure %d\n",p_count,i+1);
                    in = 1;
                }
            }
        }
        if(in == 0)
            printf("Point %d is not contained in any figure\n",p_count);
    }
 
    return 0; 
}

ACM 476

文章分類: ACM

#include<stdio.h>
#include<stdlib.h> 
 
typedef struct t_Point
{
    double x;
    double y;
}Point;
 
typedef struct t_Rect
{
    Point top_left;
    Point bottom_right;
}Rect;
 
int main()
{
    Rect r[10];
    Point p;
    int i,r_count=0,p_count=0;
    int in=0;
    char c;
    while(1)
    {
        scanf("%c", &c);
        if(c == 'r')
        {
            scanf("%lf %lf %lf %lf", 
                &r[r_count].top_left.x, 
                &r[r_count].top_left.y, 
                &r[r_count].bottom_right.x,
                &r[r_count].bottom_right.y);
            r_count++;
        }
        else if(c == '*') break;
    }
 
    while(scanf("%lf %lf", &p.x, &p.y) )
    {
        if(p.x == 9999.9 && p.y == 9999.9) break;
        p_count++;
        in = 0;
        for(i=0;i<r_count;i++)
        {
            if(p.x > r[i].top_left.x &&
               p.x < r[i].bottom_right.x &&
               p.y < r[i].top_left.y &&
               p.y > r[i].bottom_right.y)
            {   
                printf("Point %d is contained in figure %d\n",p_count,i+1);
                in = 1;
            }
        }
        if(in == 0)
            printf("Point %d is not contained in any figure\n",p_count);
    }
 
    return 0; 
}

ACM 458

文章分類: ACM

#include<iostream>
using namespace std;
int main(){
    char a[100];
    while(cin >> a){
        for(int i=0;a[i] != '\0';i++){
           cout << char(a[i]+249);
        }
        cout <<"\n";
    }
}

ACM 374

文章分類: ACM

/*
(a * b) mod c = ((a mod c) * (b mod c)) mod c 
*/
#include<iostream>
using namespace std;
 
unsigned long ComputeR(unsigned long B, unsigned long P, unsigned long M){
    if (P==1) return B % M;
    unsigned long i,res=1,count=0;
    i=1;
    res = B%M;
    while(2*i<P){
        res = (res*res)%M;
        i*=2;
        count++;
    }
    return (res * ComputeR(B,P-i,M))%M;
 
}
 
int main(){
    unsigned long B,P,M;
 
    while(cin >> B >> P >> M ){
        if (P == 0) cout << 1 << endl;
        else if (P == 1) cout << B % M << endl;
        else cout << ComputeR(B,P,M) << endl;
    }
    return 0;
}

ACM 305

文章分類: ACM

#include <iostream>
 
bool killallbad(const int max, const int half, const int times, const int from){
  int kill, killnext;
  kill = times % max;
  if (kill > half) return false;
  if ( max == half ) return true;
  killnext = kill
}
 
int main(){
  int k,range,result;
  int count;
  while(1){
    cin >> k;
    if ( k == 0 ) break;
    range = 2 * k ;
    int m=k+(k/2)+1;
    count = 0;
    do{
      if ( (m-1)%(2*k) == 0 ) m+=k;
      count++;
      if( killallbad(range, k, m, 1) ) break;
      else m++;
    }while(1); 
    cout << count << ",";
    cout << m << '\n';
  }
  return 0;
}

ACM 294

文章分類: ACM

#include<stdio.h>
#include<stdlib.h> 
#include<math.h> 
 
int main()
{
    int N,L,U; 
    int i,j,m; 
    int max, max_n, max_tmp; 
    scanf("%d", &N);
 
    for(i=0;i<N;i++)
    {
        scanf("%d %d", &L, &U);
        max_tmp = 0; 
        max = 0; 
        max_n = 0; 
        for(j=L;j<=U;j++)
        {
            max_tmp = 0; 
            for(m=1;m<=sqrt(j);m++)
            {
                if(j%m == 0)
                {
                    if(m*m == j) max_tmp++;
                    else max_tmp+=2; 
                } 
            } 
            if(max_tmp > max)
            {
                max = max_tmp;
                max_n = j; 
            } 
        } 
        printf("Between %d and %d, %d has a maximum of %d divisors.\n",L,U,max_n, max);
 
    } 
    return 0; 
}

ACM 272

文章分類: ACM

#include<stdio.h>
#include<stdlib.h>
 
int main()
{
    char c;
    int i=1;
    while(1)
    {
        c=getchar(); 
        if(c == EOF) break;
        if(c == '"')
        { 
            if(i==1) {putchar('`');putchar('`');i=0; } 
            else if(i==0){ putchar('\'');putchar('\'');i=1; } 
        }else putchar(c);
    } 
    return 0; 
}

Bad Behavior has blocked 9 access attempts in the last 7 days.