C++笔试题
来源:互联网 发布:华三端口和ip绑定 编辑:程序博客网 时间:2024/06/10 17:55
1.用算法实现将一个输入的数字颠倒,要求不调用任何系统函数,也不能将输入数字转换为字符串作为中间过渡。
#include<iostream>#include<string>using namespace std;int main(){int num;cout<<"请输入一个整数:"; cin>>num; int reverse=0;while(num!=0){ int temp=num%10; num/=10; reverse= 10*reverse+temp;} cout<<endl<<"转化后的值:"<<reverse;}
2.整数二进制表示中1的个数
方法一:
#include<iostream>#include<string>using namespace std;int main(){int a=15;int count=0;unsigned int flag=1;while(flag){if(a&flag)++count;flag=flag<<1;}cout<<"个数:"<<count;}
方法二:
int NumberOf1_Solution3(int i){ int count = 0; while (i) { ++ count; i = (i - 1) & i; } return count;}
3.实现一个字符串中空格数不能超过一个,例如 a--- b-c应该输出 a-b-c, 此处 -代表空格
void str_filter(char *input,char *output){ int a=0;while(*input!='\0'){ cout<<*input<<endl; if(*input!=' ')*output++=*input++; else{if(*input == ' ' && a==0){ a++; *output++=*input++;} if(*input == ' ' && a>0) input++; if(*input!=' ') a=0;}}*output='\0';//必须加上结尾空字符}
4.求一个整数数组的最大元素,用递归方法实现。
#include<iostream>using namespace std;#define max(a,b) ((a)>(b)?(a):(b))int maxnum(int [],int);int main(){int a[10]={1,3,4,5,7,8,9,0,2,6};cout<<a[0]<<endl<<(a+1)[0]<<endl;cout<<"递归后最大的数是:"<<maxnum(a,10)<<endl;return 0;}int maxnum(int a[],int n){ if(n==1)return a[0];else return max(a[0],maxnum(a+1,n-1));}
- c++/c笔试题
- c/c++笔试题
- c笔试题
- C语言笔试题
- C语言笔试题
- C/C++笔试题!!!!
- C语言笔试题
- c笔试题
- c笔试题
- c语言笔试题
- c/c++笔试题
- 若干C笔试题
- C/C++笔试题
- c语言笔试题
- 一道C笔试题
- C/C++笔试题
- c/c++笔试题
- C/C++笔试题
- WebStorm 6.0的live edit功能实现步骤
- 别去强迫他人按自己的习惯去选择,别用自己的价值体系去评判别人。
- 单链表的逆置
- VS2010下创建静态链接库和动态链接库
- 使用<math.h>库,找不到函数定义
- C++笔试题
- hdu 4790 Just Random
- 【拓展kmp】
- SnagIt - 专业的截图工具
- AngularJS快速开始
- poj 2029 Get Many Persimmon Trees
- Debian下实验Linux Container命令简记
- Linux最大线程数限制及当前线程数查询
- 基于xmpp openfire smack开发之Android客户端开发[3]