#13330: 第三個階段的檢測過不去,求幫忙


ysh58168@gmail.com (楊昊天)

學校 : 不指定學校
編號 : 74706
來源 : []
最後登入時間 :
2018-01-22 17:41:24
a038. 數字翻轉 | From: [110.50.137.84] | 發表日期 : 2018-02-03 15:46

#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

求幫忙謝謝!!!

 
#13605: Re:第三個階段的檢測過不去,求幫忙


snakeneedy (蛇~Snake)

學校 : 國立高雄師範大學附屬高級中學
編號 : 7661
來源 : [114.40.8.251]
最後登入時間 :
2023-01-25 19:16:06
a038. 數字翻轉 | From: [218.164.125.30] | 發表日期 : 2018-03-29 01:18

#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")

不知道這樣有沒有回答到你的問題。

 
ZeroJudge Forum