蓝桥杯 算法训练 Anagrams问题

    xiaoxiao2021-03-26  31

    算法训练 Anagrams问题   时间限制:1.0s   内存限制:512.0MB 问题描述   Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的。例如,“Unclear”和“Nuclear”、“Rimon”和“MinOR”都是Anagrams。编写一个程序,输入两个单词,然后判断一下,这两个单词是否是Anagrams。每一个单词的长度不会超过80个字符,而且是大小写无关的。   输入格式:输入有两行,分别为两个单词。   输出格式:输出只有一个字母Y或N,分别表示Yes和No。   输入输出样例 样例输入 Unclear Nuclear 样例输出 Y

    代码:

    #include<iostream> #include<string.h> using namespace std; int main() { string word1,word2;  cin>>word1>>word2; int i,j; int count=0; if(word1.length()!=word2.length()) {//若长度不同则输出N;    cout<<"N";   } else {      for(i=0;i<word1.length();i++)      for(j=0;j<word2.length();j++)   {      if(word1[i]==word2[j]||word1[i]+32==word2[j]||word1[i]-32==word2[j])     {      count++;//count用于记录Word1中字母匹配到个数      break;     }     }    if(count==word1.length()) cout<<"Y"; else cout<<"N"; return 0;
    转载请注明原文地址: https://ju.6miu.com/read-663000.html

    最新回复(0)