#UVa:10038-Jolly Jumpers

簡單來說,就是要檢查1~n-1的差值是否都存在,差值不一定要3->2->1,也可以是2->3->1,總而言之就是1~n-1的差值都要存在就是Jolly了!

C++(0.008)

/*******************************************************/
/* UVa 10038 Jolly Jumpers                             */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2012/03/16                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main(){
  int n;
  int jolly[3005];
  bool jollyp = 1;
  while( scanf( "%d", &n ) != EOF ){
    bool jump[3005] = {0};
    jollyp = 1;
    for( int i = 0 ; i < n ; i++ )
      scanf( "%d", &jolly[i] );
    for( int i = 1 ; i < n ; i++ ){
      if( abs(jolly[i] - jolly[i-1]) >= n ||
        abs(jolly[i] - jolly[i-1]) <= 0 || 
        jump[abs(jolly[i] - jolly[i-1])] ){

        jollyp = 0;
        break;
      }
      jump[abs(jolly[i] - jolly[i-1])] = 1;
    }
    if( jollyp )
      printf( "Jolly\n" );
    else
      printf( "Not jolly\n" );
  }
  return 0;
}

#UVa:10035-Primary Arithmetic

將輸入的兩個數字的每個位數分開來加加看,記得也要加上前一位數的進位值,這樣就可以做完這題了。

C++(0.044)

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

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

#UVa:10018-Reverse and Add

一直反轉數字然後加起來,就可以得解。

P.S.

  1. 數字的反轉可以看reverse()這部份是怎麼寫的。
  2. 本題至少要加1次。

C++(0.008)

/*******************************************************/
/* UVa 10018 Reverse and Add */
/* Author: LanyiKnight [at] knightzone.org */
/* Version: 2011/11/29 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

unsigned int reverse( unsigned int num ){
  int rev = 0;
  while( num ){
    rev = rev*10 + num%10;
    num /= 10;
  }
  return rev;
}

int main(){
  int N, times;
  unsigned int num, rev;
  while( scanf( "%d", &N ) != EOF ){
    for( int i = 1 ; i <= N ; i++ ){
      scanf( "%u", &num );
      times = 0;
      rev = reverse(num);
      do{
        num = num + rev;
        times++;
        rev = reverse(num);
      }while( num != rev );
      printf( "%d %u\n", times, num );
    }
  }
  return 0;
}

#UVa:913-Joana and the Odd Numbers

首先先找到行數跟那行有幾個值的關係而求得行數,再來透過行數找出最後一個值,接著就求出答案了=D

C++(0.016)

/*******************************************************/
/* UVa 913 Joana and the Odd Numbers                   */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/29                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  long long N, sum;
  while( scanf( "%lld", &N ) != EOF ){
    sum = (N+1)/2; // row number;
    sum = sum*sum*2-1; // the last number of that row;
    sum = 3*sum - 6;
    printf( "%lld\n", sum );
  }
  return 0;
}

#UVa:591-Box of Bricks

先找出平均數,就可以知道後來每一堆積木的個數。將每一堆大於平均數的積木,將其個數減去平均數的數字(也就意味這堆要搬多少個盒子到別堆去)加起來就是答案。

C++(0.008)

/*******************************************************/
/* UVa 591 Box of Bricks                               */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/29                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

int main(){
  int setnum = 1;
  int n, box[55], average, count;
  while( scanf( "%d", &n ) != EOF && n ){
    average = 0;
    count = 0;
    for( int i = 0 ; i < n ; i++ ){
      scanf( "%d", &box[i] );
      average += box[i];
    }
    average /= n;
    for( int i = 0 ; i < n ; i++ )
      count += abs(average - box[i]);
    count /= 2;
    printf( "Set #%d\nThe minimum number of moves is %d.\n\n", setnum++, count );
  }
  return 0;
}

#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;
}
共 37 頁 1 34 35 36 37