#UVa:579-ClockHands

先把時鐘刻劃360格,一格1度,則再將時針指向的位置的度數去跟分針指向的位置的度數進行絕對值相減,即可得解。(若大於180度,就利用360去減其值的絕對值去把它減到小於180度為止)

P.S. 時針指的刻度算法: 小時*30(一個小時30度) + 分/60 * 30(因為分針走一圈,時針就走30度)。分針指的刻度算法: 分*6(五分鐘走30度,則一分鐘走6度)

C++(0.036)

/*******************************************************/
/* UVa 579 ClockHands                                  */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/29                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int main(){
  int H, M;
  float H_angle, M_angle, angle;
  while( scanf( "%d:%d", &H, &M ) != EOF && (H || M) ){
    H_angle = H*30.0 + M/60.0 * 30.0;
    M_angle = M*6.0;
    angle = fabs( H_angle-M_angle );
    printf( "%.3f\n", (angle > 180.0)? 360.0 - angle : angle );
  }
  return 0;
}

#UVa:494-Kindergarten Counting Game

尋找字母與非字母之間的交界處有幾個就可以知道有幾個字了。

P.S. 不能單純只用空格來分隔單字,測資當中會有類似 This!has,four.words? 是4個字的這種測資。

C++(0.020)

/*******************************************************/
/* UVa 494 Kindergarten Counting Game                  */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/29                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cctype>
using namespace std;

int main(){
  string s, temp;
  int count;
  bool alphap = 0;
  while( getline( cin, s ) ){
    count = 0;
    for( int i = 0 ; i < s.length() ; i++ ){
      if( isalpha(s[i]) && !alphap ){
        alphap = 1;
        count++;
      }
      else if( !isalpha(s[i]) && alphap ){
        alphap = 0;
      }
    }
    printf( "%d\n", count );
  }
  return 0;
}

#UVa:488-Triangle Wave

照題目做即可。

P.S. 關於後面空白行的部分,是指每個波(不管是同筆資料或不同筆資料)之間用空白行隔開的意思。

C++(0.548)

/*******************************************************/
/* UVa 488 Triangle Wave                               */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/28                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  int n;
  int A, F;
  while( scanf( "%d", &n ) != EOF ){
    for( int i = 0 ; i < n ; i++ ){
      scanf( "%d%d", &A, &F );
      if( i ) printf( "\n" );
      for( int j = 0 ; j < F ; j++ ){
        if( j ) printf( "\n" );
        for( int k = 1 ; k <= A ; k++ ){
          for( int l = 1 ; l <= k ; l++ )
            printf( "%d", k );
          printf( "\n" );
        }
        for( int k = A-1 ; k >= 1 ; k-- ){
          for( int l = 1 ; l <= k ; l++ )
            printf( "%d", k );
          printf( "\n" );
        }
      }
    }
  }
  return 0;
}

#UVa:478-Points in Figures: Rectangles, Circles, Triangles

要計算點是否在長方形內,只需要看點之x座標有否在長方形左上角之x座標與長方形右下角之x座標之間,以及點之y座標有否在長方形左上角之y座標與長方形右下角之y座標之間就可以了。

要計算點是否在圓形內,只需要看點與圓心的距離是否小於半徑即可。

要計算點是否在三角形內,假設要求的點是n,三角形三個點分別是a,b,c,則若n點在三角形內則 nab面積 + nac面積 + nbc面積 跟 abc面積 會相等。若要判斷n點是否在三角形線上,利用nab面積 和 nac面積 和 nbc面積 其中一塊為0就在線上。

再來最麻煩的部份就是浮點數的誤差,這導致浮點數去與另外一個浮點數做相等的判斷時可能會出錯,因此我們可以利用浮點數減去另外一個浮點數小於一個極小的誤差(0.000001之類)當做等於來用。

P.S. 本題我用float沒過,換成double後就過了,越精確會越好喔~XD

C++(0.024)

/*******************************************************/
/* UVa 478 Points in Figures: Rectangles and Circles,  */
/* and Triangles                                       */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/28                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cmath>
#define ERROR 1e-9
using namespace std;

bool doubleequal( double x, double y ){
  return (fabs(x-y) < ERROR);
}

struct Point{
  double x;
  double y;
};

struct Rectangle{
  Point leftup;
  Point rightdown;
};

struct Circle{
  Point center;
  double radius;
};

struct Triangle{
  Point angle[3];
};

struct Figure{
  char type;
  union{
    Rectangle rec;
    Circle cir;
    Triangle tri;
  };
};

double dis( Point p1, Point p2 ){
  return sqrt(pow( (p1.x-p2.x), (double)2.0 ) + pow( (p1.y-p2.y), (double)2.0 ));
}

double area( Point p1, Point p2, Point p3 ){
  return fabs((p1.x*p2.y + p2.x*p3.y + p3.x*p1.y) - (p2.x*p1.y+p3.x*p2.y+p1.x*p3.y));
}

