Dailycode_6.3(跳石板、参数解析)

在这里插入图片描述

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    string str;
    getline(cin, str);
    int count = 1;
    vector<string> arr;
    int flag = 0;
    string ret;
    for (size_t i = 0; i < str.size(); i++)
    {
    	//判断是否在“”区间内  34是“的asc码
        if (str[i] == 34)
        {
            flag = 1;
            continue;
        }
        else if (str[i] == 34)
        {
            flag = 0;
            continue;
        }
        //若不在“”区间内,则要判断空格  32是空格的asc码
        if (flag == 0)
        {
            if (str[i] == 32)
            {
                count++;
                arr.push_back(ret);
                ret.clear();
            }
            else
            {
                ret.push_back(str[i]);
            }
        }
        //若在”“区间内,则无脑加入寄存的字符串内
        else 
        {
            ret.push_back(str[i]);
        }
    }
    //因为跳出循环后最后一个字符串没有加入数组内
    if (ret.size() > 0)
    {
        arr.push_back(ret);
    }
    cout << count << endl;
    vector<string> :: iterator it = arr.begin();
    while(it != arr.end())
    {
        cout << *it << endl;
        it++;
    }
    return 0;
}

在这里插入图片描述

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

void is_divisor(int N, vector<int>& arr)
{
    for (int i = 2; i <= sqrt(N); i++)
    {
        if (N % i == 0)
        {    
            arr.push_back(i);
            if (N / i != i)
            {
                arr.push_back(N/i);
            }
        }
    }
}

int main()
{
    int N,M;
    cin >> N >> M;
    vector<int> steps(M + 1,0);
    steps[N] = 1;
    for (int i = N; i <= M; ++i){
        if(steps[i]==0)
            continue;
        vector<int> arr;
        is_divisor(i,arr);
        for (int j = 0; j < arr.size(); ++j)
        {
            if (arr[j] + i <= M)
            {
                if (steps[arr[j] + i] == 0)
                    steps[arr[j] + i] = steps[i] + 1;
                else
                    steps[arr[j] + i] = (steps[i] + 1) < steps[arr[j] + i]? (steps[i] + 1) : steps[arr[j] + i];
            }
        }  
    }
    
    if(steps[M] == 0)
        cout << -1 << endl;
    else
        cout << steps[M]-1 << endl;
    return 0;   
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值