2018年2月15日 星期四

Lucky貓 一顆星_1

Lucky貓 - Q10340: All in All

給你2個字串s,t,請你寫一個程式判斷是否st的子字串。也就是說當你移走t字串中的某些字元後,剩下的字串就是s
Input
每筆測試資料一列。每列有2個字串,st,以空白分隔。
Output
對每一列輸入,請輸出是否st的子字串。
Sample Input
abc abc
sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter
Sample Output
Yes
Yes
No
Yes
No
            while (true)
            {
                string Input = Console.ReadLine();
                if (Input == "") return;
                string[] Anwser = Input.Split(' ');
                char[] check = Anwser[1].ToCharArray();
                int n = 0;
                bool Decided = true;
                foreach (char item in Anwser[0])
                {
                    if (Decided == false) continue;
                    for (int i = n; i < check.Length; i++)
                    {
                        if (item == check[i])
                        {
                            n = i + 1;
                            break;
                        }
                        if (i == check.Length - 1) Decided = false;
                    }
                }
                if (Decided == true) Console.WriteLine("YES\n");
                else Console.WriteLine("No\n");
            }


Lucky貓 - Q10473: Simple Base Conversion

   (十進位與十六進位轉換)
在這個問題中你被要求寫一個程式來做數字基底的轉換。給你一個10進位或16進位的數,請你把他轉換成16進位或10進位的數。16進位的數總是以0x當開頭,其他所有的數都被當成是10進位。輸入中不會有不合法的數。
Input
輸入含有多組測試資料。每組測試資料一列有一個不為負數的數,可能是一個10進位或16進位的數。請根據上述的敘訴判斷。這個數10進位的值一定小於 231 
若輸入為負的10進位數字時代表輸入結束。請參考 Sample Input
Output
對每組測試資料輸出一列。如果輸入的是10進位的數,請輸出其16進位的值。如果輸入是16進位的數,請輸出其10進位的值。
如同輸入一樣,輸出的16進位的數也請以 0x 開頭。請參考Sample Output

Sample Input
Sample Output
4
7
44
0x80685
-1
0x4
0x7
0x2C
525957



            while (true)
            {
                string input = Console.ReadLine(), remember = null;               
                char[] c = input.ToCharArray();
                if (c.Length == 1) Console.WriteLine("0x{0}", c[0]);
                else
                {
                    if (c[1] == 'x')
                    {
                        int score = 0;
                        for (int i = c.Length - 1; i >= 2; i--)
                        {
                            if (Convert.ToInt16(c[i]) >= 65)
                                score = score + ((Convert.ToInt32(c[i]) - 55)
                                    * Convert.ToInt32(Math.Pow(16, (c.Length - 1) - i)));
                            else
                                score = score + ((Convert.ToInt32(c[i].ToString()))
                                         * Convert.ToInt32(Math.Pow(16, (c.Length - 1) - i)));
                        }
                        Console.WriteLine(score);
                    }
                    else
                    {
                        for (int i = 0; i < c.Length; i++)
                            remember = remember + c[i];
                        int score = int.Parse(remember);
                        if (score < 0) return;
                        remember = null;
                        while (score >= 16)
                        {
                            if ((score % 16) > 9)
                                remember = Convert.ToChar((score % 16) + 55).ToString() + remember;
                            else
                                remember = (score % 16).ToString() + remember;
                            score = score / 16;
                        }
                        if (score > 9) remember = Convert.ToChar(score + 55).ToString() + remember;
                        else remember = score.ToString() + remember;
                        Console.WriteLine("0x{0}", remember);
                    }
                }
            }

Lucky貓 - Q11743: Credit Check

今日,用信用卡網路購物已經變得相當普遍,由於使用者可能打錯信用卡號,所以一般電子商務型網站都會對信用卡號作檢查。
其中一種錯誤檢查機制稱為Luhn algorithm,它可以把所有打錯一個位數的錯誤找出來,甚至於能挑出打錯多個位數的錯誤,它的檢查規則如下:
用一個例子來講解會比較方便,例如信用卡號(5181 2710 9900 0012):

   1. 將偶數位置上的數字乘2,也就是將 ( 5182719900012 )中粗體底線的數字乘2,得到10, 16, 4, 2, 18, 0, 0, 2。
   2. 將剛剛所得到的數字中每一個位數數值加總,即(1+0) + (1+6) + 4 + 2 + (1+8) + 0 + 0 + 2 = 25。將信用卡號中奇數位數的數字作加總,即1+1+7+0+9+0+0+2 = 20,再將兩數相加25+20=45。
   3. 45的個位數並非0,所以這個信用卡號並不合法。

本題請你用此法檢查信用卡號是否正確。

Input
輸入的第一列為整數N,表示測試資料的組數,接下來的N列分別為一個信用卡號,信用卡號有16個數目字,四個數字一組以一個空白字元隔開。
Output
若信用卡號是檢查合格的,請輸出"Valid",否則請輸出"Invalid"。
        Sample InputSample Output
        2
        5181 2710 9900 0012
        5181 2710 9900 0017
        Invalid
        Valid
        

        中文翻譯轉載自:Ruby兔的ACM園地


                    int N = int.Parse(Console.ReadLine());
                    for (int i = 0; i < N; i++)
                    {
                        string input = Console.ReadLine(), remember = null;
                        foreach (char item in input)
                            if (item != ' ') remember = remember + item;
                        char[] c = remember.ToCharArray();
                        string[] s = new string[8];
                        int sum = 0;
                        for (int j = 0; j < c.Length; j += 2)
                        {
                            s[j / 2] = (Convert.ToInt32(c[j].ToString()) * 2).ToString();
                            foreach (char item in s[j / 2])
                                sum = sum + int.Parse(item.ToString());
                        }
                        for (int j = 1; j < c.Length; j += 2)
                            sum = sum + int.Parse(c[j].ToString());
                        if (sum % 10 == 0) Console.WriteLine("Valid");
                        else Console.WriteLine("Invalid");
                    }



        2 則留言:

        WinFormTb02

        https://drive.google.com/drive/u/0/folders/1UwS9FZ3ELCOK6SAwirHrkxq3z_RSbxJt https://www.youtube.com/watch?v=k7IkIeww_U0&list=PLumjEWemD...