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 ...
随机推荐
- 【trim()】去掉字符串开头和结尾的空格,防止不必要的空格导致的错误。
去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String a=" abc"; Strin ...
- SERVER 2012 R2 core域环境下批量创建用户
Write by xiaoyang 转载请注明出处 步骤一:创建域 基本配置 1. 输入命令进入配置 2. 输入8进入网络配置 3. 选择要配置的网 ...
- C#DataTable 的一些操作经常操作
关于C# DataTable 的一些操作 经常操作DATATABLE 对于一些不需要再通过sql 来重复操作的 可以通过操作datatable来达到同样的效果 方法一: 也是广为人知的一种: Y ...
- 嵌入式 linux 移植修改后的libjpeg 实现内存中解码
1.修改libjpeg源码,使之实现内存解码. 修改libjpeg中读取或者输出jpeg文件的函数接口文件jdatadst.c和jdatasrc.c见下面这篇帖子. http://blog.163.c ...
- String中重要方法与字段
下列这段代码已全部包含了我要写的String类中重要的字段: //StringMisc.java// This program demonstrates the length, charAt and ...
- jQuery 简单漂亮的 Nav 导航菜单
自己写的一个简单的导航菜单,先看效果: 鼠标悬浮时菜单项向上移动成蓝底白字,点击之后底部会有蓝条表示当前选中项. 页面代码,菜单的每一项都是一个 div ,其中包括一个 ul 用来放置显示文字等,另一 ...
- upTodown
------->>> 从左图变为有图,并实现将左图上面的信息隐藏. <img src="../images/up.gif" border=&qu ...
- floodfill算法解题示例
Flood fill算法是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法.因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名.在GNU Go和扫雷中,Floo ...
- JS增删改HTML表格
要求如下: 写一个html页面,里面有一个表格,储存用户信息,包括:用户名,密码,姓名,邮箱,电话,qq,身份证号. 现在要通过js对表格进行动态的增删改查(只是内存操作即可): 首先,加载页面时用j ...
- centos6.5下的mysql5.6.30安装
1.解压mysql tar -xf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz -C /usr/local mv mysql-5.6.30-linux-gli ...