int main(){
  char condition;
  Figure fig[15];
  int total = 0;
  while( scanf( "%c", &condition ) != EOF && condition != '*' ){
    fig[total].type = condition;
    if( condition == 'r' ){
      scanf( "%lf%lf%lf%lf", &(fig[total].rec.leftup.x), 
        &(fig[total].rec.leftup.y), 
        &(fig[total].rec.rightdown.x), 
        &(fig[total].rec.rightdown.y));
    }
    else if( condition == 'c' ){
      scanf( "%lf%lf%lf", &(fig[total].cir.center.x),
        &(fig[total].cir.center.y),
        &(fig[total].cir.radius));
    }
    else if( condition == 't' ){
      scanf( "%lf%lf%lf%lf%lf%lf", &(fig[total].tri.angle[0].x),
        &(fig[total].tri.angle[0].y),
        &(fig[total].tri.angle[1].x),
        &(fig[total].tri.angle[1].y),
        &(fig[total].tri.angle[2].x),
        &(fig[total].tri.angle[2].y));
    }
    total++;
    getchar(); /* Delete the enter key */
  }
  Point test;
  int pointnum = 1;
  bool containp = 0;
  double areas[3];
  while( scanf( "%lf%lf", &(test.x), &(test.y) ) != EOF ){
    if( doubleequal( test.x, 9999.9 ) && doubleequal( test.y, 9999.9 ) )
      break;

    containp = 0;
    for( int i = 0 ; i < total ; i++ ){
      if( fig[i].type == 'r' ){
        if( test.x > fig[i].rec.leftup.x && test.x < fig[i].rec.rightdown.x )
          if( test.y < fig[i].rec.leftup.y && test.y > fig[i].rec.rightdown.y ){
            printf( "Point %d is contained in figure %d\n", pointnum, i+1 );
            containp = 1;
          }
      }
      else if( fig[i].type == 'c' ){
        if( dis( test, fig[i].cir.center ) < fig[i].cir.radius ){
          printf( "Point %d is contained in figure %d\n", pointnum, i+1 );
          containp = 1;
        }
      }
      else if( fig[i].type == 't' ){
        areas[0] = area( test, fig[i].tri.angle[0], fig[i].tri.angle[1]);
        areas[1] = area( test, fig[i].tri.angle[1], fig[i].tri.angle[2]);
        areas[2] = area( test, fig[i].tri.angle[2], fig[i].tri.angle[0]); 
        if( doubleequal( areas[0]+areas[1]+areas[2],
          area( fig[i].tri.angle[0], fig[i].tri.angle[1], fig[i].tri.angle[2]) ) && 
          !doubleequal( areas[0] , 0.0 ) && 
          !doubleequal( areas[1] , 0.0 ) && 
          !doubleequal( areas[2] , 0.0 ) ){
          printf( "Point %d is contained in figure %d\n", pointnum, i+1 );
          containp = 1;
        }
      }
    }

    if( !containp )
      printf( "Point %d is not contained in any figure\n", pointnum );
    pointnum++;
  }
  return 0;
}

#UVa:477-Points in Figures: Rectangles and Circles

要計算點是否在長方形內,只需要看點之x座標有否在長方形左上角之x座標與長方形右下角之x座標之間,以及點之y座標有否在長方形左上角之y座標與長方形右下角之y座標之間就可以了。

要計算點是否在圓形內,只需要看點與圓心的距離是否小於半徑即可。

C++(0.028)

/*******************************************************/
/* UVa 477 Points in Figures: Rectangles and Circles   */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/28                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cmath>
#define ERROR 1e-9
using namespace std;

bool floatequal( float x, float y ){
  return (fabs(x-y) < ERROR);
}

struct Point{
  float x;
  float y;
};

struct Rectangle{
  Point leftup;
  Point rightdown;
};

struct Circle{
  Point center;
  float radius;
};

struct Figure{
  char type;
  union{
    Rectangle rec;
    Circle cir;
  };
};

float dis( Point p1, Point p2 ){
  return sqrt(pow( (p1.x-p2.x), (float)2.0 ) + pow( (p1.y-p2.y), (float)2.0 ));
}

