12
返回列表 发新帖
楼主: douya
跳转到指定楼层
上一主题 下一主题
收起左侧

hbk oa

🔗
ydxdad 2015-9-2 14:07:12 | 只看该作者
全局:
douya 发表于 2015-8-30 12:24
就按我说的那么做就好,他只有一个testcase,不大

谢谢了。。。顺便update一下,我onsite已挂,OA的时候没考到这个题。不过权当刷题自己做了下,没按你说的做,稍微麻烦了点,不过可能快那么一点
  1. #include <iostream>
  2. #include <climits>
  3. #include <string>
  4. using namespace std;

  5. /**
  6. * algorithm class to find the number with:
  7. * 1) base of 17 representation is palindrome
  8. * 2) base of 2 representation has 17 consective 1 bits
  9. */
  10. class findNextPNumber {
  11. public:
  12. /**
  13.   * @string input, input string representing a number using 17 as base
  14.   *       which has p form
  15.   * @return unsigned long long, return the next 17 based palidoromed number
  16.   *           which has at least 17 consecutive 1 bits
  17.   */
  18. unsigned long long findNextPNumber17Bits(const string& input) {
  19.   string cur = input;
  20.   string next;
  21.   while (1) {
  22.    next = nextPNumber(cur);
  23.    unsigned long long num = decode(next);
  24.    if (num == ULLONG_MAX) //
  25.     return 0;
  26.    if (count_consecutive_ones(num) >= 17) {
  27.     return num;
  28.    }
  29.    cur = next;
  30.   }
  31.   return 0;
  32. }
  33. private:
  34. /**
  35.   * Test if an input string contains all 'G's (16)
  36.   * if all is 'G', return true
  37.   * else return false
  38.   */
  39. bool isAll16s(const string &num) {
  40.   int n = num.size();
  41.   for (int i = 0; i < n; i++) {
  42.    if (num[i] != 'g')
  43.     return false;
  44.   }
  45.   return true;
  46. }
  47. ;
  48. /**
  49.   *  input a string with 17 as base and output
  50.   *  the value as 10 base
  51.   */
  52. unsigned long long decode(const string& in) {
  53.   unsigned long long result = 0;
  54.   int n = in.size();
  55.   for (int i = 0; i < n; i++) {
  56.    if (isdigit(in[i]))
  57.     result = result * 17 + in[i] - '0';
  58.    else
  59.     result = result * 17 + (10 + in[i] - 'a');
  60.   }
  61.   return result;
  62. }
  63. ;

  64. /**
  65.   *  count the maximum consecutive bits of 1s
  66.   */
  67. int count_consecutive_ones(unsigned long long input) {
  68.   int count = 0;
  69.   while (input) {
  70.    input = (input & (input << 1));
  71.    count++;
  72.   }
  73.   return count;
  74. }

  75. /**
  76.   * generate next p number
  77.   */
  78. string nextPNumber(const string& num) {
  79.   if (isAll16s(num))
  80.    return '1' + string(num.size(), '0') + '1';
  81.   int n = num.size();
  82.   int mid = n / 2;
  83.   int carry = 1;
  84.   int i = mid - 1;
  85.   int nums[n];
  86.   // decode of special chars
  87.   for (int i = 0; i < n; i++) {
  88.    if (isdigit(num[i]))
  89.     nums[i] = num[i] - '0';
  90.    else
  91.     nums[i] = 10 + num[i] - 'a';
  92.   }
  93.   int j;
  94.   // If there are odd digits, then increment
  95.   // the middle digit and store the carry
  96.   if (n % 2 == 1) {
  97.    nums[mid] += carry;
  98.    carry = nums[mid] / 17;
  99.    nums[mid] %= 17;
  100.    j = mid + 1;
  101.   } else
  102.    j = mid;

  103.   // Add 1 to the rightmost digit of the left side, propagate the carry
  104.   // towards MSB digit and simultaneously copying mirror of the left side
  105.   // to the right side.
  106.   while (i >= 0) {
  107.    nums[i] += carry;
  108.    carry = nums[i] / 17;
  109.    nums[i] %= 17;
  110.    nums[j++] = nums[i--]; // copy mirror to right
  111.   }
  112.   // encode to special chars
  113.   string result;
  114.   for (int i = 0; i < n; i++) {
  115.    if (nums[i] < 10)
  116.     result += (nums[i] + '0');
  117.    else
  118.     result += (nums[i] - 10 + 'a');
  119.   }
  120.   return result;
  121. }
  122. ;

  123. };
  124. int main() {
  125. string cur = "19b91";
  126. findNextPNumber solver;
  127. cout << solver.findNextPNumber17Bits(cur);
  128. return 0;
  129. }
复制代码
回复

使用道具 举报

🔗
 楼主| douya 2015-9-3 00:37:28 | 只看该作者
全局:
ydxdad 发表于 2015-9-2 14:07
谢谢了。。。顺便update一下,我onsite已挂,OA的时候没考到这个题。不过权当刷题自己做了下,没按你说的 ...

嗯嗯,加油!我之前onsite也跪了,挺奇怪的公司
回复

使用道具 举报

🔗
liuchangyu1988 2015-12-11 10:36:46 | 只看该作者
全局:
xieqilu1989 发表于 2015-3-13 02:16
卧槽,居然这公司OA还有不同的题目,我的不是这个题目

请问你的是什么题目啊?能分享一下吗?我也马上要做他的OA了。
回复

使用道具 举报

🔗
liuchangyu1988 2015-12-11 10:50:01 | 只看该作者
全局:
请问楼主,那个testcase是那个数字啊,想自己写一下看看对不对。谢谢!
回复

使用道具 举报

🔗
liuchangyu1988 2015-12-11 11:03:31 | 只看该作者
全局:
ydxdad 发表于 2015-9-2 14:07
谢谢了。。。顺便update一下,我onsite已挂,OA的时候没考到这个题。不过权当刷题自己做了下,没按你说的 ...

请问你OA是什么题呀?能分享一下吗?谢谢
回复

使用道具 举报

🔗
MS_Joe 2016-1-10 05:08:47 | 只看该作者
全局:
请问这个“下一个具有这两个性质的数”,是说比这个大的数,在17-based里是palidromic的,在二进制里全是"1"的数吗?还是说在2进制*中间*至少有一段是带有17个1的,也就是说可以是11001111111111111111111111111111111111111100000这样的,只要中间有一段有连续17个1。还是说这两个性质可以是比如再19-based里是palindromic的,在2进制里有19个1?
回复

使用道具 举报

🔗
aiweiwei 2016-3-2 06:12:13 | 只看该作者
全局:
请问楼主你投的什么职位呀
回复

使用道具 举报

🔗
zql06job 2017-2-7 11:29:38 | 只看该作者
全局:
请问有最近的他家电面或者OA的题目吗? 还是题目一直没变??
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表