二进制
666:1010011010
888:1101111000
varint:
666:1001101000000101
888:1111100000000110
ZiaZag
using System;
namespace ConsoleApp3
{
class Program
{
static int[] HexNum = {0x268cb43, 0x7ff, 0x2b7b, 0x123a, 0xf6, 0x282};
static void Main(string[] args)
{
foreach (int nNum in HexNum)
{
int nValue = Convert.ToInt32(nNum);
int nOut = (nValue >> 1) ^ -(nValue & 1);
Console.WriteLine(nOut);
}
}
}
}
穿越峡谷
解题思路:
贪心算法,从第1位开始:每个位置都计算自己能达到的最远距离,同时每个位置要判断自己是否可达,
也就是本位置需要在当前最远能到达的距离中。最终计算出来能到达的最远距离,与数组长度比较即可。
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“输入数字数组(包含中括号):”);
string strRead = Console.ReadLine().Trim();
string[] arrStrNum = null;
int[] arrIntNum = null;
bool bCanJump = false;
strRead = strRead.Substring(1, strRead.Length - 2);
arrStrNum = strRead.Split(",");
arrIntNum = new int[arrStrNum.Length];
for (int i = 0; i < arrStrNum.Length; i++)
{
arrIntNum[i] = Convert.ToInt32(arrStrNum[i]);
}
bCanJump = CanJump(arrIntNum);
Console.WriteLine(bCanJump.ToString().ToLower());
}
/*
解题思路:
贪心算法,从第1位开始:每个位置都计算自己能达到的最远距离,同时每个位置要判断自己是否可达,
也就是本位置需要在当前最远能到达的距离中。最终计算出来能到达的最远距离,与数组长度比较即可。
*/
static bool CanJump(int[] arrNum)
{
int nLen = arrNum.Length;
if (nLen == 1)
{
return true;
}
// 覆盖范围
int nCoverRange = arrNum[0];
for (int i = 1; i <= nCoverRange; i++)
{
// 更新下:当前位置能达到的最远位置
nCoverRange = Math.Max(nCoverRange, i + arrNum[i]);
// 如果最远位置达到最后一位,则能通过
if (nCoverRange >= nLen - 1)
{
return true;
}
}
return false;
}
}
}
C求P
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“输入C(百分比):”);
string strRead = Console.ReadLine().Trim();
double dReadNum = 0d;
string strResult = null;
strRead = strRead.Substring(0, strRead.Length - 1);
dReadNum = Convert.ToDouble(strRead);
strResult = GetP(dReadNum).ToString() + "%";
Console.WriteLine(strResult);
}
static int GetP(double dC)
{
int nCount = (int)Math.Ceiling(100.0d / dC);
if (nCount == 1)
{
return 100;
}
double dNum;
double dSum = 0;
double dResult = 0;
for (int i = 0; i < nCount; i++)
{
double dKey = Math.Min(100, (dC * (i + 1)));
dNum = (1 - dSum / 100) * dKey;
dSum = dSum + dNum;
dResult = dResult + dNum * (i + 1);
}
return (int)Math.Round(10000 / dResult);
}
}
}
P求C
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“输入P(百分比):”);
string strRead = Console.ReadLine().Trim();
int nReadNum = 0;
string strResult = null;
strRead = strRead.Substring(0, strRead.Length - 1);
nReadNum = (int)Convert.ToDouble(strRead);
strResult = GetC(nReadNum).ToString() + "%";
Console.WriteLine(strResult);
}
static int GetP(double dC)
{
int nCount = (int)Math.Ceiling(100.0d / dC);
if (nCount == 1)
{
return 100;
}
double dNum = 0;
double dSum = 0;
double dResult = 0;
for (int i = 0; i < nCount; i++)
{
double dKey = Math.Min(100, (dC * (i + 1)));
dNum = (1 - dSum / 100) * dKey;
dSum = dSum + dNum;
dResult = dResult + dNum * (i + 1);
}
return (int)Math.Round(10000 / dResult);
}
static int GetC(int nP)
{
double dLow = 0.0d;
double dHigh = 100.0d;
int nResult = 0;
while (dLow <= dHigh)
{
double dMid = (dLow + dHigh) / 2;
int nResultP = GetP(dMid);
if (nResultP == nP)
{
nResult = (int)Math.Round(dMid);
break;
}
if (nResultP < nP)
{
dLow = dMid + 1;
}
else
{
dHigh = dMid - 1;
}
}
return nResult;
}
}
}
UTF8
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“输入数据:”);
string strRead = Console.ReadLine().Trim();
string[] arrStrUTF8 = strRead.Split(","); ;
byte[] arrByteUTF8 = new byte[arrStrUTF8.Length];
for(int i = 0; i < arrStrUTF8.Length; i++)
{
arrByteUTF8[i] = Convert.ToByte(arrStrUTF8[i], 16);
}
Console.WriteLine(ValidUTF8(arrByteUTF8) ? 1 : 0);
}
static bool ValidUTF8(byte[] arrUTF8)
{
int n = 0;
for (int i = 0; i < arrUTF8.Length; i++)
{
byte b = arrUTF8[i];
if (n > 0)
{
if (b >> 6 != 0b10) return false;
n--;
}
else if (b >> 7 == 0)
{
n = 0;
}
else if (b >> 5 == 0b110)
{
n = 1;
}
else if (b >> 4 == 0b1110)
{
n = 2;
}
else if (b >> 3 == 0b11110)
{
n = 3;
}
else
{
return false;
}
}
return n == 0;
}
}
}
Unicode
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“输入数据:”);
string strRead = Console.ReadLine().Trim();
string[] arrStrUTF8 = strRead.Split(","); ;
byte[] arrByteUTF8 = new byte[arrStrUTF8.Length];
for (int i = 0; i < arrStrUTF8.Length; i++)
{
arrByteUTF8[i] = Convert.ToByte(arrStrUTF8[i], 16);
}
Console.WriteLine(GetUnicodeCount(arrByteUTF8));
}
static int GetUnicodeCount(byte[] arrUTF8)
{
int n = 0;
int nCount = 0;
for (int i = 0; i < arrUTF8.Length; i++)
{
byte b = arrUTF8[i];
if (n > 0)
{
if (b >> 6 != 0b10) return 0;
n--;
}
else if (b >> 7 == 0)
{
n = 0;
nCount++;
}
else if (b >> 5 == 0b110)
{
n = 1;
nCount++;
}
else if (b >> 4 == 0b1110)
{
n = 2;
nCount++;
}
else if (b >> 3 == 0b11110)
{
n = 3;
nCount++;
}
else
{
return 0;
}
}
return nCount;
}
}
}
二进制
256:100000000
257:100000001
511:111111111
512:1000000000
二次幂
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“输入数据:”);
string strRead = Console.ReadLine().Trim();
long nReadNum = Convert.ToInt64(strRead);
Console.WriteLine(IsPower2(nReadNum) ? 1 : 0);
}
static bool IsPower2(long nNum)
{
if (nNum < 1)
{
return false;
}
return (nNum & (nNum - 1)) == 0;
}
}
}