2014-05-11 02:56

题目链接

原题:

Write a function called FooBar that takes input integer n and prints all the numbers from  upto n in a new line. If the number is divisible by  then print "Foo", if the number is divisible by  then print "Bar" and if the number is divisible by both  and , print "FooBar". Otherwise just print the number.
for example FooBar() should print as follows: Foo Bar
Foo Foo
Bar Foo FooBar I know, easy right? ;)

题目:从1到n的整数,如果被3整除就输出Foo,如果被5整除就输出Bar,如果是公倍数就输出FooBar,否则直接输出原数。

解法:这题有什么陷阱?n可以是大数吗?n可以小于1吗?如果是实际面试,肯定要问清楚的。在此,我就按最简单的处理了。在这种题目上自找麻烦是没意义的。

代码:

 // http://www.careercup.com/question?id=6543214668414976
#include <iostream>
#include <sstream>
using namespace std; int main()
{
int n;
int i; while (cin >> n && n > ) {
for (i = ; i <= n; ++i) {
if (i % ) {
if (i % ) {
cout << i;
} else {
cout << "Bar";
}
} else {
if (i % ) {
cout << "Foo";
} else {
cout << "FooBar";
}
}
cout << endl;
}
} return ;
}

Careercup - Microsoft面试题 - 6543214668414976的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  4. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  5. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  6. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  7. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  8. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  9. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

随机推荐

  1. 第1部分: 游戏引擎介绍, 渲染和构造3D世界

    原文作者:Jake Simpson译者: 向海Email:GameWorldChina@myway.com ---------------------------------------------- ...

  2. C puzzles详解【9-12题】

    第九题 #include <stdio.h> int main() { float f=0.0f; int i; ;i<;i++) f = f + 0.1f; if(f == 1.0 ...

  3. ubuntu 字体 android stuido 汉字 显示 方块

    Ubuntu 12.04 LTS 中安装 windows 字体   ubuntu 中的中文字体看着总觉的有点不爽,于是百度了下,这里记录下怎么在 ubuntu 12.04 中安装 windows 字体 ...

  4. jquery 仿手机屏幕切换界面效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 自定义控件winfrom

    附件代码:http://files.cnblogs.com/files/qtiger/CompositeControl.zip 转载:http://www.cnblogs.com/bomo/archi ...

  6. winfrom 导入Excel表到access数据库(来自小抽奖系统)

    网上有很多这种方法,本人只是针对自己的系统来实现的 //导入excel表 private void ImportTSMenu_Click(object sender, EventArgs e) { O ...

  7. Linux redis 配置文件

    # Redis configuration file example # Note on units: when memory size is needed, it is possible to sp ...

  8. cassandra 之 jdbc 使用【java、scala】

    1.数据库创建 参考接上文cassandra入门 http://www.cnblogs.com/piaolingzxh/p/4197833.html 2.下载jdbc驱动源码,构建jar包 源码下载地 ...

  9. php spl

    最近在重构后台,自写rbac,发现自己在设计模式方面尤为欠缺,没有一个长远的目光,所以打算静下心来看一看自己平时不关注的功能,spl就是其中之一. spl是Standard PHP Library(P ...

  10. php验证手机号码

    大家都应该有这个常识,中国的手机号码都是以数字“1”开头,然后利用“0~9”10个数字组成的11位数字组合,那么我们的验证规则肯定要根据这个思路来写. 根据上面的简单思路,我们便可以写下以下的验证代码 ...