PKU :: 2273
問題概要
(1,2)という座標をR1C2のように表現された文字列がある。
Column(縦)を大文字英字の26進数で表現した文字列に直して、(Column,Row)の形にした文字列を求める
1,2,3..26,27... -> A,B,C..Z,AA...
R1C2 -> B1
R52C52 -> AZ52
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string str; while( cin>>str, str!="R0C0" ){ int r = 0; int c = 0; int i; for(i=1; str[i]!='C'; i++)r = 10*r + (str[i]-'0'); for(i++; i<str.size(); i++)c = 10*c + (str[i]-'0'); char ret[128]; i = 0; while( c>0 ){ ret[i++] = ((c-1)%26) + 'A'; c = (c-1)/26; } ret[i] = 0; reverse(ret,ret+i); cout << ret << r << endl; } return 0; }
use strict; use warnings; sub dec_to_n { my($dec,$n) = @_; my $str; while( int($dec)>0 ){ my $a = (($dec-1)%$n)+1; $dec = ($dec-1)/$n; $str .= chr($a-1+65); } return reverse($str); } for(<>){ chomp; if( $_ eq "R0C0" ){ last; } if(/^R(\d+)C(\d+)$/){ print dec_to_n($2,26).$1,$/; } }