1048 数字加密 c#
因为一些需要,以后都改成csharp版本。初次上手c#,还有些不适应,写的可能比较繁琐,慢慢来吧。
这个地方有两个坑
- 需要考虑a的长度比b大的时候进行补位。在b字符串前面加0
- 好像c#的string没有自带的reverse函数,只能先转为char[],再利用array的reverse进行字符串反转
using System;
using System.Collections.Generic;
using System.Text;
namespace PTA乙级
{
class _1048_数字加密
{
static void Main(string[] args)
{
string[] input = Console.ReadLine().Split(' ');
int j = input[1].Length;
if (input[0].Length > input[1].Length)
{
//这里犯了一个错误,input[1].Length是动态增长的,需要用一个变量记录初始的长度
for (int i = 0; i < input[0].Length - j; i++)
//for (int i = 0; i < input[0].Length - input[1].Length; i++)
input[1] = '0'+ input[1];
}
//int a = int.Parse(input[0]);
//int b = int.Parse(input[1]);
char[] a = input[0].ToCharArray();
char[] b = input[1].ToCharArray();
char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K' };
List<char> result = new List<char>();
Array.Reverse(a);
Array.Reverse(b);
//测试点2,5没过,如果a的位数大于b需要补0
for (int i=0;i<b.Length;i++)
{
if(i>= a.Length)
{
result.Add(b[i]);
continue;
}
if((i%2==0))
{
//如果是奇数位
int tmp = (b[i] - '0' + a[i] - '0') % 13;
result.Add(arr[tmp]);
}
else
{
int tmp = b[i] - a[i];
if (tmp < 0) tmp += 10;
result.Add(arr[tmp]);
}
}
if(a.Length>b.Length)
{
}
result.Reverse();
for (int i = 0; i < result.Count; i++)
Console.Write(result[i]);
}
}
}