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 == '\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++面试题一大波的更多相关文章
- 万网知您所需,“域”众不同--.link/.love/.help等一大波新顶级域来袭!
万网在新顶级域市场再次发力,一大波域名界的小鲜肉新鲜上线,价格优惠,限时低至9元起,更有丰富的可注册资源. 一下,即刻世界,用记录生活,用观看世界, 用和做最好的! 新上线的个性化新顶级域价格如下: ...
- Java实现 蓝桥杯 历届试题 斐波那契
试题 历届试题 斐波那契 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 斐波那契数列大家都非常熟悉.它的定义是: f(x) = 1 - (x=1,2) f(x) = f(x-1) ...
- 算法笔记_173:历届试题 斐波那契(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 斐波那契数列大家都非常熟悉.它的定义是: f(x) = 1 .... (x=1,2) f(x) = f(x-1) + f(x-2) ... ...
- 剑指offer笔记面试题10----斐波那契数列
题目:求斐波那契数列的第n项.写一个函数,输入n,求斐波那契数列的第n项.斐波那契数列的定义如下:f(0) = 0, f(1) = 1,f(n) = f(n - 1) + f(n - 2). 测试用例 ...
- 剑指offer-面试题10-斐波那契数列-递归循环
/* 题目:求斐波那契数列的第n项 */ /* 思路: f(n) = 0 n=0, 1 n=1, f(n-1) + f(n-2) n>1 */ int Fibonacci(int n){ if( ...
- 自己动手,丰衣足食!一大波各式各样的ImageView来袭!
工作略忙,一直想自己打造一个开源控件却苦于没有时间,可是这种事情如果不动手就会一直拖下去,于是最近抽时间做了个简单的自定义形状的ImageView控件. 时间紧迫,目前仅支持正六边形.圆形.菱形.椭圆 ...
- 【S】【S】【S】一大波前端干货整合(一)
前端交流站点 大前端 http://www.daqianduan.com/ V2EX http://www.v2ex.com/ W3cplus http://www. ...
- 一大波Java来袭(二)异常处理
概要解析: 本章的知识点能够记为:1图+5keyword+先逮小的.后逮大的 一.基础 (一)定义 1.异常 是指在程序执行的时候发生的一些异常事件.良好的程序设计应该在异常发生的时候提供处理异常的 ...
- 【转】一大波实用的 bash 别名和函数
作为一个命令行探索者,你或许发现你自己一遍又一遍重复同样的命令.如果你总是用ssh进入到同一台电脑,如果你总是将一连串命令连接起来,如果你总是用同样的参数运行一个程序,你也许希望在这种不断的重复中为你 ...
随机推荐
- oc block基本使用
// // main.m // block基本使用 // // Created by Ymmmsick on 15/7/21. // Copyright (c) 2015年 Ymmmsick. All ...
- PBOC2.0安全系列之—脱机认证之静态数据认证(SDA)
一,什么是PBOC2.0 2005年3月13日,人民银行发布第55号文,正式颁发了<中国金融集成电路(IC)卡规范>(简称PBOC2.0).该规范补充完善电子钱包/存折应用:增加借/贷记应 ...
- Keil MDK中使用pc-lint的详细方法
keil MDK版本:V4.03 PC-lint版本: V8.0 关于pc-lint的强大作用,网上有很多,这里不想再复述,只说一句:能通过pc-lint检验的程序不一定没有问题,但通过了pc-li ...
- 适应多行长文本的Android TextView
适应多行长文本的Android TextView
- Oracle数据导入导出imp/exp(转)
在oracle安装目录下有EXP.EXE与IMP.EXE这2个文件,他们分别被用来执行数据库的导入导出.所以Oracle数据导入导出imp/exp就相当与oracle数据还原与备份. 一.Oracle ...
- 倒计时 NAN 问题
http://blog.csdn.net/lishangua/article/details/51506821
- relatedTarget, fromElement, toElement
原文:http://www.quirksmode.org/js/events_mouse.html#relatedtarget W3C在mouseover和mouseout事件中添加了relatedT ...
- 用MFC实现OpenGL编程
一.OpenGL简介 众所周知,OpenGL原先是Silicon Graphics Incorporated(SGI公司)在他们的图形工作站上开发高质量图像的接口.但最近几年它成为一个非常优秀的开放式 ...
- Go语言Eclipse开发环境配置-Windows
1.首先安装eclipse,选择一个适合的版本就好,解压即可 http://www.eclipse.org/downloads/ 2.下载go语言安装包 官网地址 :http://www.golang ...
- Tab Bar Controller和Navigation Controller混合使用详细教程
在IPHONE上,NAV和TAB混合使用的案例很多.但很多书籍都没详细介绍这个是怎么使用的.我也找了很久才弄清楚怎么做.现在分享给大家. 1.先建立一个Window-based Application ...