結果

問題 No.750 Frac #1
ユーザー HimatsubushinHimatsubushin
提出日時 2018-12-17 13:49:40
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 25 ms / 1,000 ms
コード長 2,193 bytes
コンパイル時間 930 ms
コンパイル使用メモリ 109,520 KB
実行使用メモリ 24,304 KB
最終ジャッジ日時 2023-10-25 23:28:25
合計ジャッジ時間 2,571 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
24,304 KB
testcase_01 AC 23 ms
24,300 KB
testcase_02 AC 24 ms
24,304 KB
testcase_03 AC 23 ms
24,304 KB
testcase_04 AC 24 ms
24,304 KB
testcase_05 AC 23 ms
24,304 KB
testcase_06 AC 24 ms
24,304 KB
testcase_07 AC 23 ms
24,304 KB
testcase_08 AC 23 ms
24,304 KB
testcase_09 AC 24 ms
24,304 KB
testcase_10 AC 24 ms
24,304 KB
testcase_11 AC 24 ms
24,304 KB
testcase_12 AC 24 ms
24,304 KB
testcase_13 AC 23 ms
24,304 KB
testcase_14 AC 23 ms
24,300 KB
testcase_15 AC 23 ms
24,292 KB
testcase_16 AC 24 ms
24,304 KB
testcase_17 AC 23 ms
24,304 KB
testcase_18 AC 24 ms
24,300 KB
testcase_19 AC 23 ms
24,304 KB
testcase_20 AC 24 ms
24,304 KB
testcase_21 AC 23 ms
24,304 KB
testcase_22 AC 23 ms
24,304 KB
testcase_23 AC 23 ms
24,304 KB
testcase_24 AC 24 ms
24,304 KB
testcase_25 AC 25 ms
24,304 KB
testcase_26 AC 24 ms
24,304 KB
testcase_27 AC 25 ms
24,304 KB
testcase_28 AC 24 ms
24,292 KB
testcase_29 AC 24 ms
24,304 KB
testcase_30 AC 25 ms
24,304 KB
testcase_31 AC 25 ms
24,304 KB
testcase_32 AC 24 ms
24,304 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;

namespace No750_Frac_1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] a = new int[n];
            int[] b = new int[n];
            string[] data = new string[2];
            int[] value = new int[n];
            int[] rank = new int[n];
            int lcm;

            for (int i = 0; i < n; i++)
            {
                data = Console.ReadLine().Split(' ');
                a[i] = int.Parse(data[0]);
                b[i] = int.Parse(data[1]);
                rank[i] = i;
            }

            if (n > 1)
            {
                lcm = Lcm(b);
                for (int i = 0; i < n; i++)
                    value[i] = a[i] * lcm / b[i];

                Sort(value, ref rank);

            }

            for (int i = 0; i < n; i++)
                Console.WriteLine(a[rank[i]] + " " + b[rank[i]]);
        }

        static void Sort(int[] value, ref int[] rank)
        {
            int n = value.Length;

            for (int i = 0; i < n - 1; i++)
            {
                for (int j = i; j < n; j++)
                {
                    if (value[i] < value[j])
                    {
                        Swap(ref value[i], ref value[j]);
                        Swap(ref rank[i], ref rank[j]);
                    }
                }
            }
        }

        static int Lcm(int[] num)
        {
            int result = Lcm(num[0], num[1]);

            if (num.Length > 2)
                for (int i = 2; i < num.Length - 1; i++)
                    result = Lcm(result, num[i]);

            return result;
        }

        static int Lcm(int a, int b)
        {
            return a * b / Gcd(a, b);
        }

        static int Gcd(int a, int b)
        {
            if (a < b)
                Swap(ref a, ref b);

            while (b != 0)
            {
                int rem = a % b;
                a = b;
                b = rem;
            }

            return a;
        }

        static void Swap(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;
        }
    }
}
0