求一个算法,我有一个数组{1,1,4,3,3},或{2,3,5,5,1}{1,1,4,3,3}:找出相同的数,并相加存下来{2,4,6}{2,3,5,5,1}:{2,3,10,1}
解决方案 »
- 在windows服务中获取另一个程序运行后的结果问题。
- 请高手指教,Hashtable ,DataSet ,"like '%";到底是那个地方的语法写错了》???????????????????????????????????
- 那位帮写一个简单的C#函数,将货币转换成分cents
- 在线等,C#新手问题,解决即给分
- iis管理中的网站和虚拟目录各代表什么意思啊!它们起什么作用呢?
- DataSet HasChanges 问题
- TreeView 事件失效问题 C#
- c#中间利用arraylist对于一个结构排序,如何制定排序地依据项??
- (新手)sql 语句的赋值问题
- 关于名字空间
- c#中多个文件上传的问题
- xmlhttp.send("")异常,C#开发
IList<int> _Value = new List<int>();
IList<int> _ValueCount = new List<int>();
for (int i = 0; i != _Temp.Length; i++)
{
int _Index = _Value.IndexOf(_Temp[i]);
if (_Index == -1)
{
_Value.Add(_Temp[i]);
_ValueCount.Add(1);
}
else
{
_ValueCount[_Index]++;
}
} int[] _IntValue = new int[_Value.Count]; for (int i = 0; i != _Value.Count; i++)
{
_IntValue[i] = _Value[i] * _ValueCount[i];
}
那又如何{ss,d,cc}
int length = ints.Length;
int[] ints2 = new int[length];
int length2 = 0;
for (int i = 0; i < length; i++)
{
int thisint = ints[i];
bool isget = true;
for (int j = 0; j < length2; j++)
{
if (ints[j] == thisint)
{
isget = false;
break;
}
}
if (isget)
{
int thisintsum = thisint;
for (int j = i + 1; j < length; j++)
{
if (ints[j] == thisint)
{
thisintsum += thisint;
}
}
ints2[length2] = thisintsum;
length2++;
}
}
int[] ints3 = new int[length2];
Array.Copy(ints2, 0, ints3, 0, length2);
int length = ints.Length;
string[] ints2 = new string[length];
int length2 = 0;
for (int i = 0; i < length; i++)
{
string thisint = ints[i];
bool isget = true;
for (int j = 0; j < length2; j++)
{
if (ints[j] == thisint)
{
isget = false;
break;
}
}
if (isget)
{
string thisintsum = thisint;
for (int j = i + 1; j < length; j++)
{
if (ints[j] == thisint)
{
thisintsum += thisint;
}
}
ints2[length2] = thisintsum;
length2++;
}
}
string[] ints3 = new string[length2];
Array.Copy(ints2, 0, ints3, 0, length2);
//改下变量类型就好
var Girls = new[] {"Christine", "Eva", "Jean", "Novia", "Winnie"};
var PMs = new[] {"Eva", "Novia", "Vincent", "Williams", "Winnie"};
// 是 Girl 且是 PM (交集)
foreach (var s in Girls.Intersect(PMs))
{
Console.WriteLine(s
}
Console.WriteLine();如果是net2.0的话,请参考
http://blog.blueshop.com.tw/uni2tw/archive/2008/02/14/54315.aspx
List<int> bb = new List<int>();
List<int> ll = new List<int>();
for (int i = 0; i < a.Length; i++)
{
aa.Add(a[i]);
}
int cou = 0;
for (int i = 0; i < aa.Count; i++)
{
while ((len = aa.IndexOf(aa[i], ++len)) >= 0)
{
cou++;
ll.Add(aa[len]);
}
if (cou != 0)
{
bb.Add(cou * aa[i]);
}
else
{
bb.Add(aa[i]);
}
cou = 0; for (int j = 0; j < ll.Count; j++)
{
aa.Remove(ll[j]);
}
}
List<string> int_33 = new List<string> ( );//重复的数
List<string> int_11 = new List<string> ( );//不重复的数 List<string> ints = new List<string> { "2" , "2" , "1" , "5" , "5" , "2" , "2","3" };
List<string> ints_1 = new List<string> { "2" , "5" };
foreach ( string s in ints_1 )
{
foreach ( string str in ints )
{
if ( s == str )
int_11.Add ( str );
}
string sTmp = "";
if ( int_11.Count > 0 )
{
foreach ( string stri in int_11 )
{
sTmp += stri;
}
int_33.Add ( sTmp );
int_11.Clear ( );
}
}
bool b = false;
foreach ( string s in ints )
{
foreach ( string str in ints_1 )
{
if ( s == str )
{
b = true;
continue;
}
}
if ( b == false )
{
int_11.Add ( s );
}
b = false;
}
还是要谢谢各位,把分数分了
List<string> int_11 = new List<string> ( );//不重复的数 List<string> ints = new List<string> { "2" , "2" , "1" , "5" , "5" , "2" , "2","3" };
List<string> ints_1 = new List<string> { "2" , "5" };
foreach ( string s in ints_1 )
{
foreach ( string str in ints )
{
if ( s == str )
int_11.Add ( str );
}
string sTmp = "";
if ( int_11.Count > 0 )
{
foreach ( string stri in int_11 )
{
sTmp += stri;
}
int_33.Add ( sTmp );
int_11.Clear ( );
}
}
bool b = false;
foreach ( string s in ints )
{
foreach ( string str in ints_1 )
{
if ( s == str )
{
b = true;
continue;
}
}
if ( b == false )
{
int_11.Add ( s );
}
b = false;
}