//打印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 == '\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 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;
}

C++面试题一大波的更多相关文章

  1. 万网知您所需,“域”众不同--.link/.love/.help等一大波新顶级域来袭!

    万网在新顶级域市场再次发力,一大波域名界的小鲜肉新鲜上线,价格优惠,限时低至9元起,更有丰富的可注册资源. 一下,即刻世界,用记录生活,用观看世界, 用和做最好的! 新上线的个性化新顶级域价格如下: ...

  2. Java实现 蓝桥杯 历届试题 斐波那契

    试题 历届试题 斐波那契 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 斐波那契数列大家都非常熟悉.它的定义是: f(x) = 1 - (x=1,2) f(x) = f(x-1) ...

  3. 算法笔记_173:历届试题 斐波那契(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 斐波那契数列大家都非常熟悉.它的定义是: f(x) = 1 .... (x=1,2) f(x) = f(x-1) + f(x-2) ... ...

  4. 剑指offer笔记面试题10----斐波那契数列

    题目:求斐波那契数列的第n项.写一个函数,输入n,求斐波那契数列的第n项.斐波那契数列的定义如下:f(0) = 0, f(1) = 1,f(n) = f(n - 1) + f(n - 2). 测试用例 ...

  5. 剑指offer-面试题10-斐波那契数列-递归循环

    /* 题目:求斐波那契数列的第n项 */ /* 思路: f(n) = 0 n=0, 1 n=1, f(n-1) + f(n-2) n>1 */ int Fibonacci(int n){ if( ...

  6. 自己动手,丰衣足食!一大波各式各样的ImageView来袭!

    工作略忙,一直想自己打造一个开源控件却苦于没有时间,可是这种事情如果不动手就会一直拖下去,于是最近抽时间做了个简单的自定义形状的ImageView控件. 时间紧迫,目前仅支持正六边形.圆形.菱形.椭圆 ...

  7. 【S】【S】【S】一大波前端干货整合(一)

      前端交流站点 大前端       http://www.daqianduan.com/ V2EX       http://www.v2ex.com/ W3cplus    http://www. ...

  8. 一大波Java来袭(二)异常处理

    概要解析: 本章的知识点能够记为:1图+5keyword+先逮小的.后逮大的  一.基础 (一)定义 1.异常 是指在程序执行的时候发生的一些异常事件.良好的程序设计应该在异常发生的时候提供处理异常的 ...

  9. 【转】一大波实用的 bash 别名和函数

    作为一个命令行探索者,你或许发现你自己一遍又一遍重复同样的命令.如果你总是用ssh进入到同一台电脑,如果你总是将一连串命令连接起来,如果你总是用同样的参数运行一个程序,你也许希望在这种不断的重复中为你 ...

随机推荐

  1. 打造坚固的安全的Linux服务器(ssh登录篇)

      Nov 3 01:22:06 server sshd[11879]: Failed password for root from 123.127.5.131 port 38917 ssh2Nov ...

  2. C++箴言:理解 new-handler的行为

    当 operator new 不能满足一个内存分配请求时,它抛出一个 exception(异常).很久以前,他返回一个 null pointer(空指针),而一些比较老的编译器还在这样做.你依然能达到 ...

  3. nginx+redis 实现 jsp页面缓存,提升系统吞吐率

    最近在开发的时候,发现之前APP客户端的一部分页面用的是webview交互,这些页面请求很多,打开一套试卷,将会产生100+的请求量,导致系统性能下降.于是考虑在最靠近客户端的Nginx服务器上做Re ...

  4. C语言入门(16)——C语言的数组

    和结构体类似,数组也是一种复合数据类型,它由一系列相同类型的元素组成.C语言支持一维数组和多维数组.如果一个数组的所有元素都不是数组,那么该数组称为一维数组. 一维数组的定义方式 在C语言中使用数组必 ...

  5. 从缓冲上看阻塞与非阻塞socket在发送接收上的区别

    最近在网络上看到一些帖子以及回复,同时又搜索了一些网络上关于阻塞非阻塞区别的描述,发现很多人在描述两者的发送接收时操作返回以及缓冲区处理的区别时有不同程度的误解.所以我想写一篇文章来纠正错误,并作为记 ...

  6. 线性表A-B

    1.顺序存储 #include<stdio.h> /* 设有两个顺序表A和B,且都递增有序,试写一算法,从A中删除与B中相同的那些元素,即求A-B */ #define getArrayL ...

  7. hdu 3934 Summer holiday(凸包最大内接三角形)

    求n个点能组成的最大三角形,一发旋转卡壳模板题... #include<algorithm> #include<iostream> #include<cstring> ...

  8. Eclipse快捷键大全(一)

    Eclipse快捷键大全(一) 常用(系统默认): 1.Format (自动排版) : Ctrl+Shift+F 2.Organize Imports (自动导入) : Ctrl+Shift+O 3. ...

  9. SQL Server 触发器2

    触发器可以做很多事情,但也会带来很多问题.使用它的技巧在于在适当的时候使用,而不要在不适当的时候使用它们. 触发器的一些常见用途如下: 弹性参照完整性:实现很多DRI不能实现的操作(例如,跨数据库或服 ...

  10. Linux解决xhost: unable to open display

    实用技巧:在Linux下设置xhost方法步骤 第一步:用root登陆linux,启动vnc服务: 第二步:根据vnc起来的端口,设置export DISPLAY=localhost:1(1表示vnc ...