int[] _Temp = new int[] { 1, 1, 4, 3, 3 }; 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]; }
你把所有的int换成 double不就好了~~
谢谢,如果是{s,s,d,c,c} 那又如何{ss,d,cc}
int[] ints = new int[] { 2, 3, 5, 5, 1 }; 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);
string[] ints = new string[] { "2", "3", "5", "5", "1" }; 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); //改下变量类型就好
net3.0以上版本直接使用linq就可以了 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
有点乱不过功能实现了。int[] a = { 1, 3, 4,4,1, 1, 3, 3 }; List<int> aa = new List<int>(); 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]); } }
少写了个变量:int len = 0;cou后面
晕了,int len = -1;
妈的,我认为这个有点简单却用了一上午的时间,唉~~~~~~~~~~ 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_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; }
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;
}