一道关于数组的面试题!!!

马哲 2008-05-29 07:31:22
数据a[n]中所存的数字,没有进行排序。另有一个数组b[m],且n>m.
问:不使用新的数组,将a[n]中的最小的数字,放入b[0],将第二小的数字放入b[1],以此类推。求解!
...全文
259 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyils168 2011-10-27
  • 打赏
  • 举报
回复
和在有n个元素的数组中找m个最小数,时间复杂度是O(n),放在数组b中,然后再对b用快排。
数据之巅 2008-06-01
  • 打赏
  • 举报
回复
呵呵,有意思!
游北亮 2008-05-30
  • 打赏
  • 举报
回复
Linq???
不错,代码更简单。
[Quote=引用 24 楼 zsxghost 的回复:]
C# codeint[] a = { 3, 2, 5, 4 };
int[] b = new int[4];
var m = from n in a orderby n select n;
int i = 0;
foreach (var n in m)
{
b[i] = n;
i++;
}
[/Quote]
zsxghost 2008-05-30
  • 打赏
  • 举报
回复
int[] a = { 3, 2, 5, 4 };
int[] b = new int[4];
var m = from n in a orderby n select n;
int i = 0;
foreach (var n in m)
{
b[i] = n;
i++;
}

zsxghost 2008-05-30
  • 打赏
  • 举报
回复
经典的弱智加垃圾试题,不符合程序员的工作范围。

太明显的错误了。
游北亮 2008-05-30
  • 打赏
  • 举报
回复
用了半小时,实现了这个目的,a数组不变,把a中的数字按小到大存入b,a中重复的数字不再存入b
using System;
using System.Data;
public class test
{
static void Main(string[] args)
{
Random r = new Random();
int[] a = new int[10];
for (int i = 0; i < 10; i++)
{
a[i] = r.Next(1, 10);
}
//int[] a = { 7,3,5,1,6,9,4,2,0,8 };
int[] b = new int[9];
WriteArray(a);
CopyArray(a, b);
WriteArray(a);
WriteArray(b);
}

static void CopyArray(int[] a, int[] b)
{
int tmp=int.MaxValue;
for(int m = 0, n = 0; m < a.Length; m++)// n用于b数组索引
{
for (int i = 0; i < a.Length; i++)
{
if(a[i] < tmp && !IsInArray(a[i],b))
tmp = a[i];
}
if(n < b.Length && tmp != int.MaxValue && (n == 0 || b[n-1] != tmp))
{
b[n] = tmp;
n++;
tmp = int.MaxValue;
}
}
}

static bool IsInArray(int m, int[] arr)
{
foreach (int i in arr)
if(i == m) return true;
return false;
}

static void WriteArray(int[] arr)
{
foreach (int i in arr)
Console.WriteLine(i);
Console.WriteLine();
}
}
游北亮 2008-05-30
  • 打赏
  • 举报
回复
楼主的意思是说,a数组的顺序不能变
huzige2008 2008-05-29
  • 打赏
  • 举报
回复
C++里是不是没有数组溢出这一说,好像当m=4时,b[4]仍可以取,取得就是b[3]存储单元的后面两个字符。
lion533335 2008-05-29
  • 打赏
  • 举报
回复
class Program
{
static void BubbleSort(int[] arr)
{
int temp;
for (int outer = arr.GetUpperBound(0)-1; outer >=1 ; outer--)
{
for (int inner = 0; inner <=outer-1; inner++)
{
if (arr[inner] > arr[inner + 1])
{
temp=arr[inner];
arr[inner]=arr[inner+1];
arr[inner+1]=temp;
}
}
DisplayArr(arr);
}
}
static void DisplayArr(int[] arr)
{
for (int i = 0; i < arr.GetUpperBound(0); i++)
Console.Write(arr[i]+ " ");
Console.WriteLine();
}
static void Main(string[] args)
{
int[] a;
int[] b;
a = new int[12] { 11, 32, 12, 42, 2, 7, 99, 4, 26, 72, 38, 20 };
b = new int[10] { 6, 22, 31, 4, 38, 78, 62, 16, 26, 81 };
BubbleSort(a);
DisplayArr(a);

for (int i = 0; i < b.GetUpperBound(0); i++)
{
b[i] = a[i];
}
Console.WriteLine("The result is:");
DisplayArr(b);
Console.ReadLine();
}
}
virusswb 2008-05-29
  • 打赏
  • 举报
