c++实现标记数字、最后单词长度和小球五次落地

标记数字:

#include <iostream>
#include <string>

using namespace std;

const string mark="*";

bool check_num(char c)
{
    if(c>47&&c<58)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void mark_num(string& in_str)
{
    int tag=0;
    size_t size=in_str.length();
    for(size_t i=0; i<size; ++i)
    {
        if(check_num(in_str[i]))
        {
            ++tag;
        }
        else
        {
            if(tag!=0)
            {
                in_str.insert(i-tag,mark);
                in_str.insert(i+1,mark);
                tag=0;
                size=in_str.length();
            }
        }
    }
    if(tag!=0)
    {
        in_str.insert(size-tag,mark);
        in_str.insert(size+1,mark);
    }
}

int main()
{

    string str ;
     getline(cin,str);
    mark_num(str);
    cout << str << endl;
    return 0;
}

最后单词长度:

#include <iostream>
#include <string>

using namespace std;

bool check_alpha(char c)
{
      if((c>96 && c<123) || (c>64 && c<91))
      {
            return true;
      }else
      {
            return false;
      }
}

int last_word_length(string& in_words)
{
      int tag=0;
      int i=in_words.length()-1;
      while(i>-1)
      {
            if(check_alpha(in_words[i]))
            {
                  ++tag;
            }else
            {
                  if(tag!=0)
                  return tag;
            }
            --i;
      }
      return tag;
}

int main()
{
      string str;
      getline(cin,str);
      cout << last_word_length(str)<< endl;
      return 0;
}


小球5次落地旅途长度:

#include <iostream>

using namespace std;

int power(int a,int b)
{
      int re=a;
      for(int i=1;i<b;++i)
      {
            re=re*a;
      }
      return re;
}

double get_journey(int high)
{
    if(high>0)
    {
        double length=high;
        double all=high;
        for(int i=1; i<5; ++i)
        {
            all+=2*length/power(2,i);
        }
        return all;
    }
    else if(high==0)
    {
        return 0;
    }
    else
    {
        return -1;
    }

}

double get_tenth_high(int high)
{
    if(high>0)
    {
        double length=high;
        return length/power(2,5);
    }
    else if(high==0)
    {
        return 0;
    }
    else
    {
        return -1;
    }
}

int main()
{
      int high=0;
      cin>>high;
    cout << get_journey(high) << endl;
    cout << get_tenth_high(high) << endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值