2014-05-12 07:44

题目链接

原题:

Given an array having  unique integers, each lying within the range <x<, how do u sort it. U can load only  numbers at a time in memory.

题目:一个数组里有16000个不重复的整数,所有元素都在1和20000之间。如果你一次至多能在内存里放1000个整数,你要如何排序整个数组?

解法1:这个题目表述是不是有问题?数组本身就是内存的数据吧?内存装不下还叫数组?既然所有元素都是不重复的,就可以用位向量了。用位向量标记,就可以通过一次扫描来排序所有元素了。如果按照一个整数4字节的话,1000个整数有4000字节,总共32000个位,是足够覆盖数据范围的。如果按照16位整数来算,这个算法就不可行了。《编程珠玑》和《Cracking the Coding Interview》上都有类似的题目。内存限制是任何时候都要考虑的实际问题。如果资源总是无限的,这个世界也就没问题需要解决了。

代码:

 // http://www.careercup.com/question?id=23123665
// all numbers are unique.
#include <cstdio>
#include <cstring>
using namespace std; int main()
{
const int MAXN = ;
FILE *fin, *fout;
unsigned bit[MAXN >> ];
int i; memset(bit, , MAXN / ); fin = fopen("in.txt", "r");
while (!feof(fin)) {
fscanf(fin, "%u", &i);
bit[i >> ] |= ( << (i & ));
}
fclose(fin);
fin = nullptr; fout = fopen("out.txt", "w");
for (i = ; i < MAXN; ++i) {
if (bit[i >> ] & ( << (i & ))) {
fprintf(fout, "%u\n", i);
}
}
fclose(fout);
fout = nullptr; return ;
}

解法2:如果元素存在重复的话,就不能用位向量了。可以用数组来分段统计每个数据范围元素出现的个数,这样能通过多次扫描达到排序的效果。

代码:

 // http://www.careercup.com/question?id=23123665
// may contain duplicates.
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; int main()
{
const int MAXN = ;
const int n = ;
FILE *fin, *fout;
int count[n];
int i, j;
int offset; fin = fopen("in.txt", "r");
fout = fopen("out.txt", "w");
if (fin == nullptr || fout == nullptr) {
printf("File doesn't exist.\n");
exit();
} offset = ;
while (offset < MAXN) {
memset(count, , n * sizeof(int)); fseek(fin, , SEEK_SET);
while (!feof(fin)) {
fscanf(fin, "%d", &i);
if (i >= offset && i < offset + n) {
++count[i - offset];
}
} for (i = ; i < n; ++i) {
for (j = ; j < count[i]; ++j) {
fprintf(fout, "%d\n", i + offset);
}
} offset += n;
}
fclose(fin);
fin = nullptr;
fclose(fout);
fout = nullptr; return ;
}

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

  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. JAVA中日期格式转换各个字母代表含义

    G  Era 标志符  Text  AD  y  年  Year  1996; 96  M  年中的月份  Month  July; Jul; 07  w  年中的周数  Number  27  W  ...

  2. CSS文档优化

    首先了解下CSS的渲染逻辑,它是从标记的最后一位开始搜索的,例如:.myclass li a,首选它会遍历所有的<a>,然后看哪些<a>之前有<li>,然后再看哪些 ...

  3. koa源码分析

    最近项目要使用koa,所以提前学习一下,顺便看了koa框架的源码. 注:源码是koa2.x koa的源码很简洁,关键代码只有4个文件,当然还包括一些依赖npm包 const Koa = require ...

  4. 【extjs6学习笔记】1.16 初始: 关于主题

    打开app.json,里面有主题设置 主题说明 theme-base 这个包是所有其他主题的基础主题,是唯一没有父主题的主题. 它包含Ext JS组件和布局正常工作绝对必需的最低限度的一组CSS规则. ...

  5. 【MFC】获取文件大小的方法

    [转载]原文地址:http://blog.csdn.net/coderwu/article/details/5652056 MFC 下可以通过 CFileStatus 获取文件大小. ULONGLON ...

  6. 开发Maven插件

    Mojo: Maven plain Old Java Object 1.插件命名规则:maven-<yourplugin>-plugin是Maven的保留字段,不允许使用,我们可以用< ...

  7. windows 10 开启安卓模拟器蓝屏

    上周装了个vs2017 rc 本想试试 开发安卓怎么样,花了一晚上时间终于装好了. 第二天开夜神模拟器的时候蓝屏了.. 然后就逐个卸载VS2017的功能,先是卸了 自带的仿真程序,没用.然后全部卸载掉 ...

  8. 从.net到java,从基础架构到解决方案。

    这一年,职业生涯中的最大变化,是从.net到java的直接跨越,是从平台架构到解决方案的不断完善. 砥砺前行 初出茅庐,天下无敌.再学三年,寸步难行.很多时候不是别人太强,真的是自己太弱,却不自知. ...

  9. selenium模糊匹配控件

    起因:在查找一些控件时,可能控件的一些属性是变化的,那在匹配时需要进行模糊匹配,模糊匹配,使用xpath 定位方式有种: contains(属性名,字符串):使用文本匹配,功能很强大 starts-w ...

  10. Codeforces Round #323 (Div. 2) C GCD Table 582A (贪心)

    对角线上的元素就是a[i],而且在所在行和列中最大, 首先可以确定的是最大的元素一定是a[i]之一,这让人想到到了排序. 经过排序后,每次选最大的数字,如果不是之前更大数字的gcd,那么只能是a[i] ...