C++基础练习题(一): 查找最短单词
2014-01-22 16:14
288 查看
/*<说明> 编程实现将字符串中最短的单词输出,在主函数中输入字符串,编写一个函数完成最短单词的查找 </说明>*/ #include<time.h> #include<iostream> using namespace std; void shortestWord(char* in) { int i,j=0; int o[1000]; for(i=0;*(in+i)!=0;i++) { if(*(in+i)==' ') { o[j]=i; //o的作用是定位每一个空格的位置 通过空格位置间隔的大小判断单词的长短 j++; } } j--; int z=0; int k=o[0]; //k是最短单词的个数 初始化为第一个空格的位置 因为后面的操作没有考虑第一个 int l[50]; for(;j!=0;j--) { if(o[j]-o[j-1]-1<k) { z=0; k=o[j]-o[j-1]-1; l[0]=j-1; } else if(o[j]-o[j-1]-1==k) { z++; //z统计有多少个最短单词 l[z]=j-1; } } if(o[0]==k) { for(int n=0;n<k;n++) { printf("%c",*(in+n)); //如果第一个单词就是最短的 打印 } printf(" "); } for(int m=z;m>0;m--) { for(int n=1;n<=k;n++) { printf("%c",*(in+o[l[m]]+n)); //打印其他最短单词 } printf(" "); } } void main() { char in[1000]="Learning a the parameters of neural networks is perhaps one of the most well studied problems within the field of machine learning. Early work on backpropagation algorithms showed that the gradient of the neural net learning objective could be computed efficiently and used within a gradient descent scheme to learn the weights of a network with multiple layers of non-linear hidden units. Unfortunately, this technique doesn’t seem to generalize well to networks that have very many hidden layers (i.e. deep networks). The common experience is that gradient-descent progresses extremely slowly on deep nets, seeming to halt altogether before making significant progress, resulting in poor performance on the training a set (under-fitting)"; int a=clock(); shortestWord(in); int b=clock(); int c=b-a; printf("%d",c); getchar(); }
上面是自己写的代码 效果并不好 测试了一下运行效果2毫秒 太慢 而且没有考虑有连续空格的情况。
/*<书上答案>*/ #include<iostream> #include<time.h> using namespace std; const int Max=200; char *findshort(char s[]) { static char s1[Max]; //其地址要返回,所以设计为静态变量 char s2[Max]; int i=0,j,len1=0,len2=0; while(s[i++]!='\0'); s[i-1]=' '; s[i]='\0'; i=0; while(s[i]!='\0') { if(s[i]==' '&&s[i+1]!='\0'&&s[i+1]==' ') //跳过多余空格 { i++; continue; } if(s[i]!=' ') //提取一个单词到S2中 { s2[len2++]=s[i]; } else if(len1==0) { len1=0; for(j=0;j<len2;j++) //将S2复制到S1中 s1[len1++]=s2[j]; s1[len1]='\0'; len2=0; } else if(len1>len2) { len1=0; for(j=0;j<len2;j++) //将S2复制到S1中 s1[len1++]=s2[j]; s1[len1]='\0'; len2=0; } else { len2=0; } i++; } return s1; } void main() { char s[Max]="asddddd gg las sdlgaw va eg aoeng a ge a e gae geoia ae x eox ge x ieg ns e a dfge qdn i am ver"; cout<<"输入单词串:"; int a=clock(); cout<<"最短单词:"<<findshort(s)<<endl; int b=clock(); int c=b-a; cout<<c<<endl; getchar(); }
这是答案中的 只打印了第一个最短单词 但是实现比自己写的代码快很多。
相关文章推荐
- Floyd All Shortest Path 所有最短路径的查找 C++程序
- 单词字典中对兄弟单词查找算法(C++实现)
- C++基础-string截取、替换、查找子串函数
- C++基础练习题之打怪
- C++语法基础--关联容器--map(二)--查找并读取map中的元素count(),find()与 从map对象中删除元素erase()
- C++基础-string截取、替换、查找子串函数
- C++基础-string截取、替换、查找子串函数
- 去哪儿网C++笔试题--二分查找、最先出现两次的字符、每个字符串中的单词个数
- C++——算法基础之动态查找表2——平衡二叉树(插入)
- C++基础笔记之八:二分查找
- 编写程序计算sentence中有多少个单词,并指出其中最长和最短的单词 C++
- C++语法基础--顺序容器(三)--关系操作符,容器大小的操作,访问元素,删除元素,查找元素
- C++基础练习题之队列(不是queue,只是一道题,千万别误会)
- 来自百度贴吧的练习题 :求最长单词的长度和最短单词的长度。
- UVa-10815-Andy's First Dictionary c++ STL set 基础练习题
- C++基础-string截取、替换、查找子串函数<转>
- 单词查找树(c++ 版)
- LinuxC/C++编程基础(11) 基于深度优先搜索查找迷宫路线
- 三向单词查找树(c++版)
- C++基础练习题之湖的深度