m0_62414947 2021-11-14 13:16 采纳率: 0%
浏览 31
已结题

在定义中可以进行强制类型转换吗

2、 定义一个字符串数组,用以接受用户输入的英文单词列表(单词数不少于十个),随后再接受另外两个单词的输入(这两个单词必须是包含在前面的单词列表里面的单词),随后判断这两个单词在字符串数组中的最短距离,要求使用函数调用实现。

  • 写回答

1条回答 默认 最新

  • m0_62414947 2021-11-15 11:37
    关注

    #include
    #include
    #include
    #include
    using namespace std;
    class Solution {
    public:
    int shortestWordDistance(vector& words, string word1, string word2) {
    int index1=-1,index2=-1,res=INT_MAX;
    for(int i=0;i<words.size();++i){
    if(words[i]==word1){//找出单词1的索引位置
    index1=i;
    if(index2!=-1&&index1!=index2){//单词二存在,且不为同一个位置
    res=min(res,abs(index1-index2));
    }
    }

            if(words[i]==word2){//找出单词2的索引位置
                index2=i;
                if(index1!=-1&&index1!=index2){//单词1存在,且不为同一个位置
                    res=min(res,abs(index1-index2));
                }
            }
    
        }
        return res;
    }
    

    };
    int main()
    { int shortestWordDistance(vector& words, string word1, string word2);
    string words[100];
    getline(cin,words[100]);
    string word1,word2;
    cin>>word1>>word2;
    int z;
    z= shortestWordDistance(words[100], word1, word2);
    cout<<z<<endl;
    return 0;
    }

    评论

报告相同问题?

问题事件

  • 系统已结题 11月22日
  • 创建了问题 11月14日