应聘C语言类工作职位面试题大汇总9

本文介绍了一个用于寻找数组中第k大元素及其位置的C++程序,并提供了使用快速排序算法的具体实现代码。此外,还展示了几个常见的程序错误案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写一段程序,找出数组中第k大小的数,输出数所在的位置。例如{2,4,3,4,7}中,第一大的数是7,位置在4。第二大、第三大的数都是4,位置在1、 3随便输出哪一个均可。函数接口为:int find_orderk(const int* narry,const int n,const int k)

  要求算法复杂度不能是O(n^2)

  谢谢!

  可以先用快速排序进行排序,其中用另外一个进行地址查找

  代码如下,在VC++6.0运行通过。给分吧^-^

  //快速排序

  #include

  usingnamespacestd;

  intPartition (int*L,intlow,int high)

  {

  inttemp = L[low];

  intpt = L[low];

  while (low < high)

  {

  while (low < high && L[high] >= pt)

  --high;

  L[low] = L[high];

  while (low < high && L[low] <= pt)

  ++low;

  L[low] = temp;

  }

  L[low] = temp;

  returnlow;

  }

  voidQSort (int*L,intlow,int high)

  {

  if (low < high)

  {

  intpl = Partition (L,low,high);

  QSort (L,low,pl - 1);

  QSort (L,pl + 1,high);

  }

  }

  intmain ()

  {

  intnarry[100],addr[100];

  intsum = 1,t;

  cout << "Input number:" << endl;

  cin >> t;

  while (t != -1)

  {

  narry[sum] = t;

  addr[sum - 1] = t;

  sum++;

  cin >> t;

  }

  sum -= 1;

  QSort (narry,1,sum);

  for (int i = 1; i <= sum;i++)

  cout << narry[i] << '/t';

  cout << endl;

  intk;

  cout << "Please input place you want:" << endl;

  cin >> k;

  intaa = 1;

  intkk = 0;

  for (;;)

  {

  if (aa == k)

  break;

  if (narry[kk] != narry[kk + 1])

  {

  aa += 1;

  kk++;

  }

  }

  cout << "The NO." << k << "number is:" << narry[sum - kk] << endl;

  cout << "And it's place is:" ;

  for (i = 0;i < sum;i++)

  {

  if (addr[i] == narry[sum - kk])

  cout << i << '/t';

  }

  return0;

  }

  1、找错

  Void test1()

  {

  char string[10];

  char* str1="0123456789";

  strcpy(string, str1);// 溢出,应该包括一个存放'/0'的字符string[11]

  }

  Void test2()

  {

  char string[10], str1[10];

  for(I=0; I<10;I++)

  {

  str1[i] ='a';

  }

  strcpy(string, str1);// I,i没有声明。

  }

  Void test3(char* str1)

  {

  char string[10];

  if(strlen(str1)<=10)// 改成<10,字符溢出,将strlen改为sizeof也可以

  {

  strcpy(string, str1);

  }

  }

  2.

  void g(int**);

  int main()

  {

  int line[10],i;

  int *p=line; //p是地址的地址

  for (i=0;i<10;i++)

  {

  *p=i;

  g(&p);//数组对应的值加1

  }

  for(i=0;i<10;i++)

  printf("%d/n",line[i]);

  return 0;

  }

  void g(int**p)

  {

  (**p)++;

  (*p)++;// 无效

  }

  输出:

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

 3. 写出程序运行结果

  int sum(int a)

  {

  auto int c=0;

  static int b=3;

  c+=1;

  b+=2;

  return(a+b+c);

  }

  void main()

  {

  int I;

  int a=2;

  for(I=0;I<5;I++)

  {

  printf("%d,", sum(a));

  }

  }

  // static会保存上次结果,记住这一点,剩下的自己写

  输出:8,10,12,14,16,

  4.

  int func(int a)

  {

  int b;

  switch(a)

  {

  case 1: 30;

  case 2: 20;

  case 3: 16;

  default: 0

  }

  return b;

  }

  则func(1)=?

  // b定义后就没有赋值。

  5:

  int a[3];

  a[0]=0; a[1]=1; a[2]=2;

  int *p, *q;

  p=a;

  q=&a[2];

  则a[q-p]=a[2]

  解释:指针一次移动一个int但计数为1

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值