Time Limit: 2000ms, Special Time Limit:5000ms, Memory Limit:65536KBTotal submit users: 25, Accepted users: 22Problem 13800 : No special judgementProblem descriptionGrasshopper Marko was jumping happily all over the meadow. He wasn’t being careful and his Nokia 3310 fell intoa puddle. His mobile phone is now acting funny! The contacts got wet and the keyboard works in a completelyunpredictable manner! All the numerical keys broke down. When we press one of them, the mobile phone acts asif we pressed another key. Luckily, there are no two keys that are acting the same so Marko can still writeall the letters. Grasshopper Marko was experimenting a bit and found out how each key acts. Now he wants to write a messageto his girlfriend. Since he is just a grasshopper, you will do that for him. all those who don’t remember how mobile phones with keys work, here is a short description.
The image shows keys with letters that we can get by pressing that key (on a working mobile phone that didn’tfall into a puddle). For example, if we want to get letter ‘a‘ we will press key 2 once, and if we want letter ‘b‘ wewill press the key 2 twice. If we want to write two letters from the same key consecutively, we have to press thepound key (‘#‘) exactly once. For example, if we want to write the string “klor“ we will press the keys in thefollowing order: “55#555666777“. InputThe first line of input contains 9 integers. The first integer marks the behaviour of key ‘1‘, the second the behaviourof key ‘2‘, the third the behaviour of key ‘3‘, and so on. Marko is not using keys ‘*‘ and ‘0‘ because he is a grasshopper. Key ‘#‘ can’t get broken. The second line of input contains a string consisting of only lowercase letters of the English alphabet. The lengthof the word won’t exceed 100 characters. OutputThe first and only line of output must contain the sequence of keys you need to press in order to write Marko’smessage. Sample Input Sample Input 1
2 3 4 5 6 7 8 9 1
klor
Sample Input 2
7 8 9 1 2 3 6 5 4
djevojka
Sample Input 3
9 8 7 6 5 4 3 2 1
skakavac
Sample Output Sample Output 1
44#444555666
Sample Output 2
68662227778#885
Sample Output 3
33335585582228#888
Judge TipsAll of the keys are shifted one place to the right so the output differs a little bit fromthe example in the task statement. Problem Source COCI 2014/2015 contest 2
解题思路
其实这道题非常的简单,我的思路就是。我们可以把一个字母,分别表示成对应的键以及对应的位置
比如b,在2号键的2号位置,所以我就设置为2.2
通过这样子我就可以到时候利用这个小数的整数部分确定换掉之后要输出什么数字,以及利用小数部分来确定究竟要输出多少次。至于是否要输出“#”,我们可以定义一个变量来进行比较。
代码展示
#include<iostream>
#include<string>
using namespace std;
int main()
{
int p[9];
for(int i=0;i<9;i++)
p[i]=0;
while(cin>>p[0])
{
for(int i=1;i<9;i++)
cin>>p[i];
cin.get();
string a;
cin>>a;
int n=a.size();
double b[100];
for(int i=0;i<100;i++)
b[i]=0;
for(int i=0;i<n;i++)
{
if(a[i]=='a')
b[i]=2.1;
if(a[i]=='b')
b[i]=2.2;
if(a[i]=='c')
b[i]=2.3;
if(a[i]=='d')
b[i]=3.1;
if(a[i]=='e')
b[i]=3.2;
if(a[i]=='f')
b[i]=3.3;
if(a[i]=='g')
b[i]=4.1;
if(a[i]=='h')
b[i]=4.2;
if(a[i]=='i')
b[i]=4.3;
if(a[i]=='j')
b[i]=5.1;
if(a[i]=='k')
b[i]=5.2;
if(a[i]=='l')
b[i]=5.3;
if(a[i]=='m')
b[i]=6.1;
if(a[i]=='n')
b[i]=6.2;
if(a[i]=='o')
b[i]=6.3;
if(a[i]=='p')
b[i]=7.1;
if(a[i]=='q')
b[i]=7.2;
if(a[i]=='r')
b[i]=7.3;
if(a[i]=='s')
b[i]=7.4;
if(a[i]=='t')
b[i]=8.1;
if(a[i]=='u')
b[i]=8.2;
if(a[i]=='v')
b[i]=8.3;
if(a[i]=='w')
b[i]=9.1;
if(a[i]=='x')
b[i]=9.2;
if(a[i]=='y')
b[i]=9.3;
if(a[i]=='z')
b[i]=9.4;
}
int tmp=0;
for(int i=0,last=-1;i<a.size();i++)
{
tmp=(int)(b[i]+0.5);
for(int j=0;j<9;j++)
{
if(p[j]==tmp)
{
tmp=j+1;
break;
}
}
if(tmp==last)
cout<<"#";
int tmp2=b[i]*10;
tmp2=tmp2;
for(int k=0;k<tmp2;k++)
cout<<tmp;
last=tmp;
}
cout<<endl;
}
} 很遗憾,这种做法得到的是WA,问题出现在哪呢? double的精度损失
避免这种情况是,当你转换成int的时候,要么加上0.5再转(别进位了。。。。。。),要么就直接用整数吧例如4.2就用42,/10得到整数位置,得到个位位置。
附上两个AC代码
#include<iostream>
#include<string>
using namespace std;
int main()
{
int p[9];
for(int i=0;i<9;i++)
p[i]=0;
while(cin>>p[0])
{
for(int i=1;i<9;i++)
cin>>p[i];
cin.get();
string a;
cin>>a;
int n=a.size();
int b[100];
for(int i=0;i<100;i++)
b[i]=0;
for(int i=0;i<n;i++)
{
if(a[i]=='a')
b[i]=21;
if(a[i]=='b')
b[i]=22;
if(a[i]=='c')
b[i]=23;
if(a[i]=='d')
b[i]=31;
if(a[i]=='e')
b[i]=32;
if(a[i]=='f')
b[i]=33;
if(a[i]=='g')
b[i]=41;
if(a[i]=='h')
b[i]=42;
if(a[i]=='i')
b[i]=43;
if(a[i]=='j')
b[i]=51;
if(a[i]=='k')
b[i]=52;
if(a[i]=='l')
b[i]=53;
if(a[i]=='m')
b[i]=61;
if(a[i]=='n')
b[i]=62;
if(a[i]=='o')
b[i]=63;
if(a[i]=='p')
b[i]=71;
if(a[i]=='q')
b[i]=72;
if(a[i]=='r')
b[i]=73;
if(a[i]=='s')
b[i]=74;
if(a[i]=='t')
b[i]=81;
if(a[i]=='u')
b[i]=82;
if(a[i]=='v')
b[i]=83;
if(a[i]=='w')
b[i]=91;
if(a[i]=='x')
b[i]=92;
if(a[i]=='y')
b[i]=93;
if(a[i]=='z')
b[i]=94;
}
int tmp=0;
for(int i=0,last=-1;i<a.size();i++)
{
tmp=b[i]/10;
for(int j=0;j<9;j++)
{
if(p[j]==tmp)
{
tmp=j+1;
break;
}
}
if(tmp==last)
cout<<"#";
int tmp2=b[i];
for(int k=0;k<tmp2;k++)
cout<<tmp;
last=tmp;
}
cout<<endl;
}
} #include<iostream>
#include<string>
using namespace std;
int main()
{
int p[9];
for(int i=0;i<9;i++)
p[i]=0;
while(cin>>p[0])
{
for(int i=1;i<9;i++)
cin>>p[i];
cin.get();
string a;
cin>>a;
int n=a.size();
double b[100];
for(int i=0;i<100;i++)
b[i]=0;
for(int i=0;i<n;i++)
{
if(a[i]=='a')
b[i]=2.1;
if(a[i]=='b')
b[i]=2.2;
if(a[i]=='c')
b[i]=2.3;
if(a[i]=='d')
b[i]=3.1;
if(a[i]=='e')
b[i]=3.2;
if(a[i]=='f')
b[i]=3.3;
if(a[i]=='g')
b[i]=4.1;
if(a[i]=='h')
b[i]=4.2;
if(a[i]=='i')
b[i]=4.3;
if(a[i]=='j')
b[i]=5.1;
if(a[i]=='k')
b[i]=5.2;
if(a[i]=='l')
b[i]=5.3;
if(a[i]=='m')
b[i]=6.1;
if(a[i]=='n')
b[i]=6.2;
if(a[i]=='o')
b[i]=6.3;
if(a[i]=='p')
b[i]=7.1;
if(a[i]=='q')
b[i]=7.2;
if(a[i]=='r')
b[i]=7.3;
if(a[i]=='s')
b[i]=7.4;
if(a[i]=='t')
b[i]=8.1;
if(a[i]=='u')
b[i]=8.2;
if(a[i]=='v')
b[i]=8.3;
if(a[i]=='w')
b[i]=9.1;
if(a[i]=='x')
b[i]=9.2;
if(a[i]=='y')
b[i]=9.3;
if(a[i]=='z')
b[i]=9.4;
}
int tmp=0;
for(int i=0,last=-1;i<a.size();i++)
{
tmp=(int)(b[i]+0.5);
for(int j=0;j<9;j++)
{
if(p[j]==tmp)
{
tmp=j+1;
break;
}
}
if(tmp==last)
cout<<"#";
int tmp2=(int)(b[i]*10+0.5);
for(int k=0;k<tmp2;k++)
cout<<tmp;
last=tmp;
}
cout<<endl;
}
}