C#演算法題目自己的解答 約德爾測試
阿新 • • 發佈:2019-01-28
約德爾測試
蘭博和提莫閒聊之後,迴歸到了他們的正題,約德爾人的未來。
說起約德爾人的未來,黑默丁格曾經提出了一個約德爾測試,將約德爾人的歷史的每個階段都用一個字元表達出來。(包括可寫字元,不包括空格。)。然後將這個字串轉化為一個01串。轉化規則是如果這個字元如果是字母或者數字,這個字元變為1,其它變為0。然後將這個01串和黑默丁格觀測星空得到的01串做比較,得到一個相似率。相似率越高,則約德爾的未來越光明。
請問:相似率為多少?
輸入 | 樣例輸入 |
---|---|
每組輸入資料為兩行,第一行為有關約德爾人歷史的字串,第二行是黑默丁格觀測星空得到的字串。 | @!%12dgsa |
(兩個字串的長度相等,字串長度不小於1且不超過1000。) | 010111100 |
輸出 | 樣例輸出 |
---|---|
輸出一行,在這一行輸出相似率。用百分數表示。 | 66.67% |
(相似率為相同字元的個數/總個數,精確到百分號小數點後兩位。printf(“%%”);輸出一個%。) |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 約德爾測試
{
class Program
{
static void Main(string[] args)
{
string a = Console.ReadLine();
string b = Console.ReadLine();
int[] num1 = new int[a.Length];
int[] num2 = new int[b.Length];
int i = 0;
foreach (char c in b)
{
num2[i] = c - '0' ;
i++;
}
i = 0;
foreach (char c in a)
{
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z'))
{
num1[i] = 1;
}
else
num1[i] = 0;
i++;
}
double count = 0;
for (i = 0; i < a.Length; i++)
{
if (num1[i] == num2[i])
count++;
}
Console.WriteLine("{0:F2}%",count/a.Length*100);
Console.ReadLine();
}
}
}