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进入到同一台电脑,如果你总是将一连串命令连接起来,如果你总是用同样的参数运行一个程序,你也许希望在这种不断的重复中为你 ...
随机推荐
- de4dot命令 v2.0.3.3405
de4dot v2.0.3.3405 Copyright (C) 2011-2013 [email]de4dot@gmail.com[/email] Latest version and source ...
- javascript instanceof
object instanceof constructor instanceof运算符用来检测constructor.prototype是否存在于参数object的原型链上. 对于instanceof ...
- 使用 rpython 在 windows 下生成的程序无法运行
在 windows 用rpython编译出的文件总是无法运行,报 通过跟踪发现,rpython 每次都会将生成的C代码.Makefile 等放置在 %TEMP%\usession-release-2. ...
- hive中partition如何使用
1.背景 1.在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入了partition概念. 2.分区表指的是在创建表 ...
- [ACM] hdu 2191 珍惜如今,感恩生活 (多重背包)
Problem Description 急!灾区的食物依旧短缺! 为了拯救灾区同胞的生命,心系灾区同胞的你准备自己採购一些粮食支援灾区,如今如果你一共同拥有资金n元,而市场有m种大米,每种大米都是袋装 ...
- Mustache学习
Mustache是基于JavaScript的一款模版Web引擎,Web 模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,通常是标准的 HTML 文档. 一.Musta ...
- SharePoint webpart中悬浮窗口的webconfig路径
SharePoint webpart中悬浮窗口的webconfig路径在.../_layouts/15/下.
- android入门——BroadCast(2)
自定义广播 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=&q ...
- OCP prepare 20140703
1. trim trim('aaa' from 'aaabbbccc') 这个是错误的.ora-30001: trim set should have only one character 2. in ...
- C趣味100道之58.拉丁方的一些想法。
题目如上. 思路(未写) 完整代码如下: #include<iostream> #include<queue> #include<math.h> using nam ...