איך טראכט אז די היימישע קעפ קענען מיר די בעסטע העלפן
מיין פראבלעם איז אזוי, אויב האב איך אסאך באקסעס וואס יעדע באקס קען האבן אנדערע קוואנטיטי למשל
באקס 1 האט 12
באקס 2 האט 4
באקס 3 האט 7
באקס 4 האט 2
באקס 5 האט 8
באקס 6 האא 6
יעצט דארף איך האבן 25, און איך דארף עס האבן פון די אלע.
קוקנדיג זעה איך אז באקס 1,3,6 (12+7+6) איז 25, אבער וויאזוי מאך איך דאס אין קאוד?
איך שרייב אין #C און SQL
אבער איך קען לייענען אלע אנדערע שפראכן (מער ווייניגער) אויב האט איר א וועג נישט קיין חילוק אין וועלעכע שפראך, ביטע מיר ענטפערן.
א גרויסן ישר כח פון פאראויס (איך ברעך מיר שוין דעם קאפ שעה'ן, איך ווייס נישטאמאל וויאזוי צו סירטשון פארדעם אין גוגל)
גאסט
איך דארף אן אלגאריטים
די אחראים: זייער נייגעריג,אחראי,thefact
- פאטאקי08
- שר עשרת אלפים
- תגובות: 13865
- זיך איינגעשריבן אום: דאנערשטאג אוגוסט 09, 2007 1:30 pm
- פארבינד זיך:
דאס ארבעט אין עקסעס
קאוד פאר T-SQL
קאוד: וועל אויס אלע
Private Sub Command0_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Table1")
Dim i As Long, arr() As Long, counter As Long, cycle As Long
i = 0
rs.MoveFirst
With rs
Do Until i = 25
If .EOF Then
.MoveFirst
i = 0
'empty array
cycle = cycle + 1
For n = 1 To cycle
.MoveNext
Next
End If
i = i + !Field1
arr(counter) = !ID
If i > 25 Then
i = i - !Field1
'remove ID from array
End If
counter = counter + 1
.MoveNext
Loop
End With
'Print i
' Print Array
End Sub
קאוד: וועל אויס אלע
ID Field1
1 12
2 5
3 6
4 8
5 5
6 11
7 12
8 5
9 20
10 5
11 12
12 15
13 6
14 5
15 16
16 15
17 15
18 9
19 16
20 8
21 6
22 8
23 5
24 8
25 12
26 4
27 7
28 8
29 6
קאוד פאר T-SQL
קאוד: וועל אויס אלע
IF Object_id('#temptable') IS NOT NULL
DROP TABLE #temptable
IF Object_id('#tempArray') IS NOT NULL
DROP TABLE #temparray
CREATE TABLE #temptable
(
id INT,
field1 INT
)
CREATE TABLE #temparray
(
id INT,
field1 INT
)
INSERT INTO #temptable
VALUES (1,
12),
(2,
5),
(3,
6),
(4,
8),
(5,
5),
(6,
11),
(7,
12),
(8,
5),
(9,
20),
(10,
5),
(11,
12),
(12,
15),
(13,
6),
(14,
5),
(15,
16),
(16,
15),
(17,
15),
(18,
9),
(19,
16),
(20,
8),
(21,
6),
(22,
8),
(23,
5),
(24,
8),
(25,
12),
(26,
4),
(27,
7),
(28,
8),
(29,
6)
DECLARE @id INT,
@field1 INT,
@i INT=0,
@counter INT=0,
@cycle INT=0,
@cycleCounter INT=0
DECLARE abc CURSOR static FOR
SELECT *
FROM #temptable;
OPEN abc;
FETCH next FROM abc INTO @id, @field1
WHILE ( @i != 25 )
BEGIN
IF ( @@FETCH_STATUS = -1 )
BEGIN
FETCH first FROM abc INTO @id, @field1
SET @i=0
DELETE FROM #temparray
SET @cycle = @cycle + 1
SET @cycleCounter =1
WHILE ( @cycleCounter < @cycle )
BEGIN
FETCH next FROM abc INTO @id, @field1
SET @cycleCounter=@cycleCounter + 1
END
END
SET @i = @i + @field1
INSERT INTO #temparray
VALUES (@id,
@field1)
IF ( @i > 25 )
BEGIN
SET @i = @i - @field1
DELETE FROM #temparray
WHERE id = @id
END
SET @counter = @counter + 1
FETCH next FROM abc INTO @id, @field1
END
CLOSE abc
DEALLOCATE abc
PRINT @i
SELECT *
FROM #temparray
לעצט פארראכטן דורך פאטאקי08 אום פרייטאג יוני 27, 2014 5:45 am, פארראכטן געווארן 1 מאל.
ישר כח פארן פרובירן צי ענטפערן, אבער עס ארבעט נישט, איך בין אויף געקומען מיט א פאנקשיען וואס מאכט דאס, איך האב א ליסט פון set אבזשעקטס אין א list
דא איז מיין class
און עס ארבעט, עס איז נישט די שנעלסטע, אבער עס איז שנעלער פון אלע וואס איך האב געטראפן
דא איז מיין class
קאוד: וועל אויס אלע
public class set
{
public int id;
public int value;
public set() { }
public set(int id, int value)
{
this.id = id;
this.value = value;
}
}
public class SubSetSum
public List<set> Find(List<set> list, int Value)
{
List<set> found = new List<set>();
//try to find a set that is an exact match
set c = (from set ic in list
where ic.value == Value
select ic).FirstOrDefault();
if (c != null)//found exact
{
found.Add(c);
}
else
{
//only take sets that are below the value we are looking for
list = (from set ic in list
where ic.value < Value
select ic).ToList();
if (list.Count == 0)//nothing left
return found;
else if ((from set s in list//if sum all is less then Value
select s.value).Sum() < Value)
return found;
/*
using bits to map out which index in the array to use, each set gets mapped to a bit. So if you have 4 sets you'll have 4 bits to work with.
* if bit 1 and 3 are on, it will look at array indexes 1 and 3 to see if their sum matches to Value
4 bits would have any combination of 4 array items (4 bits = 16), so using 2 power of list.Count will yield all combinations, max = 2 ^ list.Count
*/
int max = (int)Math.Pow(2, list.Count);
for (int i = 1; i < max; i++)
{
//get all bits for i into a BitArray (an array of booleans for each bit)
var bits = new System.Collections.BitArray(new int[] { i });
//check if sum of all sets that have the bit turned on, are equals Value
var sum = (from int idx in Enumerable.Range(0, list.Count)//each index from 0 to number of sets in the list
where bits[idx] == true//where the bit is on
select list[idx].value)//select the sets value
.Sum();//sum of all sets in positions which bits are on
if (sum == Value)//if matched
{
//extract all the sets that match, and add them into the found list
found.AddRange((from int idx in Enumerable.Range(0, list.Count)
where bits[idx] == true
select list[idx]).ToList());
break;
}
}
}
return found;
}
}
און עס ארבעט, עס איז נישט די שנעלסטע, אבער עס איז שנעלער פון אלע וואס איך האב געטראפן