C++基础练习题(一): 查找最短单词

/*<说明>
编程实现将字符串中最短的单词输出,在主函数中输入字符串,编写一个函数完成最短单词的查找
</说明>*/
#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();
}

这是答案中的 只打印了第一个最短单词 但是实现比自己写的代码快很多。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++是一种通用的编程语言,它支持面向对象的编程风格,并且具有强大的系统编程能力。下面是C++的一些基础语法: 1. 注释:在C++中,注释可以用来解释代码,以便于其他人阅读和理解。单行注释使用双斜线(//),多行注释使用斜线和星号(/* ... */)。 2. 标识符:标识符是变量、函数、类等的名称。标识符由字母、数字和下划线组成,并且以字母或下划线开头。 3. 变量:在C++中,需要声明变量来存储数据。变量的声明包括类型和名称。例如,int表示整数类型,而float表示浮点数类型。 4. 数据类型:C++提供了多种数据类型,包括整型(int、short、long)、浮点型(float、double)、字符型(char)、布尔型(bool)等。 5. 运算符:C++支持各种运算符,例如算术运算符(+、-、*、/)、关系运算符(==、!=、<、>)、逻辑运算符(&&、||、!)等。 6. 控制流语句:C++提供了多种控制流语句,例如条件语句(if-else)、循环语句(for、while、do-while)、跳转语句(break、continue、return)等。 7. 函数:函数是可重用的代码块,用于执行特定的任务。函数由函数头和函数体组成,函数头包括返回类型、函数名和参数列表。 8. 类和对象:C++是面向对象的语言,支持类和对象的概念。类是一种用户定义的数据类型,用于封装数据和方法。对象是类的实例,可以通过调用对象的方法来操作数据。 这只是C++语言的一些基础语法,还有很多其他的概念和特性。如果你对某个特定的主题有更深入的兴趣,我可以为你提供更详细的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值