列表

详情


NC207573. SumoandEasySum

描述

Give a positive integer K and the following conditions:


Please help Sumo write a program to calculate , the result should be expressed as a simple fraction.

输入描述

The first line gives an integer , indicates that the following T test cases will be given.

Next T lines, each line gives a positive integer .

输出描述

For each test case, print a line to express the answer.

示例1

输入:

2
2
3

输出:

2
3/5

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 21ms, 内存消耗: 508K, 提交时间: 2020-06-06 15:47:46

#include<bits/stdc++.h>
using namespace std;
const long long mod = 1e9+7;
int t, k;
int main()
{
	for(cin >> t; t--; )
	{
		cin >> k;
		int mu = k*k - k - 1;
		int gcd = __gcd(k, mu);;
		k /= gcd, mu /= gcd;
		if(mu == 1)
			printf("%d\n", k);
		else
			printf("%d/%d\n", k, mu);
	}
}

C++(clang++11) 解法, 执行用时: 21ms, 内存消耗: 504K, 提交时间: 2020-12-24 17:28:13

#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int k;
        cin>>k;
        int a=k*k-k-1;
        if(k%a==0) cout<<k/a<<"\n"; else cout<<k/__gcd(k,a)<<"/"<<a/__gcd(k,a)<<"\n";
    }
}

Python3(3.5.2) 解法, 执行用时: 124ms, 内存消耗: 3540K, 提交时间: 2020-06-06 16:53:36

import math

_ = int(input())
for __ in range(_):
    k = int(input())
    p = k
    q = k * (k - 1) - 1
    g = math.gcd(p, q)
    p //= g
    q //= g
    if q == 1:
        print(p)
    else:
        print("{}/{}".format(p, q))

上一题