分类目录归档:Codeforces

Codeforces 71A Way Too Long Words

去年面谷歌的时候还被问过这道题。最后还是悲剧了,好伤心:sob:

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

namespace _71A
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            while (n-- != 0)
            {
                string word = Console.ReadLine();
                if (word.Length <= 10)
                {
                    Console.WriteLine(word);
                }
                else
                {
                    string abbr = word[0].ToString() + (word.Length - 2).ToString() + word[word.Length - 1].ToString();
                    Console.WriteLine(abbr);
                }
            }
        }
    }
}

Codeforces 4A Watermelon

判断是否为大于2的偶数即可。

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

namespace _4A
{
    class Program
    {
        static void Main(string[] args)
        {
            int w = Convert.ToInt32(Console.ReadLine());
            if (w > 2 && w % 2 == 0)
            {
                Console.WriteLine("YES");
            }
            else
            {
                Console.WriteLine("NO");
            }
        }
    }
}

Codeforces 1B Spreadsheets

首先需要判断输入是Excel格式还是RC格式的串。判断方法:如果在读入到数字之后又读到字母,就是RC格式;否则就是Excel格式。

转换时,Excel格式的列名相当于26进制的数,但不完全一致:Z代表26而不是0。需要在转换时注意一下trick。

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

namespace _1B
{
    class Program
    {

        static int mod26(int col)
        {
            int ret = col % 26 - 1;
            if (ret == -1)
            {
                ret += 26;
            }
            return ret;
        }

        static int CalcRCColName(string Excel_C)
        {
            int C = 0;
            for (int i = 0; i < Excel_C.Length; i++)
            {
                C *= 26;
                C += Excel_C[i] - 'A' + 1;
            }
            return C;
        }

        static string CalcExcelColName(int col)
        {
            string ret = "";
            while(true)
            {
                ret = ((char)('A' + (mod26(col)))).ToString() + ret;
                int adjust = 0;
                if (col % 26 == 0)
                {
                    adjust = 1;
                }
                col = col / 26 - adjust;
                if (col == 0 )
                    break;
            }
            return ret;
        }

        static string RCToExcel(string position)
        {
            int c_pos = position.IndexOf('C');
            int R = int.Parse(position.Substring(1, c_pos - 1));
            int C = int.Parse(position.Substring(c_pos + 1));
            string Excel_C = CalcExcelColName(C);
            return Excel_C + R.ToString();
        }

        static string ExcelToRC(string position)
        {
            int num_pos = position.IndexOfAny("0123456789".ToArray());
            string C = position.Substring(0, num_pos);
            string R = position.Substring(num_pos);
            int RC_C = CalcRCColName(C);

            return "R" + R + "C" + RC_C.ToString();
        }

        static bool IsExcelFormat(string position)
        {
            int state = 0;
            foreach(var c in position)
            {
                if (char.IsDigit(c))
                {
                    if (state == 0)
                        state = 1;
                }
                else
                {
                    if (state == 1)
                        return false;
                }
            }
            return true;
        }

        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            while(n-- != 0)
            {
                string pos = Console.ReadLine();
                if (IsExcelFormat(pos))
                {
                    Console.WriteLine(ExcelToRC(pos));
                }
                else
                {
                    Console.WriteLine(RCToExcel(pos));
                }
            }
        }
    }
}

Codeforces 1A Theatre Square

长和宽除以边长后向上取个整。

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

namespace _1A
{
    class Program
    {
        static Int64 calc(Int64 i, Int64 a)
        {
            return i / a + (i % a == 0 ? 0 : 1);
        }

        static void Main(string[] args)
        {
            Int64[] ints = Console.ReadLine().Split().Select(i => Int64.Parse(i)).ToArray();
            Int64 w = calc(ints[0], ints[2]);
            Int64 h = calc(ints[1], ints[2]);

            Console.WriteLine(w * h);
        }
    }
}