Codeforces 1B. Spreadsheets
2014-12-07 13:24
351 查看
B. Spreadsheets
time limit per test
10 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA,
28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.
The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.
Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.
Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
Input
The first line of the input contains integer number n (1 ≤ n ≤ 105),
the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells
with the column and/or the row numbers larger than 106.
Output
Write n lines, each line should contain a cell coordinates in the other numeration system.
Sample test(s)
input
output
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
bool Check(string &str) // false system 1, true system 2
{
if(str[0] != 'R')
return false;
if(!isdigit(str[1]))
return false;
for(int i = 2; i < str.size(); ++i)
{
if(str[i] == 'C')
return true;
}
return false;
}
void ConvertTo1(string &str)
{
int pos = 2;
while(str[pos] != 'C')
{
++pos;
}
string col(str.begin() + 1, str.begin() + pos);
string b(str.begin() + pos + 1, str.end());
int row = atoi(b.c_str());
vector<int> ans;
while(row)
{
row = row - 1;
ans.push_back(row % 26);
row /= 26;
}
for(int i = ans.size() - 1; i >= 0; --i)
{
printf("%c", 'A' + ans[i]);
}
printf("%s", col.c_str());
}
void ConvertTo2(string &str)
{
int row = 0, pos = 0;
while(!isdigit(str[pos]))
{
row = row * 26 + str[pos] - 'A' + 1;
++pos;
}
string col(str.begin() + pos, str.end());
printf("R%sC%d", col.c_str(), row);
}
int main()
{
//freopen("F://input.txt", "r", stdin);
int n;
scanf("%d", &n);
while(n--)
{
string str;
cin >> str;
if(Check(str))
ConvertTo1(str);
else
ConvertTo2(str);
printf("\n");
}
return 0;
}
time limit per test
10 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA,
28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.
The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.
Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.
Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
Input
The first line of the input contains integer number n (1 ≤ n ≤ 105),
the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells
with the column and/or the row numbers larger than 106.
Output
Write n lines, each line should contain a cell coordinates in the other numeration system.
Sample test(s)
input
2 R23C55 BC23
output
BC23 R23C55
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
bool Check(string &str) // false system 1, true system 2
{
if(str[0] != 'R')
return false;
if(!isdigit(str[1]))
return false;
for(int i = 2; i < str.size(); ++i)
{
if(str[i] == 'C')
return true;
}
return false;
}
void ConvertTo1(string &str)
{
int pos = 2;
while(str[pos] != 'C')
{
++pos;
}
string col(str.begin() + 1, str.begin() + pos);
string b(str.begin() + pos + 1, str.end());
int row = atoi(b.c_str());
vector<int> ans;
while(row)
{
row = row - 1;
ans.push_back(row % 26);
row /= 26;
}
for(int i = ans.size() - 1; i >= 0; --i)
{
printf("%c", 'A' + ans[i]);
}
printf("%s", col.c_str());
}
void ConvertTo2(string &str)
{
int row = 0, pos = 0;
while(!isdigit(str[pos]))
{
row = row * 26 + str[pos] - 'A' + 1;
++pos;
}
string col(str.begin() + pos, str.end());
printf("R%sC%d", col.c_str(), row);
}
int main()
{
//freopen("F://input.txt", "r", stdin);
int n;
scanf("%d", &n);
while(n--)
{
string str;
cin >> str;
if(Check(str))
ConvertTo1(str);
else
ConvertTo2(str);
printf("\n");
}
return 0;
}
相关文章推荐
- 暑期集训Day2训练赛D题Spreadsheets(codeforces 1B)
- Codeforces 1B. Spreadsheets
- CodeForces 1B. Spreadsheets(模拟)
- Codeforces_1B_Spreadsheets
- codeforces-1B. Spreadsheets
- 【codeforces 1B】Spreadsheets——模拟,水
- codeforces_1B Spreadsheets
- codeforces--Spreadsheets(模拟)
- codeforces--Spreadsheets(模拟)
- codeforces-1B-Spreadsheets( C++ && PHP && 字符串操作 && 数学逻辑 )
- codeforces - 1B - Spreadsheets
- CODEFORCES:1B. Spreadsheets
- codeforces 1B Spreadsheets 解题报告
- Codeforces 486B - OR in Matrix
- Codeforces 166E Tetrahedron(dp)
- CodeForces 621B Wet Shark and Bishops
- Codeforces 225C Barcode DP
- Codeforces 835C - Star sky 【预处理前缀和】
- codeforces 931E Logical Expression dp
- codeforces--279--