笔试算法题
来源:互联网 发布:php简易查询系统源码 编辑:程序博客网 时间:2024/06/07 11:32
1.输入一串字符串,统计输入大小写英文字母、数字、空格、其他字符的数量
#include<stdio.h>int main(){ int letter,num,space,other; letter=num=space=other=0; char ch; while((ch=getchar())!='\n') { if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z') letter++; else if(ch>='0'&&ch<='9') num++; else if(ch==' ') space++; else other++; } printf("letter num is %d\n",letter); printf("number num is %d\n",num); printf("space num is %d\n",space); printf("other num is %d\n",other);}
2.完全平方数:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
#include<stdio.h>#include<math.h>int main(){ int i,j,n,temp,flag; for(i=1;;i++) { if(sqrt(i+100)==(int)sqrt(i+100)) if(sqrt(i+100+168)==(int)sqrt(i+100+168)) { printf("%d\n",i); break; } printf("f %d\n",i); }}
3.输入一段英文,如( hello world chen swpu )去掉空格后输出
#include<stdio.h>#include<string.h>int main(){ char s[1000]; int des[1000]; int i; int j=0; int m=2; gets(s); for(i=0;*(s+i)!='\0';i++) { if(*(s+i)==' ') des[j++]=i; } printf("%d\n",des[j-1]); for(i=des[j-1]-1;i>=0;i--) { if(i>des[j-m]) printf("%c",*(s+i)); else { m++; printf(" "); } } printf("%d",strlen(s));}
4.判断素数和输出小于n的所有素数
#include<iostream>using namespace std;int checkPrime(int m)//判断素数{ int i,j; if(m<2) return -1; else for(i=2;i<=m/2;i++) if(m%i==0) return -1; return 1;}int outputPrime(int m)//输出小于m的所有素数{ int i,j; for(i=2;i<=m;i++) { for(j=2;j<=i/2;j++) if(i%j==0) { break; } if((i/2+1)==j) cout<<i<<" "; } return 0;}int main(){ int i,j,m; cin>>m; //if(checkPrime(m)==1) //cout<<m<<endl; outputPrime(m);}
5.猴子第一天摘了若干个桃子,当即吃了一半,还不解馋,又多吃了一个;第二天,吃剩下的桃子的一半,还不过瘾,又多吃了一个;以后每天都吃前一天剩下的一半多一个,到第10天想再吃时,只剩下一个桃子了。问第一天共摘了多少个桃子?
#include<iostream>using namespace std;int main(){ int temp=1; int i; for(i=1;i<10;i++) { temp=(temp+1)*2; cout<<"temp="<<temp<<" ";//输出2-9天吃的桃子 }}
6.10进制转2进制输出
#include<iostream>using namespace std;int main(){ int m,i; int a=1; i=0; cin>>m; while(m>0) { if(m%2==1) a|=1<<i; else a&=0<<i; ++i; m/=2; } for (int i=sizeof(int)*8-1;i>=0;i--) { int mask=1<<i; if (mask & a) cout<<"1"; else cout<<"0"; if(i%8==0) cout<<" "; }}
7.16进制转10进制
#include<iostream>#include<cmath>#include<cstdio>#include<cstring>using namespace std;int hex_char_value(char c){ int m; if(c>='0'&&c<='9') m=c-'0'; else if(c>='a'&&c<='z') m=(c-'a'+10); else if(c>='A'&&c<='Z') m=(c-'A'+10); return m;}int hex_str_value(const char *hex,int len){ int result=0; int i,j; for(i=0;i<len;i++) { result+=pow(16,i)*(hex_char_value(hex[i])); } return result;}int main(){ int m,n,j; char s[1024]; gets(s); n=strlen(s); m=hex_str_value(s,n); cout<<m;}
0 0
- 算法笔试题_1
- 算法笔试题_2
- 算法笔试题
- 算法笔试题
- 算法笔试题
- 笔试中的算法题
- 算法笔试题
- 笔试面试算法题
- 算法笔试题
- 一道算法笔试题
- 一道算法笔试题
- 常见算法笔试题
- 笔试题算法
- 算法笔试题
- 阿里算法笔试题
- 笔试算法题
- 算法笔试题
- 乐视笔试算法题美团算法笔试题
- shell中的特殊符号
- 蓝桥杯-方格填数
- 如何在C#.net中自定义和使用特性
- CDOJ 1349 柱爷大战滑稽王(dp LCS转LIS)
- ActiveMQ完整的java客户端例子(源码)
- 笔试算法题
- 设计模式之装饰者模式简单理解
- Android BroadcastReceiver中播放提示语音有时失效问题
- 7.2 MySQL权限系统原理
- 判断一个double型数据是否为0
- poj 2456 Aggressive cows (二分)
- android studio运行程序时正常,调试时出现NoSuchMethodException的解决办法
- 第十二周:83. Remove Duplicates from Sorted List
- Android安全防护之旅---带你把Apk混淆成中文语言代码