[今日头条] 笔试题目2016/9/28
2016-09-29 11:11
459 查看
华电北风吹
天津大学认知计算与应用重点实验室
2016-09-29
String Shifting
字符串循环移位后有多少个跟原字符串相同。
解题思路:
当成字符串问题即可。KMP,BM等方法均可解决。
字典序:
题目描述:
给定整数n和m,将1到n的这n个整数按字典序排列之后,求其中的第m个数字。字典序排序规则和字符串排序规则一样。
参考代码:
天津大学认知计算与应用重点实验室
2016-09-29
String Shifting
字符串循环移位后有多少个跟原字符串相同。
解题思路:
当成字符串问题即可。KMP,BM等方法均可解决。
字典序:
题目描述:
给定整数n和m,将1到n的这n个整数按字典序排列之后,求其中的第m个数字。字典序排序规则和字符串排序规则一样。
参考代码:
#include <iostream> #include <string> #include <sstream> #include <string.h> #include <math.h> using namespace std; int result[20]; int CountBit(int n) { int result = 0; while (n) { n /= 10; result++; } return result; } int MaxBitNum(int n) { return n / pow(10, CountBit(n) - 1); } string Int2String(int n) { stringstream ss; string s; ss << n; ss >> s; return s; } string OutLengthNum(int x, int k) { string s = ""; while (x) { s = Int2String(x % 10) + s; x /= 10; } while (s.length()<k) { s = '0' + s; } return s; } void func(int n, int m) { int countNum = 0; int bitLength = CountBit(n); int maxBit = MaxBitNum(n); int i = 1, j = 1; for (; i < maxBit; i++) { for (j = 1; j <= bitLength; j++) { if (countNum + pow(10, j - 1) < m) { countNum += pow(10, j - 1); } else { cout << i; if (bitLength > 1) { cout << OutLengthNum(m - countNum - 1, bitLength - 1); } cout << endl; return; } } } for (j = 1; j < bitLength; j++) { if (countNum + pow(10, j - 1) < m) { countNum += pow(10, j - 1); } else { cout << maxBit; if (j > 1) { cout << OutLengthNum(m - countNum - 1, bitLength - 1); } cout << endl; return; } } for (int temp = n / 10 * 10; temp <= n; temp++) { countNum++; if (countNum == m) { cout << temp << endl; return; } } for (i = maxBit + 1; i <= 9; i++) { for (j = 1; j < bitLength; j++) { if (countNum + pow(10, j - 1) < m) { countNum += pow(10, j - 1); } else { cout << i; if (j > 1) { cout << OutLengthNum(m - countNum - 1, bitLength - 1); } cout << endl; return; } } } } int main() { memset(result, 0, sizeof(result)); int n, m; while(cin >> n >> m) func(n, m); return 0; }
相关文章推荐
- Trie树/字典树题目(2017今日头条笔试题:异或)
- 2018今日头条3.24笔试题目2
- 今日头条研发算法岗实习生笔试题题目篇_0324晚
- 今日头条2018春招研发岗笔试题目
- 今日头条 笔试题 (位运算)
- 2018今日头条内推笔试2
- 【牛客网】马三来刷题之回文解码(今日头条2017年客户端实习在线笔试题)
- 今日头条2018校招笔试题之字符串的问题
- 今日头条 异或 笔试题
- 2018春招-今日头条笔试题5题(后附大佬答案-c++版)
- 今日头条笔试
- 今日头条2017后端工程师实习生笔试题——最大映射
- 今日头条2017后端工程师实习生笔试题
- 今日头条2017暑期实习生招聘笔试编程题之函数最宽尖峰
- 2018春招-今日头条笔试题-第二题(python)
- 或与加(今日头条笔试)
- 今日头条笔试题(一)
- 今日头条笔试之手串问题
- 今日头条2018笔试第二题
- 2017-今日头条笔试题