回复
数据a[n]中所存的数字,没有进行排序。另有一个数组b[m],且n>m.
有点意思
比如说吧
a【4】{3,6,1,9}
b【3】{}
你怎么能都放入b中呢,少了一个位置啊,除非有重复,但是如果b更少呢,只有两个一个,那a就都一样了,好想不是这样的吧。
还是说放满b就算了,多余的a元素就不要了呢,那就排序吧,a排序,然后循环b的个数,放进去几个算几个,好想也不是这么白痴吧
我姓区不姓区 2008-05-29
  • 打赏
  • 举报
回复

static void Main(string[] args)
{
Random r = new Random();
int[] a = new int[10];
for (int i = 0; i < 10; i++)
{
a[i] = r.Next(1, 11);
Console.WriteLine(a[i]);
}
Console.WriteLine();
int[] b;
CopyArray(a, out b, 9);
foreach (int i in b)
Console.WriteLine(i);

}
static void CopyArray(int[] a, out int[] b, int blength)
{
b = new int[blength];
if (a.Length < b.Length)
return;
string s = "";
for (int i = 0; i < b.Length; i++)
{
b[i] = int.MaxValue;
int index = -1;
for (int j = 0; j < a.Length; j++)
{

if (a[j] <= b[i])
{
if (!s.Contains(j.ToString()))
{
b[i] = a[j];
index = j;
}
}
}
if (index != -1)
s += index + " ";
}
}
马哲 2008-05-29
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 ojlovecd 的回复:]
数组中会有重复的数据吗?
[/Quote]

这个倒是没有提,我想有可能有吧。
我姓区不姓区 2008-05-29
  • 打赏
  • 举报
回复
数组中会有重复的数据吗?
马哲 2008-05-29
  • 打赏
  • 举报
回复
如果:a[n]的顺序不能改变呢?
我姓区不姓区 2008-05-29
  • 打赏
  • 举报
回复
应该是b=new int [blength];改正一下
其实就是先将a排序再逐个赋值给b就行了
sz_free_bird 2008-05-29
  • 打赏
  • 举报
回复
C 经典的冒泡排序,知道吧?只借助一个临时变量就可以实现。
我姓区不姓区 2008-05-29
  • 打赏
  • 举报
回复

static void Main(string[] args)
{
Random r = new Random();
int[] a = new int[10];
for (int i = 0; i < 10; i++)
{
a[i] = r.Next(1, 10);
}
int[] b;
CopyArray(a, out b,9);
foreach (int i in b)
Console.WriteLine(i);
}

static void CopyArray(int[] a, out int[] b, int blength)
{
b = new int[9];
if (a.Length < b.Length)
return;
int t=0;
bool swap = false;
for (int i = 0; i < a.Length && !swap; i++)
{
swap = true;
for (int j = i + 1; j < a.Length; j++)
{
if (a[j] < a[i])
{
t = a[i];
a[i] = a[j];
a[j] = t;
swap = false;
}
}
}
for (int i = 0; i < b.Length; i++)
{
b[i] = a[i];
}
}

游北亮 2008-05-29
  • 打赏
  • 举报
回复
实现上,最简单的就是
从a中找出最小的,放到b中,然后从a中不存在于b中的元素中找出最小的放成b[1]

算法效率比较低而已
游北亮 2008-05-29
  • 打赏
  • 举报
回复
那么第a[M+1]以后的元素,存到哪里去?还是说丢弃?

[Quote=引用 6 楼 mznumber1 的回复:]
是N>M,确定,一定以及肯定。
[/Quote]
马哲 2008-05-29
  • 打赏
  • 举报
回复
哈...当时我也考虑了一下,怎么也想不通。
加载更多回复(6)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