# C++面试题一大波

594人阅读 评论(0)

//打印1到最大的n位数。
//题目：输入数字n，按顺序打印出从1到最大的n位十进制数。比如：
//输入3，则打印出1、2、3一直到最大的3位数999.
//[陷阱]：这个题目很容易想到的办法就是先求出最大的数。然后循环输出就可以了。
#include <iostream>
#include <string.h>
using namespace std;
void Grial(char *str, char *s)
{
if (*s == '\0')
{
cout << str << endl;
return;
}
else
{
for (int i = 0; i <= 9; i++)
{
Grial(str,s+1);
*s = '1' + i;
}
}
}
void Grial(int x)
{
char *s = new char[x];
memset(s,'0',sizeof(s));
*(s + x) = '\0';
char *str = s;
Grial(str, s);
}
int main()
{
Grial(2);
return 0;
}
#include <iostream>
using namespace std;

void Grial(char *str,int n)
{
int count = 1;
char *p = str+n;
while (count != 0 && *str != '1')
{
if (*p - '0' + count >= 10)
{
*p = '0';
count = 1;
}
else
{
*p += count;
count = 0;
}
p--;
}
}
void Grial(int x)
{
char *str = new char[x + 1];
memset(str, '\0', sizeof(str));
for (int i = 0; i <= x; i++)
{
strcat(str,"0");//多开辟一位，用来作为终止判断条件。
}
char *p = str;
while (1)
{
p = str;
Grial(p,x);
p = str;
while (*p == '0')p++;
if (*str == '1' && p == str)break;//终止位置。
cout << p << endl;
}
}
int main()
{
Grial(3);
return 0;
}

#include <iostream>
using namespace std;
/*
3.数值的正数次方

*/

double GetSum(double base,int exponent)
{
double count = 1;
while (exponent)
{
count *= base;
exponent--;
}
return count;
}
double power(double base,int  exponent)
{
if (base == 0)return 0;
if (exponent == 0)
return 1;
double count = 1;
if (exponent > 0)
{
count = GetSum(base,exponent);
}
else
{
count = 1/GetSum(base, -exponent);
}
return count;
}
int main()
{
cout << power(2, -2) << endl;
return 0;
}

#include <iostream>
using namespace std;
//求数组中出现次数超过一半的数字。
int Grial(int a[], int low,int high,int M)
{
int i = low;
int j = high;
if (i >= j)return 0 ;
int key = a[i];
while (i < j)
{
while (i < j && a[j] > key)j--;
a[i] = a[j];
while (i < j && a[i] < key)i++;
a[j] = a[i];
if(i<j)
{
i++;
j--;
}
}
a[i] = key;
if (i >= M)
{
return a[i];
}
else if (i<M)
{
return Grial(a, i + 1, high, M);
}
else
{
return Grial(a, low, i - 1, M);
}
}
int main()
{
//int a[] = {0,1,1,2,1,1,2};
int a[] = { 0, 1, 1, 1, 1, 1, 1, 4, 2, 3, 4 };
int n = sizeof(a)/sizeof(int);
int mid = (n % 2 == 0) ? (n / 2 + 1) : n / 2;
cout << Grial(a,0,n-1,mid) << endl;
return 0;
}

#include <iostream>
using namespace std;
//求数组中出现次数超过一半的数字。
int Grial(int a[], int n)
{
int count = 0;
int val;
for (int i = 0; i < n; i++)
{
if (count == 0)
{
val = a[i];
}
if (val == a[i])
{
count++;
}
else
{
count--;
}
}
return val;
}
int main()
{
int a[] = { 0, 1, 1, 1, 1, 1, 1, 4, 2, 3, 4 };
cout << Grial(a, sizeof(a) / sizeof(int));
return 0;
}
/*

*/

/*#include <iostream>
using namespace std;
void Grial(int a[], int n)
{
int i = -1;
int j = 0;
while (j<n)
{
while (a[j] % 2 == 0)j++;
i++;
if (j == n)break;
if (i != j)
{
a[i] ^= a[j];
a[j] ^= a[i];
a[i] ^= a[j];
}
j++;
}
}
int main()
{
int a[] = { 4, 5, 3, 1, 4, 6, 7, 8, 0, 6, 5643, 5, 6,1 };
Grial(a, sizeof(a)/sizeof(int));
for (int i = 0; i < 14; i++)
{
cout << a[i] << "  ";
}
cout << endl;
return 0;
}
/*
3.数字在排序数组中出现的次数。

*/
#include <iostream>
using namespace std;

int Grial(int a[], int n,int val)
{
//二分查找。
int i = 0;
int j = n - 1;
int mid;
int count = 0;
while (i <= j)
{
mid = (i + j) / 2;
if (a[mid]>val)
{
j = mid - 1;
}
else if (a[mid] < val)
{
i = mid + 1;
}
else
{
i = mid - 1;
j = mid + 1;
count++;
while (a[i--] == val)count++;
while (a[j++] == val)count++;
return count;
}
}
return -1;
}
int main()
{
int a[] = { 1, 2, 3, 3, 3, 3, 4, 5 };
cout << Grial(a, sizeof(a) / sizeof(int),5) << endl;
return 0;
}

2
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：184085次
• 积分：4980
• 等级：
• 排名：第5632名
• 原创：291篇
• 转载：2篇
• 译文：0篇
• 评论：96条
文章分类