其他
学生作品|2024创新开发--《点名器》
下面的程序是他的应用中的一部分,由他自己写的递归算法:
#include <iostream>
#include <cmath>
#include <map>
#include <tuple>
using namespace std;
unsigned int all = 0;
bool is_input_allow(unsigned int ac, unsigned int rp){
if(all && rp <= ac && ac < all){
return true;
}
return false;
}
[[maybe_unused]] double no_improve(){
return 1/(double)all;
}
[[maybe_unused]] double delete_improve(int ac, int rp){
/*repeat in this turn
*repeated => rete = 0
*not repeated => rate = 1/lefts
*lefts = all - ac
*/
if(ac != rp){
return 0;
}
return (1-((double)ac/(double)all))*(1/((double)all-(double)ac));
}
double decrease_improve(double ac, double rp, map<tuple<int, int>, double> *cache){
if(rp > ac){
return 0;
}
if(ac == 0 && rp == 0){
return 1/(double)all;
}
if(cache->count(make_tuple((int)ac, (int)rp))) {
return cache->at(make_tuple((int)ac, (int)rp));
}
if(rp == 0){
cache->insert(pair<tuple<int, int>, double> (make_tuple((int)ac, (int)rp),
decrease_improve(ac - 1, rp, cache)+
pow((1/(double)all), ac)*(1-(((double)all - 1)/pow(((double)all), ac)))));
return cache->at(make_tuple((int)ac, (int)rp));
}
cache->insert(pair<tuple<int, int>, double> (make_tuple((int)ac, (int)rp),decrease_improve(ac - 1, rp - 1, cache)*(1/(double)all) +
decrease_improve(ac - 1, rp, cache)+
(ac - rp) * (pow((1/(double)all), ac)*(1-(((double)all - 1)/pow(((double)all), ac))))));
return cache->at(make_tuple((int)ac, (int)rp));
}
int main(){
unsigned int allChosen = 0;
unsigned int repeatTimes = 0;
map<tuple<int, int>, double> cache = {};
cin >> allChosen >> repeatTimes >> all;
while (!is_input_allow(allChosen, repeatTimes)) {
cout << "这个人很神秘!" << endl;
cin >> allChosen >> repeatTimes >> all;
}
cout << decrease_improve(allChosen, repeatTimes, &cache) << endl;
// for(auto it = cache.begin(); it != cache.end(); ++it){
// cout << "(" << get<0>(it->first) << "," << get<1>(it->first) << ")\t\t" << it->second << endl;
// }
return 0;
}
上图是他写的递归算法,应用在点名器中。为什么要推这个同学的作品呢?是因为想通过这个例子来说明:不是只有奥赛才能培养学生的编程能力。这个学生并没有奥赛经历,通过参加中小学信息素养实践活动,他去年获得了全国参与奖。而且今年,他利用自己的开发能力,为老师解决了现实教学中的应用问题。