写了半天要提交,发现要求java写郁闷...发到这边来看看吧.
class Program
{
/// <summary>
/// 两个大数相乘:char* multiply(char*,char*)。给了两个字符串,
/// 每个都是代表了一个很长的10进制表示的数, 比如 String str1 = "23456789009877666555544444";
/// String str2 = "346587436598437594375943875943875", 最后求出它们的乘积。 不用jdk的数学运算包BigInteger.或者类似的包。
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{ string s1 = "23456789009877666555544444";
string s2 = "346587436598437594375943875943875"; string res = LargeNumber(s1, s2); } static string LargeNumber(string s1, string s2)
{
string result = "";
int s1Length = s1.Length;
List<MutiValue> mutiList = new List<MutiValue>();
for (int i = 0; i <= s1Length - 1; i++)
{
int currenti = Convert.ToInt32(s1[i] + "");
List<MutiValue> tempMuti = SingleNumberMultiply(currenti, s2, s1Length - 1 - i);
mutiList.AddRange(tempMuti);
}
result = getResult(mutiList);
return result;
} static List<MutiValue> SingleNumberMultiply(int i1, string s2, int i1position)
{
List<MutiValue> mutiList = new List<MutiValue>();
int i2length = s2.Length;
for (int i = 0; i <= i2length - 1; i++)
{
int curi2 = Convert.ToInt32(s2[i] + "");
int itotalpos = i2length - 1 - i + i1position;
MutiValue muti = TwoSingleMuti(i1, curi2, itotalpos);
mutiList.Add(muti);
}
return mutiList;
}
static string getResult(List<MutiValue> values)
{
values.Sort(new MutiSort());
int length = values.Count;
StringBuilder sb = new StringBuilder();
int preMutiLevel = 0;
int preLargeValue = 0;
int preLevelValue = 0;
for (int i = 0; i < length; i++)
{
if (preMutiLevel == values[i].MutiLevel)
{
///相同倍率
preLargeValue += values[i].Level2;
preLevelValue += values[i].Level1;
}
else
{
int record = preLevelValue % 10;
sb.Insert(0, record);
///不同倍率,计算之前的数值
preLargeValue = preLevelValue / 10 + preLargeValue + values[i].Level1;
if (i == length - 1)
{
int secmaxvalue = preLargeValue % 10;
sb.Insert(0, secmaxvalue);
int maxval = preLargeValue / 10 + values[i].Level2;
if (maxval != 0)
sb.Insert(0, maxval);
}
preLevelValue = preLargeValue;
preLargeValue = values[i].Level2;
}
preMutiLevel = values[i].MutiLevel; }
return sb.ToString();
}
static MutiValue TwoSingleMuti(int i1, int i2, int iposition)
{
MutiValue mv = new MutiValue();
int res = i1 * i2;
mv.Level1 = res % 10;
mv.Level2 = res / 10;
mv.MutiLevel = iposition;
return mv;
}
class MutiSort : IComparer<MutiValue>
{
public int Compare(MutiValue x, MutiValue y)
{
return x.MutiLevel - y.MutiLevel;
}
}
class MutiValue
{
public int Level2 { get; set; }
public int Level1 { get; set; } /// <summary>
/// 倍率
/// </summary>
public int MutiLevel { get; set; }
}
}
class Program
{
/// <summary>
/// 两个大数相乘:char* multiply(char*,char*)。给了两个字符串,
/// 每个都是代表了一个很长的10进制表示的数, 比如 String str1 = "23456789009877666555544444";
/// String str2 = "346587436598437594375943875943875", 最后求出它们的乘积。 不用jdk的数学运算包BigInteger.或者类似的包。
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{ string s1 = "23456789009877666555544444";
string s2 = "346587436598437594375943875943875"; string res = LargeNumber(s1, s2); } static string LargeNumber(string s1, string s2)
{
string result = "";
int s1Length = s1.Length;
List<MutiValue> mutiList = new List<MutiValue>();
for (int i = 0; i <= s1Length - 1; i++)
{
int currenti = Convert.ToInt32(s1[i] + "");
List<MutiValue> tempMuti = SingleNumberMultiply(currenti, s2, s1Length - 1 - i);
mutiList.AddRange(tempMuti);
}
result = getResult(mutiList);
return result;
} static List<MutiValue> SingleNumberMultiply(int i1, string s2, int i1position)
{
List<MutiValue> mutiList = new List<MutiValue>();
int i2length = s2.Length;
for (int i = 0; i <= i2length - 1; i++)
{
int curi2 = Convert.ToInt32(s2[i] + "");
int itotalpos = i2length - 1 - i + i1position;
MutiValue muti = TwoSingleMuti(i1, curi2, itotalpos);
mutiList.Add(muti);
}
return mutiList;
}
static string getResult(List<MutiValue> values)
{
values.Sort(new MutiSort());
int length = values.Count;
StringBuilder sb = new StringBuilder();
int preMutiLevel = 0;
int preLargeValue = 0;
int preLevelValue = 0;
for (int i = 0; i < length; i++)
{
if (preMutiLevel == values[i].MutiLevel)
{
///相同倍率
preLargeValue += values[i].Level2;
preLevelValue += values[i].Level1;
}
else
{
int record = preLevelValue % 10;
sb.Insert(0, record);
///不同倍率,计算之前的数值
preLargeValue = preLevelValue / 10 + preLargeValue + values[i].Level1;
if (i == length - 1)
{
int secmaxvalue = preLargeValue % 10;
sb.Insert(0, secmaxvalue);
int maxval = preLargeValue / 10 + values[i].Level2;
if (maxval != 0)
sb.Insert(0, maxval);
}
preLevelValue = preLargeValue;
preLargeValue = values[i].Level2;
}
preMutiLevel = values[i].MutiLevel; }
return sb.ToString();
}
static MutiValue TwoSingleMuti(int i1, int i2, int iposition)
{
MutiValue mv = new MutiValue();
int res = i1 * i2;
mv.Level1 = res % 10;
mv.Level2 = res / 10;
mv.MutiLevel = iposition;
return mv;
}
class MutiSort : IComparer<MutiValue>
{
public int Compare(MutiValue x, MutiValue y)
{
return x.MutiLevel - y.MutiLevel;
}
}
class MutiValue
{
public int Level2 { get; set; }
public int Level1 { get; set; } /// <summary>
/// 倍率
/// </summary>
public int MutiLevel { get; set; }
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货