請教一下,我的transform函式問題到底出在哪裡:D?
debug好久都還是看不懂我的作法跟chatgpt的作法差在哪...
拜託各路大神了
#include <iostream> #include <string> #include <math.h> #include using namespace std; int swchInt(char a) { int reNum = 0; switch(a){ case 'I': reNum = 1; break; case 'V': reNum = 5; break; case 'X': reNum = 10; break; case 'L': reNum = 50; break; case 'C': reNum = 100; break; case 'D': reNum = 500; break; case 'M': reNum = 1000; break; } return reNum; } //------------------------------------我本來的做法 // int transform(string numStr) // { // int total = 0; // char numa, numb = '1'; // total += swchInt((char)numStr[0]); // for(char c:numStr) // { // if(numb == '1') // { // numb = c; // }else{ // numa = numb; // numb = c; // if(swchInt(numa) < swchInt(numb)) total -= 2*swchInt(numa); // else if(swchInt(numa) >= swchInt(numb)) total += swchInt(numb); // } // } // return total; // } //------------------------------------chatgpt作法 int transform(const string& numStr) { int total = 0; int prevValue = 0; for (char c : numStr) { int currentValue = swchInt(c); if (currentValue > prevValue) { total += currentValue - 2 * prevValue; } else { total += currentValue; } prevValue = currentValue; } return total; } //------------------------------------ string int_to_roman(int value) { std::map romanMap = {{1,"I"},{4,"IV"},{5,"V"},{9,"IX"},{10,"X"},{40,"XL"},{50,"L"},{90,"XC"},{100,"C"},{400,"CD"},{500,"D"},{900,"CM"},{1000,"M"}}; string romanNum = ""; for(auto it = romanMap.rbegin(); it != romanMap.rend(); it++) { while(value >= it->first) { value -= it->first; romanNum += it->second; } } return romanNum; } int main() { string numA, numB, input, outcome = ""; do{ cin>>numA; if(numA == "#") continue; cin>>numB; if(abs(transform(numA)-transform(numB)) != 0) cout< 0 && transform(numB) > 0) cout<<"ZERO"<<endl; } return 0; }