Careercup - Microsoft面试题 - 6543214668414976
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的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- Careercup - Microsoft面试题 - 24308662
2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
随机推荐
- objective-C运算符和表达式
运算符可以分为以下几种: 算术运算符:+,-,*,/,%,++,—-. 关系运算符:<,>,<=,>=,==,!= 布尔逻辑运算符:!,&&,|| 位运算符:| ...
- 返回json格式时间,解析时间
传入:Json格式的时间 JS如下: yyyy-M(MM)-d(dd) H(HH):m(mm):s(ss) function timeStamp2String(time) { var data=tim ...
- [leetcode]_Reverse Integer
经历了三道树的题后,完全崩溃中,急需一道非树图的题来挽救信心. 题目:反转数字.input : 123 , output : 321. 思路:直接,没什么好说的. 自己代码:很龊,有大量的冗余信息,还 ...
- 重拾C,一天一点点_12
连续两天没写了,今天继续! sizeof 对象 或 sizeof (类型名) 返回一个整型值,等于指定对象或类型占用的存储空间字节数.(返回值是无符号整型值,其类型为size_t,在头文件<st ...
- Java实现抽奖游戏
代码如下: import java.io.*; public class PresentDemo { /** * @param args */ public static void main(Stri ...
- 新浪微博登录界面上下拉伸图片--第三方开源--PullToZoomListViewEx(二)
这是PullZoomView在ScrollView实现,Android PullZoomView在ScrollView的实现是:PullToZoomScrollViewEx 下载地址:https:// ...
- (转)Android属性设置android:noHistory="true"
设置 android:noHistory="true"后,该Activity在statck中不留历史痕迹.默认的值是false. 举例说明,假设有三个Activity分别是:A,B ...
- 通过Messenger与后台连接(单向操作,activity向service发送数据)
xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- 3.第一个python程序
学习任何一门语言的第一步,首先要写个'hello world',这算是程序员的一个传统.但在写之前,还有注意几个问题. 首先,python是一门脚本语言,而脚本语言的特点就是:我们写的代码会先由解释器 ...
- java的基本数据类型特征
java的数据类型分为基本数据类型和引用数据类型. 基本数据类型分为数值型.字符型(char).布尔型(boolean) 数值型变量 1.整数型 类型 占用存储空间 表示范围 byte 1字节Byte ...