Careercup - Facebook面试题 - 6139456847347712
2014-05-01 01:50
原题:
Microsoft Excel numbers cells as ... and after that AA, AB.... AAA, AAB...ZZZ and so on.
Given a number, convert it to that format and vice versa.
题目:微软的Office Excel对于每行每列的命名方式是1, 2, 3, ..., 26, AA, AB, ..., ZZ, AAA, ..., ZZZ。请写个函数完成这种“数字”和自然数的互相转换。
解法:数清楚每个长度“数字”的个数,然后分段转换即可。
代码:
// http://www.careercup.com/question?id=6139456847347712
#include <cstdio>
#include <iostream>
#include <string>
using namespace std; string intToString(int n)
{
string s = ""; if (n <= ) {
while (n > ) {
s.push_back(n % + '');
n /= ;
}
reverse(s.begin(), s.end());
return s;
} int exp;
int len; exp = ;
len = ;
while (n > exp) {
n -= exp;
++len;
exp *= ;
} --n;
for (int i = ; i < len; ++i) {
s.push_back(n % + 'A');
n /= ;
}
reverse(s.begin(), s.end()); return s;
} int stringToInt(const string &s)
{
int n = ;
int len = (int)s.length(); if (s[] >= '' && s[] <= '') {
sscanf(s.c_str(), "%d", &n);
return n;
} int exp = ; for (int i = ; i < len; ++i) {
n += exp;
exp *= ;
}
exp = ;
for (int i = ; i < len; ++i) {
exp = exp * + (s[i] - 'A');
}
n += exp;
++n; return n;
} int main()
{
int n;
string s; while (scanf("%d", &n) == && n > ) {
s = intToString(n);
n = stringToInt(s);
cout << n << ' ' << s << endl;
} return ;
}
Careercup - Facebook面试题 - 6139456847347712的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- 关于CSS的只言片语
这段时间做了一个简单的页面,借机又重温了一下CSS的相关知识,现总结一下: 工欲善其事必先利其器,让我们先做一点准备工作 1.在页面添加: <meta http-equiv="x-ua ...
- html使用空格对齐文本( ;&emsp;&ensp;)
字符以及HTML实体 描述以及说明 这是我们使用最多的空格,也就是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的(只算1个).要使用html实体表示才可累加. ...
- Sql Server CTE递归
WITH cte AS ( SELECT a.FNUMBER,a.FMATERIALID AS MainId,b.FMATERIALID AS ChileID,CAST(b.FMATERIALID A ...
- CSS之Document方法的使用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SQLSERVER 启用跨库查询脚本
启用Ad Hoc Distributed Queries的方法,执行下面的查询语句就可以了: exec sp_configure 'show advanced options',1reconfigur ...
- 在sql-server上建立mysql链接库
EXEC sp_addlinkedserver @server = N'RESUME_MYSQL_CONN', @srvproduct=N'MySQL ODBC 5.1 Driver', @provi ...
- C# 类构造函数赋值里属性与字段赋值注意项
public class Test { public Test(int age) { this.Age=age;//如果这里使用的是this.age=age;那么属性里的判断将不会执行 } priva ...
- QA在网站建设中的作用
在网站建设项目中,有一个团队负责产品测试并识别产品中的缺陷是很有意义的.问题在于,不应该只依赖这个团队来发现所有的缺陷,就像航空公司不能只依靠空乘人员确保飞机安全一样.这个观点的核心是一个简单的事实, ...
- 【学习笔记】【C语言】算术运算
C语言一共有34种运算符,包括了常见的加减乘除运算 1. 加法运算+ 除开能做加法运算,还能表示正号:+5.+90 2. 减法运算- 除开能做减法运算,还能表示符号:-10.-29 3. 乘法运算* ...
- C# 线程--第一单线程基础
概念 什么是进程? 当一个程序被打开运行时,它就是一个进程.在进程中包括线程,进程可以由一个或多个线程组成. 什么是线程? 线程是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC), ...