Official

C - Cut .0 Editorial by en_translator


Solution 1. In fact it works

In case of C++, one can simply receive \(X\) as a double type and print it as is without specifying the format to correctly answer the problem. This is because it never prints trailing 0. Other languages may have similar features.
However, this method is not always applicable. Let us also consider other ways.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  double x;
  cin >> x;
  cout << x << "\n";
  return 0;
}

Solution 2. Treat as a string problem

Receive \(X\) as a string, apply the following process, and print it; then your code will be accepted.

  • While the last character is 0, repeatedly remove it.
  • Then, remove trailing . if exists.

This works because the input guarantees that the input has a decimal point, and thus removing a trailing 0 always remove one in the decimal part.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  string x;
  cin >> x;
  while(x.back()=='0'){x.pop_back();}
  if(x.back()=='.'){x.pop_back();}
  cout << x << "\n";
  return 0;
}

posted:
last update: