結果

問題 No.1464 Number Conversion
ユーザー square1001square1001
提出日時 2020-07-03 20:22:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,255 bytes
コンパイル時間 620 ms
コンパイル使用メモリ 66,432 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-15 03:53:36
合計ジャッジ時間 2,043 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 1 ms
4,368 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 1 ms
4,372 KB
testcase_08 AC 1 ms
4,372 KB
testcase_09 AC 1 ms
4,368 KB
testcase_10 AC 2 ms
4,368 KB
testcase_11 AC 1 ms
4,372 KB
testcase_12 AC 2 ms
4,368 KB
testcase_13 AC 2 ms
4,368 KB
testcase_14 AC 2 ms
4,372 KB
testcase_15 AC 1 ms
4,368 KB
testcase_16 AC 2 ms
4,372 KB
testcase_17 AC 2 ms
4,372 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 1 ms
4,368 KB
testcase_21 AC 2 ms
4,372 KB
testcase_22 AC 2 ms
4,368 KB
testcase_23 AC 2 ms
4,368 KB
testcase_24 AC 1 ms
4,368 KB
testcase_25 AC 2 ms
4,372 KB
testcase_26 AC 2 ms
4,368 KB
testcase_27 AC 1 ms
4,368 KB
testcase_28 AC 1 ms
4,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <cassert>
#include <iostream>
using namespace std;
bool isvalid(string S) {
	if(S == "") return false;
	for(int i = 0; i < S.size(); ++i) {
		if(!('0' <= S[i] && S[i] <= '9') && S[i] != '.') {
			return false;
		}
	}
	int period_cnt = 0;
	for(int i = 0; i < S.size(); ++i) {
		if(S[i] == '.') ++period_cnt;
	}
	if(period_cnt >= 2) return false;
	if(S.front() == '.' || S.back() == '.') return false;
	if(S != "0" && S[0] == '0' && S[1] != '.') return false;
	int period_pos = S.size();
	if(period_cnt == 1) {
		for(int i = 0; i < S.size(); ++i) {
			if(S[i] == '.') period_pos = i;
		}
	}
	if(S.size() - period_pos > 8 + 1) return false; // more than 8 digits after period
	if(period_pos >= 8) return false; // >= 10000000
	int x = 0;
	for(int i = 0; i < period_pos; ++i) {
		x = x * 10 + (S[i] - '0');
	}
	if(x > 1000000) return false;
	return true;
}
long long gcd(long long x, long long y) {
	if(y == 0) return x;
	return gcd(y, x % y);
}
int main() {
	string S;
	cin >> S;
	assert(isvalid(S));
	long long x = 0, y = 0;
	for(int i = 0; i < S.size(); ++i) {
		if(S[i] == '.') y = 1;
		else x = x * 10 + (S[i] - '0'), y *= 10;
	}
	if(y == 0) y = 1;
	long long g = gcd(x, y);
	cout << x / g << "/" << y / g << endl;
	return 0;
}
0