浙江大学PAT---Basic Level---1005----继续(3n+1)猜想

题目:http://pat.zju.edu.cn/contests/pat-b-practise/1005

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PAT_1005
{
    class Program
    {
        static void Main(string[] args)
        {
            int num;
            string input;

            //输入
            //
            num=Int32.Parse(Console.ReadLine());
            input=Console.ReadLine();

            //转换
            //
            string[] inputs = new string[num];
            int[] data = new int[num];
            inputs = input.Split(' ');
            for (int i = 0; i < num; i++)
            {
                data[i] = Int32.Parse(inputs[i]);
            }

            //计算关键数
            //
            for (int i = 0; i < num; i++)
            {
                int temp = data[i];
                while (temp> 1)
                {
                    temp = cutdata(temp);
                    for (int j = 0; j < num; j++)
                    {
                        if (data[j] == temp)
                            data[j] = 1;
                    }
                }
            }

            int[] sort_data=sortdata(data);

            print_data(sort_data);
            Console.ReadKey();
        }

        /// <summary>
        /// 砍数
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        private static int cutdata(int num)
        {
            if (num == 1)
                return 0;
            else if (num % 2 == 0)
                return (int)(num / 2);
            else
                return (int)((num + num + num + 1) / 2);
        }

        /// <summary>
        /// 冒泡排序
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private static int[] sortdata(int[] data)
        {
            int count = 0;
            int[] sort_data=new int[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                if (data[i] > 1)
                {
                    sort_data[count]=data[i];
                    count++;
                }
            }

            int temp;
            for (int i = 0; i < count; i++)
            {
                for (int j = i + 1; j < count; j++)
                {
                    if (sort_data[j] > sort_data[i])
                    {
                        temp = sort_data[i];
                        sort_data[i] = sort_data[j];
                        sort_data[j] = temp;
                    }
                }
            }

            data = new int[count];
            Array.Copy(sort_data, 0, data, 0, count);

            return data;
        }

        /// <summary>
        /// 打印数组
        /// </summary>
        /// <param name="data"></param>
        private static void print_data(int[] data)
        {
            for (int i = 0; i < data.Length-1; i++)
            {
                Console.Write("{0} ", data[i]);
            }
            Console.Write(data[data.Length - 1]);
        }
    }
}


你可能感兴趣的:(浙江大学PAT---Basic Level---1005----继续(3n+1)猜想)