每個正確選項都有得分權(quán)重,選正確一個給該正確選項得分權(quán)重的分值,選錯一個,則總分為0
假如:正確答案ABC ,總分10分,權(quán)重分別為 20% 40% 40%
提交答案 ABC 得 (20%+40%+40%)* 10 = 10分
提交答案 AB 得 (20%+40%)* 10 = 6分
提交答案 ABD 得 0分
集思廣益,如果可以請使用PHP作答
PHP面向?qū)ο蟀?/p>
/**
* Created by: Singee77
*/
class Standard
{
//答對全部題所得總分
private $totalScore = 0;
//標準答案
private $standard = [];
//提交答案
private $answer = [];
//所得總分
private $getScore = 0;
public function __construct($totalScore)
{
$this->setTotalScore($totalScore);
}
/**
* @return int
*/
public function getTotalScore()
{
return $this->totalScore;
}
/**
* @param int $totalScore
*/
public function setTotalScore($totalScore)
{
$this->totalScore = $totalScore;
}
/**
* @param array $standard
*/
public function setStandard($standard)
{
$this->standard = $standard;
}
/**
* @return array
*/
public function getStandard()
{
return $this->standard;
}
/**
* @param $answer
*/
public function checkStandard()
{
foreach ($this->answer as $each) {
if (!$weight = $this->checkAnswer($each)) {
//選錯一個總分為0
$this->setGetScore(0);
break;
}
//答對一個就追加分數(shù)
$this->appendGetScore($this->getTotalScore() * $weight);
}
}
/**
* @param array $answer
*/
public function setAnswer($answer)
{
$this->answer = $answer;
}
/**
* @return array
*/
public function getAnswer()
{
return $this->answer;
}
/**
* @param $each
* return $weight
*/
private function checkAnswer($each)
{
return array_key_exists($each, $this->standard) ? $this->standard[$each] : 0;
}
/**
* @param int $getScore
*/
public function setGetScore($score)
{
$this->getScore = $score;
}
/**
* @return int
*/
public function getGetScore()
{
return $this->getScore;
}
/**
* @param int $totalScore
*/
public function appendGetScore($appendScore)
{
$this->getScore += $appendScore;
}
}
//實例一個CHECK對象并設(shè)置總分
$std = new Standard(10);
//設(shè)置標準答案以及占比
$std->setStandard(['A' => 0.2, 'B' => 0.4, 'C' => 0.4]);
//設(shè)置答案
$std->setAnswer(['A', 'B']);
//計算分數(shù)
$std->checkStandard();
//獲取所得總分
$totalScore = $std->getTotalScore();
echo $totalScore;
不會php, 用一下js
// 選擇的選項
const select = ['a','b','d']
// 分數(shù)
const score = {
'a': 20,
'b': 20,
'c': 60,
'd': 0
}
// 根據(jù)所選 得到 分數(shù)如:abd 的到 20,20,0
const a = select.map(x => score[x])
// 如果 a里面有 0 直接返回 0 否則 reduce 后得到 分數(shù)
total = a.includes(0) ? 0 : a.reduce((total, i) => total+ (i *0.1) , 0)
假設(shè)你的答案有一些分隔符例如,
,你有每道題的標準答案,可以如下:
/**
*@param $standard array 標準答案
*@param $answer string 提交答案
*/
function get_score($standard,$answer){
//分數(shù)
$score = 0;
$answer = explode(',', $answer);
if(!empty($answer)){
foreach ($answer as $v) {
if(isset($standard[$v])){
$score+=(int)($standard[$v]*10);
}else{
$score = 0;
break;
}
}
}
return $score;
}
var_dump(get_score([
'A' => 0.2,
'B' => 0.4,
'C' => 0.4
],'A,B'));
思路,獲得提交與答案的交集,如果交集個數(shù)不等于提交個數(shù),則認為,存在選錯的值,如果交集個數(shù)等于提交個數(shù),遍歷求分即可
<?php
function multiSelectScore($answer, $wight, $allScore, $submit)
{
$score = 0;
$answerWight = array_combine($answer, $wight);
// 計算交集
$selects = array_intersect($answer, $submit);
if(count($selects) === count($submit)) {
// 遍歷交集
foreach($selects as $key => $select) {
$score += $answerWight[$select] * 0.01 * $allScore;
}
}
return $score;
}
// 答案
$answer = ["A", "B", "C"];
// 權(quán)重
$wight = ["20","30","50"];
// 總分
$allScore = 10;
// 提交的選項
$submit = ["A","C"];
$score = multiSelectScore($answer, $wight, $allScore, $submit);
echo json_encode($score) . "\n";
function selectScore(preAnswerScore,answer) {
let totalScore = 0;
let noScoreKey = [];
let hasNoScoreKey = false;
for(let key in preAnswerScore) {
if(preAnswerScore[key] == 0){
noScoreKey.push(key);
}
}
noScoreKey.map( item => {
if(answer.includes(item)){
hasNoScoreKey = true;
}
});
if(hasNoScoreKey) {
totalScore = 0;
}else{
answer.map (item => {
totalScore += preAnswerScore[item];
});
totalScore /= 10;
}
return totalScore;
}
let preAnswerScore = {a: 20, b: 40, c: 40, d: 0};
let answer = ['c','b','d']
console.log(selectScore(preAnswerScore, answer));
}
js走一波,考慮到你其他選擇題也適用
function calc($result,$answer,$score){
$count = count($result);
$d = array_diff($answer,$result);
if($d){
return 0;
}
$c = array_diff($result,$answer);
$lost = count($c);
if(!$lost){
return $score;
}
switch ($count) {
case 1:
$per = [1=>1];
break;
case 2:
$per = [1=>0.4,2=>1];
break;
case 3:
$per = [1=>0.2,2=>0.6,3=>1];
break;
default:
$per = [1=>0.1,2=>0.5,3=>0.8,4=>1];
break;
}
$true = $count - $lost ;
return $per[$true]*$score;
}
$result = ['A','B','C'];
$answer = ['A','B'];
$score = 10;
echo calc($result,$answer,$score);
function check_score(array $answer, array $correct, $total_score){
$answer=array_flip($answer);
$check=array_intersect_key($correct, $answer);
return in_array($check, 0) ? 0 : array_sum($ckeck)*$total_score;
}
$answer
為答案數(shù)組(['A', 'B', 'C']
)
$correct
為正確答案數(shù)組(['A'=>0.2, 'B'=>0.4, 'C'=>0.4, 'D'=>0]
)
$total_score
為總分
首先翻轉(zhuǎn)$answer
讓選項成為key(['A'=>0, 'B'=>1, 'C'=>2]
)
然后根據(jù)key計算$correct
和$answer
的交集$check
,$check
的key和value都保留$correct
的。
檢查$check
的value中是否有0
,有的話說明有錯誤答案,返回0分,否則計算$check
的value的和,并乘以總分返回。
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團,成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達內(nèi)教育集團成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負責(zé)iOS教學(xué)及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。