NOI1.7编程基础之字符串 02:找第一个只出现一次的字符
总时间限制: 1000ms 内存限制: 65536kB
描述
给定一个只包含小写字母的字符串,请你找到第一个仅出现一次的字符。如果没有,输出no。
输入
一个字符串,长度小于100000。
输出
输出第一个仅出现一次的字符,若没有则输出no。
样例输入
abcabd
样例输出
c
思路:对字符串进行遍历,记录每一个字符出现的次数,对仅出现一次的字符进行输出
1 #include <iostream> 2 #include <iomanip> 3 #include <iostream> 4 #include <cmath> 5 #include <string.h> 6 #include <stdio.h> 7 using namespace std; 8 int main() 9 { 10 char str[100000]; 11 cin.getline(str, sizeof(str)); 12 for(int i=0;i<strlen(str);i++) 13 { 14 int s=0;//s是出现的次数 15 for(int j=0;j<strlen(str);j++) 16 { 17 if(str[i] == str[j]) 18 s++; 19 } 20 // cout<<"s="<<s<<endl; 21 if(s==1)//输出仅出现一次的字符 22 { 23 cout<<str[i]<<endl; 24 exit(0); 25 } 26 } 27 cout<<'no'<<endl; 28 }
浙公网安备 33010602011771号