#UVa:10673-Play with Floor and Ceil

首先,要先知道兩個括號的意義:

  • ┌ ┐ => 取無條件進位(即是取ceil值) Ex. ┌1/3┐ = 1
  • └ ┘ => 取無條件捨去(即是取floor值) Ex. └1/3┘ = 0

接著p和q就刷過各種可能即可得解。

C++(0.376)

/*******************************************************/
/* UVa 10673 Play with Floor and Ceil                  */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2011/12/17                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

long long div( long long x, long long k, double (*func)(double) ){
  return (long long)func( (double)x/(double)k );
}

int main(){
  int n;
  long long x, k, p, q, temp;
  bool answerp;
  while( scanf( "%d", &n ) != EOF ){
    for( int i = 0 ; i < n ; i++ ){
      scanf( "%lld%lld", &x, &k );
      answerp = 0;
      for( p = 0 ; !answerp ; p++ )
        for( q = 0 ; !answerp ; q++ ){
          temp = p*div(x,k,floor)+q*div(x,k,ceil);
          if( temp == x )
            answerp = 1;
          else if( temp > x )
            break;
        }
      printf( "%lld %lld\n", --p, --q );
    }
  }
  return 0;
}

沒有迴響

本文還沒有迴響,快來搶頭香!

發表迴響