search
HomeBackend DevelopmentPHP Tutorial php 多选题评分算法 求指点

php 多选题评分算法 求指导
多选题一题3分
原则是:少选给2分,多选不给分,选错不给分
假如:正确答案123
提交答案 123 得3分 
提交答案 12,13,23,都可得2分 
提交答案 1234,234,134,124 没分
怎么实现,还是有更好的方法  

PHP 算法
------解决方案--------------------
按你的描述,你的多选题不是用 checkbox 而是用 text 输入的
并且备选答案不会多于9个

于是得分可写作
count(array_intersect(str_split($正确答案), str_split($提交答案));

就是:分别切割成数组,然后取交集
------解决方案--------------------
如果你是使用 checkbox
那么提交后得到的就是数组,与正确答案数组取交集就是了
------解决方案--------------------
$正确答案 = array(1, 2, 3);
$提交答案 = array(1, 2);
$得分 = count(array_intersect($正确答案, $提交答案));
echo $得分; //2
------解决方案--------------------
引用:
$正确答案 = array(1, 2, 3);
$提交答案 = array(1, 2);
$得分 = count(array_intersect($正确答案, $提交答案));
echo $得分; //2

取交集和楼主的题目要求不符合吧,比如提交答案为数组array(1,2,4),那么交集也是(1,2),实际应该得0分吧。
------解决方案--------------------
是的,不完全符合。
但是选 1,2,4 就不得分是不合常理的。除非 4 是反选项,比如:都不是
如果是这样的话,出题者就是在有意误导答题者

如果非要这样出题的话,可以这样写
$正确答案 = array(1, 2, 3);<br />
$错误答案 = array(4);<br />
$提交答案 = array(1, 2, 4);<br />
$得分 = array_intersect($错误答案, $提交答案) ? 0 : count(array_intersect($正确答案, $提交答案));<br />
echo $得分; //0



引用:
引用:$正确答案 = array(1, 2, 3);
$提交答案 = array(1, 2);
$得分 = count(array_intersect($正确答案, $提交答案));
echo $得分; //2
取交集和楼主的题目要求不符合吧,比如提交答案为数组array(1,2,4),那么交集也是(1,2),实际应该得0分吧。
……

------解决方案--------------------
交并差就可以了
答卷在前,答案在后
有差集,不给分
无交集,不给分(排除上一个后这个是考虑空白答卷的情况,如果确认不会有白卷这句可以不用)
无差集,有交集,交集数量!=答卷,半分
无差集,有交集,交集数量=答卷,满分(这个可以不写逻辑,直接用else也行,因为排除上面几种情况剩下就是交集相等)

注意
1.上面要顺序判断,是 else if 不相容逻辑而不是 switch 可容逻辑,因为省了一些判断条件
2.答卷和答案都要确保无重复值,不然交集数量判断会有问题
------解决方案--------------------
可以用位运算来计算
A:1 B:2 C:4 D:8
如何选择AB 则为3
如何选择ABCD 则为15

比如正确为4 则 看看选择的结果为N跟4比
if(N==4) 3分
if 4&N==N 2分
其他 O分




------解决方案--------------------
引用:
可以用位运算来计算
A:1 B:2 C:4 D:8
如何选择AB 则为3
如何选择ABCD 则为15

比如正确为4 则 看看选择的结果为N跟4比
if(N==4) 3分
if 4&N==N 2分
其他 O分


位运算是不错的选择,选择肢 但更大就不太方便了
------解决方案--------------------
按照老大的思路,可以这样做
<br>
$correct = array(1, 2, 3);<br>
$submit = array(1, 2);<br>
$score = count(array_intersect($correct, $submit));<br>
if($score 
  $score = 0;<br>
}<br>
echo $score; //2 <div class="clear">
                 
              
              
        
            </div>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment