小T,小A和小D由于没有好好复习,拿到试卷后发现所有问题自己都不会做,于是他们决定猜测正确答案。
对于答案序列他们都有自己的想法:
小T 使用了这个序列:C,C,A,A,B,B,C,C,A,A,B,B……
小A说最好的顺序是:A,B,C,A,B,C,A,B,C,A,B,C……
小D决定下面这个序列更好:B,A,B,C,B,A,B,C,B,A,B,C……
请你写一个程序,根据给出的正确答案,确定三个人中谁对的最多。
【输入数据】
第一行一个整数 nn,表示考试的数量。 第二行一个长度为 nn 的字符串(只包含A、B、C三种字符),表示考试的正确答案。
【输出数据】
第一行输出一个整数 mm,表示三个人中做对最多的数量。
从第二行开始,输出答对数最多的人的名字(每行一个,按照字典序输出)
【输入样例】
9
AAAABBBBB【输出样例】
4
A
D
T【数据范围】
对于 100%100% 的数据,有 1≤n≤1001≤n≤100。
#include <iostream>
using namespace std;
int main() {
int nn;
char answer;//正确答案
char table_t[6] = {'A','A','B','B','C','C'};//小T的序列
char table_a[3] = {'A','B','C'}; //小A的序列
char table_d[4] = {'B','A','B','C'}; //小D的序列
int count_t = 0,count_a = 0,count_d = 0;
cin>>nn;//输入题数
cin.get();//把输入流里的换行符读取进来.如果不加上这一句,在下面的代码中会将此换行符当成第一题答案
for (int i=0;i<nn;i++)
{
// 获取一个正确答案
answer = (char)cin.get();
// 小T,小A,小D的答案
char t = table_t[i%6];
char a = table_a[i%3];
char d = table_d[i%4];
// 答案是否正确,正确的计数器加1
if (t == answer)count_t++;
if (a == answer)count_a++;
if (d == answer)count_d++;
}
// 找出最大值,中间值,最小值
if (count_a>=count_d && count_a>=count_t)
{
cout<<count_a<<endl<<'A'<<endl;
if (count_d>=count_t)
{
cout<<'D'<<endl<<'T';
}
else
{
cout<<'T'<<endl<<'D';
}
}
else if (count_d>=count_a && count_d>=count_t)
{
cout<<count_d<<endl<<'D'<<endl;
if (count_a>=count_t)
{
cout<<'A'<<endl<<'T';
}
else
{
cout<<'T'<<endl<<'A';
}
}
else
{
cout<<count_t<<endl<<'T'<<endl;
if (count_a>=count_d)
{
cout<<'A'<<endl<<'D';
}
else
{
cout<<'D'<<endl<<'A';
}
}
return 0;
}
原答案有个错误,char table_t[6] = {'A','A','B','B','C','C'};//小T的序列错了,应为CCAABB
改正后:
#include <iostream>
using namespace std;
int main() {
int nn;
char answer;//正确答案
char table_t[6] = {'C','C','A','A','B','B'};//小T的序列
char table_a[3] = {'A','B','C'}; //小A的序列
char table_d[4] = {'B','A','B','C'}; //小D的序列
int count_t = 0,count_a = 0,count_d = 0;
cin>>nn;//输入题数
cin.get();//把输入流里的换行符读取进来.如果不加上这一句,在下面的代码中会将此换行符当成第一题答案
for (int i=0;i<nn;i++)
{
// 获取一个正确答案
answer = (char)cin.get();
// 小T,小A,小D的答案
char t = table_t[i%6];
char a = table_a[i%3];
char d = table_d[i%4];
// 答案是否正确,正确的计数器加1
if (t == answer)count_t++;
if (a == answer)count_a++;
if (d == answer)count_d++;
}
// 找出最大值,中间值,最小值
if (count_a>=count_d && count_a>=count_t)
{
cout<<count_a<<endl<<'A'<<endl;
if (count_d>=count_t)
{
cout<<'D'<<endl<<'T';
}
else
{
cout<<'T'<<endl<<'D';
}
}
else if (count_d>=count_a && count_d>=count_t)
{
cout<<count_d<<endl<<'D'<<endl;
if (count_a>=count_t)
{
cout<<'A'<<endl<<'T';
}
else
{
cout<<'T'<<endl<<'A';
}
}
else
{
cout<<count_t<<endl<<'T'<<endl;
if (count_a>=count_d)
{
cout<<'A'<<endl<<'D';
}
else
{
cout<<'D'<<endl<<'A';
}
}
return 0;
}