#include<iostream> usingnamespace std; intmain(void) { int numbers, picks; cin >> numbers; cin >> picks; longdouble result = 1.0; // 为了保护numbers和picks的值不被改变,所以用n和p拷贝一份副本; for (int n = numbers, p = picks; p > 0; p--) { cout << n << " " << p << endl; result = result * n / p; n--; }
#include<iostream> usingnamespace std; longdoubleprobability(unsigned numbers, unsigned picks); intmain(void) { double total, choices; cout << "Enter the total number of choices on the game card and\n" "the number of picks allowed:\n"; while ((cin >> total >> choices) && choices <= total) { cout << "You have one chance in "; cout << probability(total, choices); cout << " of winning.\n"; cout << "Next two numbers (q to quit): "; } cout << "bye\n"; return0; }
longdoubleprobability(unsigned numbers, unsigned picks) { longdouble result = 1.0; longdouble n; unsigned p; for (n = numbers, p = picks; p > 0; p--, n--) { result *= (n / p); // 不能让n / p得到一个取整,因此要把n设置成long double类型 } return result; }
2025年2月10日复习
关于自己当时的问题的解答
问ai,while(cin >> n); 这里 的 cin >> n也是由返回值的,然后里面不要写成 while(cin >> n, n);你最好写成while(cin >> n && n);我记得ai是这么说的
#include<iostream> usingnamespace std; longdoublecalculate(int number, int pick); intmain(){ int number; int pick;
cout << "Please enter number and pick : "; // 记住了第一次的提示信息和后面的提示信息是不一样的 cin >> number; cin >> pick;
while (!cin.fail() && pick <= number) { // do something cout << "your probabilities is " << calculate(number, pick) << endl;
cout << "Please enter another number and pick or q to quit: "; // 字符q直接就cin fail了,所以下次循环的 判断条件第一个expression就是false了 cin >> number; cin >> pick; } cout << "bye"; return0; } longdoublecalculate(int number, int pick){ longdouble result = 1.0; for (int n = number, p = pick; p > 0; n--, p--) { result = result * (1.0 * n / p); } return result; }
#include<iostream> usingnamespace std; longdoublecalculate(int number, int pick); intmain(){ int number; int pick;
cout << "Please enter number and pick : "; // 记住了第一次的提示信息和后面的提示信息是不一样的
while ((cin >> number >> pick) && pick <= number) { // do something cout << "your probabilities is " << calculate(number, pick) << endl;
cout << "Please enter another number and pick or q to quit: "; // 字符q直接就cin fail了,所以下次循环的 判断条件第一个expression就是false了 } cout << "bye"; return0; } longdoublecalculate(int number, int pick){ longdouble result = 1.0; for (int n = number, p = pick; p > 0; n--, p--) { result = result * (1.0 * n / p); } return result; }
还有就是自己能够自然的写出下面,得益于acwing老是写这个.
1 2 3
for (int n = number, p = pick; p > 0; n--, p--) { result = result * (1.0 * n / p); }
7.3 函数和数组
2025年2月10日
复习到这里了.
为什么凡是涉及到数组的传参不仅会传数组名,往往还会传一个int len ,也就是数组的长度,因为你在子函数内
// 定义一个函数指针数组 constdouble *(*pa[3])(constdouble *, int) = {f1, f2, f3}; auto pb = pa; cout << "PART2:-----------------------" << endl; for (int i = 0; i < 3; i++) { // 要我说,还是别用这种写法,让人搞不清楚的罪魁祸首就是下面这一行的写法 cout << "Address\t" << pa[i](av, 3) << "\tValue\t" << *pa[i](av, 3) << endl; } for (int i = 0; i < 3; i++) { cout << "Address\t" << (*pb[i])(av, 3) << "\tValue\t" << *(*pb[i])(av, 3) << endl; }
cout << "PART3:-----------------------" << endl; // pc(pd) is a pointer to an array of function pointers auto pc = &pa; // 自己写这个写对了. constdouble *(*(*pd)[3])(constdouble *, int) = &pa;