D';使用php的Hondt方法计算器?
大家好,我的项目有问题,让我解释一下。我可以将数据拖到xml文件中,但无法通过D'Hondt方法读取分配的座位数。我已经试过很多次了,但都做不到。有人能帮我吗 这是我的密码D';使用php的Hondt方法计算器?,php,xml,Php,Xml,大家好,我的项目有问题,让我解释一下。我可以将数据拖到xml文件中,但无法通过D'Hondt方法读取分配的座位数。我已经试过很多次了,但都做不到。有人能帮我吗 这是我的密码 $i = 0; foreach ($xml->city as $city) { echo '<tr><th>'; echo $xml->city[$i]->attributes(). "</th>"; echo '<th>Votes&l
$i = 0;
foreach ($xml->city as $city) {
echo '<tr><th>';
echo $xml->city[$i]->attributes(). "</th>";
echo '<th>Votes</th><th>Seats:';
echo $city->total_seats.'</th></tr>';
foreach ($xml->city[$i]->party_votes as $party_votes) {
foreach ($party_votes as $party_votes) {
echo '<tr style="color:'.$color[$i].'"><td>';
echo $party_votes->attributes()."</td><td> ";
echo $citya[$i]=$party_votes->total_votes. "</td>";
}
}
$i++;
echo "</tr>";
}
$i=0;
foreach($xml->city as$city){
回声';
echo$xml->city[$i]->attributes();
回声“Votessets:”;
echo$city->total_座位数。“;
foreach($xml->city[$i]->party\u投票作为$party\u投票){
foreach($party_票数为$party_票数){
回声';
echo$party_投票->属性();
echo$citya[$i]=$party\u voces->total\u voces.“;
}
}
$i++;
回声“;
}
这是xml文件
8.
100000
80000
30000
20000
6.
10000
50000
40000
30000
从xml文档中提取值和生成D'Hondt分布是两个独立的问题。我将集中讨论后者。
下面是一个使用a和a的示例
(请记住,a)和b)是两个完全独立的运行;如果您愿意,您可以以一种稍微更有效的方式将它们组合在一起-但是这个示例根本没有针对效率进行优化…)从xml文档中提取值和生成D'Hondt分布是两个独立的问题。我将集中讨论后者。 下面是一个使用a和a的示例
(请记住,a)和b)是两个完全独立的运行;如果您愿意,您可以以一种稍微更有效的方式将它们组合在一起-但是这个示例根本没有针对效率进行优化…)您尝试了D'Hondt方法的哪种变体?至少有五种不同但相当的变体。我想每个城市都做这个例子,还是你想把所有席位和选票相加,然后分配席位?您提供的sampe数据的示例结果会很好。根据每个城市的座位数,您尝试了哪种D'Hondt方法?至少有五种不同但相当的变体。我想每个城市都做这个例子,还是你想把所有席位和选票相加,然后分配席位?您提供的sampe数据的示例结果会很好。根据每个城市的座位数,感谢您的解释,但我尝试使用自己的代码来实现,但我无法实现。你知道如何使用我的代码吗?谢谢你的解释,但是我试着使用自己的代码,但我无法使用。你知道如何使用我的代码吗?
<?php
function gen_dhondt(array $results) {
// using SplPriorityQueue as a sorted listed
$foo = new SplPriorityQueue;
$foo->setExtractFlags(SplPriorityQueue::EXTR_DATA);
// initialize the list
foreach( $results as $party=>$votes ) {
$foo->insert(['name'=>$party, 'div'=>1, 'votes'=>$votes], $votes);
}
// get the element with the highest votes/divisor
foreach( $foo as $next ) {
// re-enqueue this element with ++divisor
++$next['div'];
$foo->insert($next, $next['votes']/$next['div']);
// "return" the name of this element
yield $next['name'];
}
}
$seats = 8;
$results = array('C'=>30000, 'B'=>80000, 'A'=>100000, 'D'=>20000);
// a) just show the order in which the seats are distributed
$lit = new LimitIterator( gen_dhondt($results), 0, $seats);
foreach( $lit as $p ) {
echo $p, "\r\n";
}
// b) count the seats
var_export(
array_count_values(
iterator_to_array(
new LimitIterator( gen_dhondt($results), 0, $seats)
)
)
);
A
B
A
B
A
C
B
A
array (
'A' => 4,
'B' => 3,
'C' => 1,
)