int main(){
  char condition;
  Figure fig[15];
  int total = 0;
  while( scanf( "%c", &condition ) != EOF && condition != '*' ){
    fig[total].type = condition;
    if( condition == 'r' ){
      scanf( "%f%f%f%f", &(fig[total].rec.leftup.x), 
        &(fig[total].rec.leftup.y), 
        &(fig[total].rec.rightdown.x), 
        &(fig[total].rec.rightdown.y));
    }
    else if( condition == 'c' ){
      scanf( "%f%f%f", &(fig[total].cir.center.x),
        &(fig[total].cir.center.y),
        &(fig[total].cir.radius));
    }
    total++;
    getchar(); /* Delete the enter key */
  }
  Point test;
  int pointnum = 1;
  bool containp = 0;
  while( scanf( "%f%f", &(test.x), &(test.y) ) != EOF ){
    if( floatequal( test.x, 9999.9 ) && floatequal( test.y, 9999.9 ) )
      break;

    containp = 0;
    for( int i = 0 ; i < total ; i++ ){
      if( fig[i].type == 'r' ){
        if( test.x > fig[i].rec.leftup.x && test.x < fig[i].rec.rightdown.x )
          if( test.y < fig[i].rec.leftup.y && test.y > fig[i].rec.rightdown.y ){
            printf( "Point %d is contained in figure %d\n", pointnum, i+1 );
            containp = 1;
          }
      }
      else if( fig[i].type == 'c' ){
        if( dis( test, fig[i].cir.center ) < fig[i].cir.radius ){
          printf( "Point %d is contained in figure %d\n", pointnum, i+1 );
          containp = 1;
        }
      }
    }

    if( !containp )
      printf( "Point %d is not contained in any figure\n", pointnum );
    pointnum++;
  }
  return 0;
}

#UVa:476-Points in Figures: Rectangles

要計算點是否在長方形內,只需要看點之x座標有否在長方形左上角之x座標與長方形右下角之x座標之間,以及點之y座標有否在長方形左上角之y座標與長方形右下角之y座標之間就可以了。

C++(0.016)

/*******************************************************/
/* UVa 476 Points in Figures: Rectangles               */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/28                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cmath>
#define ERROR 1e-9
using namespace std;

bool floatequal( float x, float y ){
  return (fabs(x-y) < ERROR);
}

struct Point{
  float x;
  float y;
};

struct Rectangle{
  Point leftup;
  Point rightdown;
};

int main(){
  char condition;
  Rectangle rec[15];
  int total = 0;
  while( scanf( "%c", &condition ) != EOF && condition != '*' ){
    scanf( "%f%f%f%f", &(rec[total].leftup.x), 
      &(rec[total].leftup.y), 
      &(rec[total].rightdown.x), 
      &(rec[total].rightdown.y));
    total++;
    getchar(); /* Delete the enter key */
  }
  Point test;
  int pointnum = 1;
  bool containp = 0;
  while( scanf( "%f%f", &(test.x), &(test.y) ) != EOF ){
    if( floatequal( test.x, 9999.9 ) && floatequal( test.y, 9999.9 ) )
      break;

    containp = 0;
    for( int i = 0 ; i < total ; i++ ){
      if( test.x > rec[i].leftup.x && test.x < rec[i].rightdown.x )
        if( test.y < rec[i].leftup.y && test.y > rec[i].rightdown.y ){
          printf( "Point %d is contained in figure %d\n", pointnum, i+1 );
          containp = 1;
        }
    }

    if( !containp )
      printf( "Point %d is not contained in any figure\n", pointnum );
    pointnum++;
  }
  return 0;
}

#UVa:458-The Decoder

利用ASCII碼找出Sample Input和Sample Output相差的k值,將輸入的每個字都以此k值做加減即可得解。

C++(0.172)

/*******************************************************/
/* UVa 458 The Decoder                                 */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/25                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  const int K = (int)('*'-'1');
  string s;
  while( getline( cin, s ) ){
    for( int i = 0 ; i < s.length() ; i++ )
      printf( "%c", s[i]+K );
    printf( "\n" );
  }
  return 0;
}

#UVa:272-TEX Quotes

照著題目要求,更換雙引號即可。

C++(0.016)

/*******************************************************/
/* UVa 272 TEX Quotes                                  */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/24                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  string s;
  bool leftorright = 0;
  while( getline( cin, s ) )
  {
    for( int i = 0 ; i < s.length() ; i++ )
      if( s[i] == '"' ){
        if( leftorright )
          printf( "''" );
        else
          printf( "``" );
        leftorright ^= 1;
      }
      else{
        printf( "%c", s[i] );
      }
    printf( "\n" );
  }
  return 0;
}

#UVa:100-The 3n + 1 problem

照著題目所說的去遞迴即可得解。

C++(0.832)

/*******************************************************/
/* UVa 100 The 3n+1 problem                            */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/23                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int cyclelength( int n );

int main(){
  int i, j, min_i_j, max_i_j;
  int max_length, term_length;
  while( scanf( "%d%d", &i, &j ) != EOF ){
    max_length = 0;
    min_i_j = (i < j)? i : j;
    max_i_j = (i > j)? i : j;
    for( ; min_i_j <= max_i_j ; min_i_j++ ){
      term_length = cyclelength(min_i_j);
      max_length = (term_length > max_length)? term_length : max_length;
    }
    printf( "%d %d %d\n", i, j, max_length );
  }
  return 0;
}

int cyclelength( int n ){
  if( n == 1 )
    return 1;
  else if( n % 2 )
    return 1+cyclelength(3*n+1);
  else
    return 1+cyclelength(n/2);
}
共 36 頁 1 34 35 36