//打印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. 4x4矩阵键盘扫描

    4x4矩阵键盘扫描 Windows 10 IoT Core 是微软针对物联网市场的一个重要产品,与以往的Windows版本不同,是为物联网设备专门设计的,硬件也不仅仅限于x86架构,同时可以在ARM架 ...

  2. MSSQL 获取指定日期所在星期的第一天和最后一天日期 获取指定日期坐在月的第一天和最后一天

    ufn_GetWeekFirstAndEndDay    获取指定日期所在星期的第一天和最后一天日期 ALTER FUNCTION [dbo].[ufn_GetWeekFirstAndEndDay]( ...

  3. mysql排行榜sql的实现

    SELECT num_rows AS num_rows, openid , openid, money FROM lt_cash_user ) t ORDER BY money DESC ) t1 W ...

  4. mysql数据库学习(二)--表操作

    一.表操作 以下内容都是自己学习的时候看过的一些知识,作为笔记记录一下吧,大部分都是所看文章的内容. 1.创建表 前面的基础篇笔记是相当于搭建了一个方便管理的文件夹树根,下面要学习的是一些关于表的知识 ...

  5. 饿了么移动APP的架构演进(转)

    原文:http://www.jianshu.com/p/2141fb0dc62c 文/圣迪(简书作者)原文链接:http://www.jianshu.com/p/2141fb0dc62c著作权归作者所 ...

  6. 使用Abator生产ibatis配置文件

    什么都不说了,直接进入正题. 插件安装地址:http://ibatis.apache.org/tools/abator 里面有name和url,填了就可以安装了. 通过菜单的 File > Ne ...

  7. 关于css3中before与after用单冒号还是双冒号的疑虑

    在 CSS3 中为了区别伪元素和伪类为伪元素使用了双冒号,因此如果使用了 display 或者 width 等属性时使得显示脱离了原本元素后,建议按照标准双写.

  8. asp.net ImageMap控件

    ImageMap 控件可创建包含定义的作用点区域的图像.当用户单击作用点区域时,该控件可生成到服务器的回发或导航到指定的 URL 首先是添加一个asp:ImageMap 选择asp:CircleHot ...

  9. Python核心编程读笔 12:OOP

    第13章 面向对象编程 一.基本概念 1.object类是所有类的基类,如果你的类没有继承任何其他父类,object 将作为默认的父类. 2.python创建实例时无需new: myFirstObje ...

  10. java 多线程学习(一)

    public class ThreadA extends Thread { ; public ThreadA() { super("ThreadID:" + (++threadID ...