#UVa:264-Count on Cantor

找出n所在的那排斜排編號以及該斜排的最末項編號,並找出此兩編號與第n項的分母分子的關係即可得解。

C++(0.012)

/*******************************************************/
/* UVa 264 Count on Cantor                             */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2012/03/27                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  int n;
  int slash, term;
  int part1, part2;

  while( scanf( "%d", &n ) != EOF ){
    slash = 1;
    term = 1;
    while( term < n ) term += ++slash;

    part1 = 1 + term - n;
    part2 = slash - part1 + 1;

    printf( "TERM %d IS ", n );
    if( slash % 2 ) printf( "%d/%d\n", part1, part2 );
    else printf( "%d/%d\n", part2, part1 );
  }
  return 0;
}

沒有迴響

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

發表迴響