CSW_YSIY @ 2024-04-13 11:50:54
#include<bits/stdc++.h>
using namespace std;
int a[1100];
string s;
int main()
{
cin>>s;
for(int i=0;i<s.size();i++)
{
a[int(s[i]-'a')]++;
}
for(int i=0;i<s.size();i++)
{
if(a[i]==1)
{
cout<<s[i];
return 0;
}
}
cout<<"no";
return 0;
}
by Kobe_Bryan @ 2024-04-13 11:53:23
@CSW_YSIY 猜测要初始化数组
by WMWD @ 2024-04-13 11:54:23
用 map
吧
by Kobe_Bryan @ 2024-04-13 11:58:12
@CSW_YSIY 是第二个循环中i的问题,如果字符串为:
by Kobe_Bryan @ 2024-04-13 12:00:48
代码:
#include<iostream>
using namespace std;
int main(){
int a[26]={0},len;
string n;
getline(cin,n);
len=n.size();
for(int i=0;i<len;i++)
{
a[n[i]-'a']++;
}
for(int i=0;i<len;i++)
{
if(a[n[i]-'a']==1)
{
cout<<n[i];
return 0;
}
}
cout<<"no";
return 0;
}
by CSW_YSIY @ 2024-04-13 13:27:19
@Kobe_Bryan 感谢!
by WMWD @ 2024-04-13 17:24:50
@CSW_YSIY 可以直接去存,但是存完后遍历的下标是
#include<bits/stdc++.h>
using namespace std;
int a[1100];
string s;
int main()
{
cin>>s;
for(int i=0;i<s.size();i++)
{
a[s[i]]++;
}
for(int i=0;i<s.size();i++)
{
if(a[s[i]]==1)
{
cout<<s[i];
return 0;
}
}
cout<<"no";
return 0;
}