您的答案為: JOB DONE ? FINE! 正確答案為: JOB DONE ? FINE!
您的答案為: S O S 正確答案為: S O S
----------------------------------------------------
****************************************************
----------------------------------------------------
#include<iostream>
#include<map>
using namespace std;
map<string, char> dah={{".-", 'A'},{"-...",'B'},{"-.-.",'C'},{"-..",'D'},{".",'E'},{"..-.",'F'},{"--.",'G'},{"....",'H'},{"..",'I'},{".---",'J'},{"-.-",'K'},{".-..",'L'},{"--",'M'},{"-.",'N'},{"---",'O'},{".--.",'P'},{"--.-",'Q'},{".-.",'R'},{"...",'S'},{"-",'T'},{"..-",'U'},{"...-",'V'},{".--",'W'},{"-..-",'X'},{"-.--",'Y'},{"--..",'Z'},{"-----",'0'},{".----",'1'},{"..---",'2'},{"...--",'3'},{"....-",'4'},{".....",'5'},{"-....",'6'},{"--...",'7'},{"---..",'8'},{"----.",'9'},{".-.-.-",'.'},{"--..--",','},{"..--..",'?'},{".----.",'\''},{"-.-.--",'!'},{"-..-.",'/'},{"-.--.",'('},{"-.--.-",')'},{".-...",'&'}};
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int t,k;
cin>>t;
int x=t;
t++;
k=0;
while(t--){
string s,c="";
getline(cin,s);
if(k){cout<<"Message #"<<k<<'\n';}
for(int i=0;i<s.size();i++){
if(s[i]==' '){
cout<<dah.find(c)->second;
if(s[i+1]==' ')cout<<" ";
c="";
continue;
}
c+=s[i];
if(i==s.size()-1){
cout<<dah.find(c)->second;
break;
}
}
if(k){
if(k!=x)cout<<'\n';
cout<<'\n';
}
k++;
}
}
#0: 50% WA (line:4)
您的答案為: JOB DONE ? FINE! 正確答案為: JOB DONE ? FINE!#1: 50% WA (line:2)
您的答案為: S O S 正確答案為: S O S
----------------------------------------------------
****************************************************
----------------------------------------------------#include
#include
using namespace std;
map<string, char> dah={{".-", 'A'},{"-...",'B'},{"-.-.",'C'},{"-..",'D'},{".",'E'},{"..-.",'F'},{"--.",'G'},{"....",'H'},{"..",'I'},{".---",'J'},{"-.-",'K'},{".-..",'L'},{"--",'M'},{"-.",'N'},{"---",'O'},{".--.",'P'},{"--.-",'Q'},{".-.",'R'},{"...",'S'},{"-",'T'},{"..-",'U'},{"...-",'V'},{".--",'W'},{"-..-",'X'},{"-.--",'Y'},{"--..",'Z'},{"-----",'0'},{".----",'1'},{"..---",'2'},{"...--",'3'},{"....-",'4'},{".....",'5'},{"-....",'6'},{"--...",'7'},{"---..",'8'},{"----.",'9'},{".-.-.-",'.'},{"--..--",','},{"..--..",'?'},{".----.",'\''},{"-.-.--",'!'},{"-..-.",'/'},{"-.--.",'('},{"-.--.-",')'},{".-...",'&'}};
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int t,k;
cin>>t;
int x=t;
t++;
k=0;
while(t--){
string s,c="";
getline(cin,s);
if(k){cout<<"Message #"<<k<<'\n';}
for(int i=0;i<s.size();i++){
if(s[i]==' '){
cout<<dah.find(c)->second;
if(s[i+1]==' ')cout<<" ";
c="";
continue;
}
c+=s[i];
if(i==s.size()-1){
cout<<dah.find(c)->second;
break;
}
}
if(k){
if(k!=x)cout<<'\n';
cout<<'\n';
}
k++;
}
}
cin>>t>>ws;
k=1;
while(t--){
string s,c="";
getline(cin,s);
cout<<"Message #"<<k<<'\n';
for(int i=0;i<s.size();i++){
if(s[i]==' '){
cout<<dah.find(c)->second;
if(s[i+1]==' '){cout<<' ';i++;}
c="";
continue;
}
c+=s[i];
if(i==s.size()-1){
cout<<dah.find(c)->second;
}
}
cout<<'\n';
k++;
}
改成這樣就可以了 然後你的map不完整 getline以前加一個cin>>ws可以避免讀到空的字串(好像也可以用cin.ignore()??) 看一些人有用過