C++面试题一大波

//打印1到最大的n位数。
//题目:输入数字n。按顺序打印出从1到最大的n位十进制数。比方:
//输入3。则打印出1、2、3一直到最大的3位数999.
//[陷阱]:这个题目非常easy想到的办法就是先求出最大的数。然后循环输出就能够了。

#include <iostream> #include <string.h> using namespace std; void Grial(char *str, char *s) { if (*s == '') { 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) = ''; 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, '', 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 power(double base, int exponent),
求base的exponent次方。不得使用库函数。不须要考虑大数问题。

注意:考虑非法输入的返回。 */ 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.数字在排序数组中出现的次数。
题目:
统计一个数字在排序数组中出现的次数。比如:排序数组{1,2,3,3,3,3。4,5}
和数字3,因为3出现了4次。因此输出4.
*/
#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;
}
原文地址:https://www.cnblogs.com/bhlsheji/p/5257836.html