[ABC426B]The Odd One Out题解

Time Limit: 2 sec / Memory Limit: 1024 MiB

Score : 200 points

Problem Statement

You are given a string S of length at least 3 consisting of lowercase English letters.
S contains exactly two types of characters, and exactly one character is different from the others. Find that one character.

For example, if S is odd, report o.

有道 翻译

问题陈述

您将得到一个长度至少为 3 的字符串 S ,由小写英文字母组成。

S 恰好包含两种类型的字符,并且恰好有一个字符与其他字符不同。找到那个角色。

例如,如果 S 是‘奇数’,则报告‘ 0 ’。

Constraints

  • S is a string of length at least 3 and at most 10 consisting of lowercase English letters.
  • S contains exactly two types of characters, and exactly one character is different from the others.

有道 翻译

# #约束

— S 是由小写英文字母组成的字符串,长度至少为 3 ,最多为 10 。

— S 包含两种类型的字符,且其中一个字符与其他字符不同。


Input

The input is given from Standard Input in the following format:

S

有道 翻译

# #输入

输入来自标准输入,格式如下:

S

Output

Print the answer.

有道 翻译

# #输出

打印答案。


Sample Input 1

Copy

odd

Sample Output 1

Copy

o

In odd, the character different from the others is o.

有道 翻译

###输出示例

o

在‘ odd ’中,与其他字符不同的字符是‘ o ’。


Sample Input 2

Copy

dad

Sample Output 2

Copy

a

In dad, the character different from the others is a.

有道 翻译

###示例输出

a

在“爸爸”中,与其他字母不同的字符是“a”。


Sample Input 3

Copy

wwwwwwwwwv

Sample Output 3

Copy

v

In wwwwwwwwwv, the character different from the others is v.

有道 翻译

###输出示例

v

在‘ wwwwwwwwwv ’中,与其他字符不同的字符是‘ v ’。

思路

直接记录字符判断即可。

代码见下

#include<bits/stdc++.h>
using namespace std;
char ch,ch1,ch2;
long long c1=0,c2=0;
int main(){
	ch1='(';
	ch2='(';
	while(cin>>ch){
		if(ch1=='('){
			ch1=ch;
			c1++;
		}
		else if(ch1==ch){
			c1++;
		}
		else if(ch2=='('){
			ch2=ch;
			c2++;
		}
		else if(ch==ch2){
			c2++;
		}
	}
	if(c1==1){
		cout<<ch1<<endl;
	}
	else{
		cout<<ch2<<endl;
	}
	return 0;
}

posted @ 2025-10-04 21:56  bz02_2023f2  阅读(1)  评论(0)    收藏  举报  来源