nyist 303 序号互换
生活随笔
收集整理的這篇文章主要介紹了
nyist 303 序号互换
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
序號(hào)互換
時(shí)間限制:1000 ms ?|? 內(nèi)存限制:65535 KB 難度:2 描述Dr.Kong設(shè)計(jì)了一個(gè)聰明的機(jī)器人卡多,卡多會(huì)對(duì)電子表格中的單元格坐標(biāo)快速計(jì)算出來(lái)。單元格的行坐標(biāo)是由數(shù)字編號(hào)的數(shù)字序號(hào),而列坐標(biāo)使用字母序號(hào)。觀察字母序號(hào),發(fā)現(xiàn)第1列到第26列的字母序號(hào)分別為A,B,…,Z,接著,第27列序號(hào)為AA,第28列為AB,依此類推。
若給Dr.Kong的機(jī)器人卡多一個(gè)數(shù)字序號(hào)(比如32),它能很快算出等價(jià)的字母序號(hào)(即AF),若給機(jī)器人一個(gè)字母序號(hào)(比如AA)),它也能很快算出等價(jià)的數(shù)字序號(hào)(27),你能不能與卡多比試比試,看誰(shuí)能算得更快更準(zhǔn)確。
接下來(lái)有N行, 每行或者是一個(gè)正整數(shù),或者是一個(gè)僅由大寫字母組成的字符串。
輸入保證,所有數(shù)字序號(hào)和字母序號(hào)對(duì)應(yīng)的數(shù)字序號(hào)均 ≤ 2*10^9
代碼:
#include <stdio.h>
#include <string.h>
#include <math.h>
int a[10];
int char_int(char ch[])
{
??? int i = 0;
??? int sum = 0;
??? for(i = 0; i < strlen(ch); i++)
????? {
????????? sum = sum * 10 + ( (int)ch[i] - 48);
????? }
??? return sum;
}
void int_print(char ch[])
{
??? int x = char_int(ch);
??? int i = 0;
??? while(x)
??? {
??????? if(x%26 != 0)
?? ??? ?{
?? ??? ??? ?a[i] = x % 26;
?? ??? ??? ?x /= 26;
??????? }
?? ??? ?else
?? ??? ?{
?? ??? ??? ?a[i] = 26;
?? ??? ??? ?x = x/26 - 1;
?? ??? ?}
??????? ++i;
??? }
? int j = 0;
? for(j = i-1; j>=0; j--)
?? {
??????????? printf("%c",a[j] + 64);
?? }
?? printf("\n");
}
void char_print(char ch[])
{
??? int i;
??? int k = 0;
??? int sum = 0;
??? for(i = strlen(ch)-1; i >= 0; i--)
??????? {
??????????? sum += ((int)ch[i]-64) * pow(26,k);
??????????? k++;
??????? }
???????? printf("%d\n",sum);
}
int main()
{
??? int n;
??? scanf("%d",&n);
??? while(n--)
??? {
??????? char ch[20];
??????? scanf("%s",ch);
??????? if(ch[0]>='A' && ch[0]<='Z')
??????????? char_print(ch);
??????? else
??????????? int_print(ch);
??? }
??? return 0;
}
字符轉(zhuǎn)數(shù)字很簡(jiǎn)單,那個(gè)字符轉(zhuǎn)數(shù)字就是考慮一下那個(gè)最后一個(gè)是Z的情況這樣就好解決了
轉(zhuǎn)載于:https://www.cnblogs.com/yyroom/archive/2013/03/02/2940516.html
總結(jié)
以上是生活随笔為你收集整理的nyist 303 序号互换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《超越想象——Windows_8应用设计
- 下一篇: 律理(四)----关于“用韵的疏密和宽严