#UVa:10107-What is the Median?

數值一直由小到大插入陣列中,即可得其中位數。

C++(0.040)

/*******************************************************/
/* UVa 10107 What is the Median?                       */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/30                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  int N[10005], top = 0, temp;
  while( scanf( "%d", &N[top++] ) != EOF ){
    for( int i = 0 ; i < top ; i++ )
      if( N[top-1] > N[i] ){
        temp = N[top-1];
        for( int j = top-1 ; j > i ; j-- )
          N[j] = N[j-1];
        N[i] = temp;
        break;
      }
      if( top % 2 )
        printf( "%d\n", N[top/2] );
      else
        printf( "%d\n", (N[top/2]+N[top/2-1])/2 );
  }
  return 0;
}

#UVa:10082-WERTYU

先把鍵盤打成一個陣列,這樣比較好得解。

C++(0.012)

/*******************************************************/
/* UVa 10082 WERTYU                                    */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2018/06/02                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main(){
  const char keyboard[] = " `1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
  const int kblen = strlen(keyboard);
  char hash[256];
  string s;

  for(int i = 0 ; i < 256 ; ++i){
    hash[i] = i;
  }

  for( int i = 2 ; i < kblen ; ++i ){
    hash[keyboard[i]] = keyboard[i-1];
  }

  while(getline(cin, s)){
    for(int i = 0 ; i < s.length() ; i++){
      printf( "%c", hash[s[i]] );
    }
    printf( "\n" );
  }

  return 0;
}

#UVa:10071-Back to High School Physics

這題只要套公式即可得解。

P.S. 因為是一等速度運動,所以位移的公式就是 平均速度 * 經過的時間,而因為正好是要兩倍時間後的位移,所以此時的速度即為平均速度,再乘上2倍時間即得解。

C++(0.036)

/*******************************************************/
/* UVa 10071 Back to High School Physics               */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/29                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  int v, t;
  while( scanf( "%d%d", &v, &t ) != EOF ){
    printf( "%d\n", 2*v*t );
  }
  return 0;
}

#UVa:10062-Tell me the frequencies!

這題的話,我是先一個一個字看它的ASCII碼是多少,把陣列中它的ASCII碼那格加1,表示這個字多出現了一次。

我用了三個變數,一個存最大出現的ASCII碼,一個存最小出現的ASCII碼,一個存最多出現的次數。

然後我就從出現一次開始搜尋,直到最多出現的次數,每次搜尋都是從最大ASCII碼搜尋到最小ASCII碼這樣,搜尋到次數一樣就輸出。

(當然這題用Sort也是OK啦XD)

C++(0.036)

/*******************************************************/
/* UVa 10062 Tell me the frequencies!                  */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/29                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

int main(){
  string s;
  int min_ASCII, max_ASCII, max_count;
  bool blankline = 0;
  while( getline( cin , s ) ){
    if( blankline ) printf( "\n" );

    int ASCII[130] = {0};
    min_ASCII = 200;
    max_ASCII = 0;
    max_count = 0;

    for( int i = 0 ; i < s.length() ; i++ ){
      ASCII[(int)s[i]]++;
      min_ASCII = min( min_ASCII, (int)s[i] );
      max_ASCII = max( max_ASCII, (int)s[i] );
      max_count = max( max_count, ASCII[(int)s[i]] );
    }

    for( int i = 1 ; i <= max_count ; i++ )
      for( int j = max_ASCII ; j >= min_ASCII ; j-- )
        if( ASCII[j] == i )
          printf( "%d %d\n", j, i );
        blankline = 1;
  }
  return 0;
}

#UVa:10055-Hashmat the Brave Warrior

這題只要將兩個數絕對值相減即可,唯一的陷阱就是在於數值範圍,剛好到2^32,就連unsigned int也會爆,所以請用long long吧!

C++(0.136)

/*******************************************************/
/* UVa 10055 Hashmat the brave warrior                 */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/11/29                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  long long warrior1, warrior2;
  while( scanf( "%lld%lld", &warrior1, &warrior2 ) != EOF ){
    printf( "%lld\n", (warrior1 > warrior2)? warrior1-warrior2 : warrior2-warrior1 ); 
  }
  return 0;
}

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