#include <iostream>
#include <string>
#include <sstream>
#include<algorithm>
using namespace std;
int main()
{
string input;
stringstream answer;
int output;
while (cin>>input)
{
int length = input.length();
if (input[length-1]!='0')
{
reverse(input.begin(), input.end());
cout << input << endl;
}
else
{
answer << input;
answer >> output;
output = output / 10;
//cout << "out" << output << endl;
answer.str("");
answer.clear();
answer << output;
answer >> input;
reverse(input.begin(), input.end());
cout << input << endl;
answer.str("");
answer.clear();
}
}
}
這是我的程式碼,第三階段的檢測過不去,大概知道為甚麼。
輸入002540會變成452,而不是45200
求幫忙謝謝!!!
#include <iostream> #include <string> #include <sstream> #include<algorithm> using namespace std; int main() { string input; stringstream answer; int output; while (cin>>input) { int length = input.length(); if (input[length-1]!='0') {
cout << "case 1" << end; // edited reverse(input.begin(), input.end()); cout << input << endl; } else {
cout << "case 2" << end; // edited answer << input; answer >> output; output = output / 10; //cout << "out" << output << endl; answer.str(""); answer.clear(); answer << output; answer >> input; reverse(input.begin(), input.end()); cout << input << endl; answer.str(""); answer.clear(); } } }
稍微改了一下你的程式碼,可以測得
00102 → "case 1" → 20100
10200 → "case 2" → 0201
0010200 → "case 2" → 0201
高位數的 0
要在讀成數字時捨棄,"case 1" 中沒做,"case 2" 中有做。
低位數的 0
要在數字反轉後捨棄,因為反轉後變成高位數,"case 2"的 output = output / 10;
只捨棄一個 0,但可能有多個 0。(不會進到 "case 1")
不知道這樣有沒有回答到你的問題。