在工作过程中,把开发过程较好的一些内容片段备份一次,如下内容是关于C# 通过Luhn算法验证信用卡卡号是否有效的内容,应该是对小伙伴们也有好处。
using System;
using System.Linq;
namespace Luhn
{
class Program
{
public static bool luhn(long n)
{
long nextdigit, sum = 0;
bool alt = false;
while (n != 0)
{
nextdigit = n % 10;
if (alt)
{
nextdigit -= (nextdigit > 9) ? 9 : 0;
}
sum += nextdigit;
alt = !alt;
n /= 10;
}
return (sum % 10 == 0);
}
public static bool luhnLinq(long n)
{
string s = n.ToString();
return s.Select((c, i) => (c - '0') << ((s.Length - i - 1) & 1)).Sum(n => n > 9 ? n - 9 : n) % 10 == 0;
}
static void Main(string[] args)
{
long[] given = new long[] {49927398716, 49927398717, 1234567812345678, 1234567812345670};
foreach (long num in given)
{
string valid = (luhn(num)) ? " is valid" : " is not valid";
Console.WriteLine(num + valid);
}
}
}
}
运行上面的代码,输出结果如下
49927398716 is valid
49927398717 is not valid
1234567812345678 is not valid
1234567812345670 is valid
网友评论