c++ 알고리즘문제 질문입니다. (왜 오답으로 뜨는지 모르겠어요. 모든 경우의 수에 대해서 맞게 한것같은데 ㅠㅠ)
조회수 917회
https://algospot.com/judge/problem/read/CONVERT 이 문제를 풀고있는데요. 간단히 설명하면
1 kg -> 2.2046 pound
1 pound -> 0.4536 kg
1 liter -> 0.2642 gallon
1 gallon -> 3.7854 liter
으로 바꾸는 문제입니다.
예시 input은
5
1 kg
2 l
7 lb
3.5 g
0 l
예시 Output은
1 2.2046 lb
2 0.5284 g
3 3.1752 kg
4 13.2489 l
5 0.0000 g
입니다.
제가 쓴 코드로는 모든 경우의 수에서 잘 나오는데 왜 오답으로 처리되는지 모르겠습니다.
전체 코드를 올려서 정말 죄송합니다. ㅠㅠㅠ 그렇지만 많은 경우의 수를 대입해도 다 정답이 나오기에 너무 답답해서 올립니다.
#include <iostream>
#include <cstring>
#include <sstream>
#include <vector>
#include <ios>
#include <limits>
#include <cmath>
using namespace std;
string Convert(string data);
int main()
{
int N;
cin>>N;
/*buffer를 없애는 작업*/
cin.ignore(numeric_limits<streamsize>::max(),'\n');
int temp(N);
vector<string> Ansarr(N);
int i=0;
while(temp>0){
string A;
/*단순하게 cin을 이용하면 공백문자를 받아들일 수 가 없다!*/
getline(cin,A,'\n');
int len=A.length();
A=Convert(A);
Ansarr[i++]=A;
temp--;
}
i=0;
while(N>0){
/*i++이 계산되고 앞쪽 i가 출력되는 구조라 i+1이라 하면 안됨*/
cout<<i<<' '<< Ansarr[i++]<<endl;
N--;
}
}
string Convert(string data)
{
string Result,unit;
double ConResult;
if(data.back()=='g'){
if(data.at(data.length()-2)=='k'){
/*kg일때*/
unit="lb";
data.pop_back();
data.pop_back();
data.pop_back();
/*stoi는 integer로 atof는 double형으로 바꾸어준다*/
double temp=atof(data.c_str());
ConResult=temp*2.2046;
}
else{
/*g일때*/
unit="l";
data.pop_back();
data.pop_back();
double temp=atof(data.c_str());
ConResult=temp*3.7854;
}
}
else if(data.back()=='b'){
/*lb일때*/
unit="kg";
data.pop_back();
data.pop_back();
double temp=atof(data.c_str());
ConResult=temp*0.4536;
}
else{
/*ㅣ일때*/
unit="g";
data.pop_back();
data.pop_back();
double temp=atof(data.c_str());
ConResult=temp*0.2642;
}
double rounded_up=roundf(ConResult*10000)/10000;
Result=to_string(rounded_up);
Result.pop_back();
Result.pop_back();
Result=Result+" "+unit;
return Result;
}
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
댓글 입력