📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
回复: 10
跳转到指定楼层
上一主题 下一主题
收起左侧

新鲜Amazon面经(数数字)

全局:

2014(10-12月) 码农类General 本科 全职@amazon - 网上海投 - 技术电面  | | Fail |

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
哎 今天电面AMAZON, 遇到一题数数字 不知道是我太水了还是怎么了  感觉电面这题有点难, 地里大牛比较多 请大家讨论一下:
Write a program
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
觉得考虑怎么把数字说出来很蛋疼  10-19这些都不规律  各种case用用好几个switch  
考虑越多越头大

上一篇:Zillow 两轮电面 面经
下一篇:请问大家Amazon OA过后多久给消息啊?
🔗
rettyye3 2014-10-28 04:13:15 | 只看该作者
全局:
  1. string NUMS[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
  2.     "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

  3. string TENS[] = {"zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

  4. string thousand[] = {"zero", "thousand", "million", "billion", "trillion"};

  5. string func(int x)
  6. {
  7.     int hundreds = x/100;
  8.     int res = x%100;
  9.     int tens = x%100/10;
  10.     int ones = x%10;
  11.     string result = "";
  12.     if(hundreds != 0) result = NUMS[hundreds]+" hundred";
  13.     if(res != 0)
  14.     {
  15.         if(result != "") result = result + " ";
  16.         if(res < 20)
  17.             result = result + NUMS[res];
  18.         else
  19.             result = result + TENS[tens] + "-" + NUMS[ones];
  20.     }
  21.     return result;
  22. }

  23. string translate(int x)
  24. {
  25.     string result = "";
  26.     if(x == 0) result = "zero";
  27.     else
  28.     {
  29.         int i = 0;
  30.         while(x != 0)
  31.         {
  32.             int m = x%1000;
  33.             if(m != 0)
  34.             {
  35.                 if(result != "") result = " " + result;
  36.                 if(i == 0)
  37.                     result = func(m) + result;
  38.                 else
  39.                     result = func(m) + " " + thousand[i]  + result;
  40.             }        
  41.             i ++;
  42.             x = x/1000;
  43.         }
  44.     }
  45.     return result;
  46. }
复制代码
目前考虑到的corner case就是0, 需要输出zero, 不知道还有没有其他复杂的corner case, 其他是要考虑负数? 小数?
回复

使用道具 举报

🔗
 楼主| yyboyz 2014-10-28 04:40:44 | 只看该作者
全局:
rettyye3 发表于 2014-10-28 04:13
目前考虑到的corner case就是0, 需要输出zero, 不知道还有没有其他复杂的corner case, 其他是要考虑负数?  ...

不考虑负数和小数  你这代码能运行吗?
回复

使用道具 举报

🔗
rettyye3 2014-10-28 05:15:29 | 只看该作者
全局:
yyboyz 发表于 2014-10-28 04:40
不考虑负数和小数  你这代码能运行吗?

可以啊... 我本地跑过了, 有什么问题吗?
回复

使用道具 举报

🔗
hydejyy 2014-10-28 05:19:31 | 只看该作者
全局:
CTCI 17.7 刚刷到
回复

使用道具 举报

🔗
 楼主| yyboyz 2014-10-28 05:27:55 | 只看该作者
全局:
居然是原题...What the f***..
回复

使用道具 举报

🔗
lixiang.xjtu 2014-10-28 05:34:08 | 只看该作者
全局:
这题尼玛还能再无聊点吗。我特地没看这题,因为我觉得太无聊了
回复

使用道具 举报

🔗
owenwilder 2014-10-28 05:51:29 | 只看该作者
全局:
原题啊。这只要取余做各种判断就成吧。不过确实很麻烦,电话考这题确实不好写。
回复

使用道具 举报

🔗
王可雪 2014-10-28 06:24:52 | 只看该作者
全局:
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. using std::string;

  5. const string dict[] = {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen", "twenty"};

  6. class SpeakNum{
  7. public:
  8.   string speakNum(int n) {
  9.     string ans;
  10.     if (n < 0) {
  11.       ans += "minus ";
  12.       n = -n;
  13.     }
  14.     if (n / 1000000) {
  15.       if (ans.empty())
  16.                ans += " ";
  17.       ans += speakNum(n / 1000000);
  18.       ans += "million";
  19.     }
  20.     n %= 1000000;
  21.     if (n / 1000) {
  22.       if (!ans.empty())
  23.                ans += " ";
  24.       ans += speakNum(n / 1000);
  25.       ans += " thousand";
  26.     }
  27.     n %= 1000;
  28.     if (n / 100) {
  29.       if (!ans.empty())
  30.                ans += " ";
  31.       ans += dict[n / 100];
  32.       ans += " hundred";
  33.     }
  34.     n %= 100;
  35.     if (n <= 20) {
  36.       if (!ans.empty())
  37.                ans += " ";
  38.       ans += dict[n];
  39.     }
  40.     else {
  41.       if (!ans.empty())
  42.                ans += " ";
  43.       ans += dict[10+n/10];
  44.       ans.replace(ans.end()-3, ans.end(), "y");
  45.       if (n / 10) {
  46.            if (!ans.empty())
  47.                ans += " ";
  48.                ans += dict[n % 10];
  49.       }
  50.     }
  51.     return ans;
  52.   }
  53.   
  54. };
复制代码
回复

使用道具 举报

🔗
MYcolting 2014-11-9 10:17:46 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

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

本版积分规则

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