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的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. Android ListView动态改变Item高度

    在adapter的getView方法中进行设置,代码如下 @Override public View getView(int position, View convertView, ViewGroup ...

  2. SliverLight(how to show data point on the column series)

    You should know that Silverlight comes with win form drawing software is different, it has no the la ...

  3. pip在windows域下使用代理安装package方法

    首先说明下,本人在公司使用windows域账户代理上网,用pip在线安装package 返回ProxyError,类似Tunnel connection failed: 407 authenticat ...

  4. JavaScript语言标识符和保留字

    任何一种计算机语言都离不开标识符和保留字,下面我们将详细介绍JavaScript标识符和关键字.标识符      标识符就是给变量.函数和对象等指定的名字.构成标识符的字母是有一定的规范,JavaSc ...

  5. (转)Yale CAS + .net Client 实现 SSO(1)

    由于信息系统集成需要,最近研究了一下CAS.从网上找了不少资料,很多是针对Java平台的,为数不多的针对.net Client的文章往往片面的介绍某个方面,照着去做确会遇到大量的问题,特别是“重定向循 ...

  6. javascript之Array基础篇

    整理了 Array 中很基础的要掌握的知识点,希望可以帮助初学者,也希望自己以后多用多融会贯通. 创建数组 使用Array构造函数: var a=new Array();//创建一个空数组 var a ...

  7. 升级ionic版本后,创建新项目报Error Initializing app错误解决

    命令行,进入项目路径后,运行 ionic start myApp --v2 命令执行后,报如下错误 Installing npm packages...Error with start undefin ...

  8. Eclipse中tomcat之后,tomcat的相关配置会被Eclipse重置

    之前用MyEclipse,在tomcat的conf中修改了配置文件,启动就OK了. 现在改用Eclipse,发现改了,之后发现没有用,Eclipse重启tomcat之后,配置文件就被重置了. 众里寻他 ...

  9. div层遮盖flash(兼容浏览器)

    今天测试div层和flash的交互,发现div层总是被flash层遮盖,在百度上找了一会,说是加个<param name="wmode" value="transp ...

  10. xml操作

    一.LINQ to XML 编程基础 1.LINQ to XML类 System.Xml.Linq命名空间含有19个类,下表列出了它们的名称及其描述: 类 描述 XAttribute 表示一个 XML ...