[NOIP2007 普及组] 守望者的逃离

#include
using namespace std;
int m, s, t, a, b, now = 1, flag, tmp;
void tinky() {
    if (m >= 10) {
        m -= 10;
        b += 60;
    } else m += 4;
    if (b > a)a = b;
}
int esp( int s, int t) {
    while (now <= t) {
        a += 17;
        tinky();
        if (a >= s) {
            flag = 0;
            return now;
        }
        now++;
    }
    flag = 1;
    return a;
}
int main() {
    cin >> m >> s >> t;
    tmp = esp(s, t);
    if (flag == 0)cout << "Yes" << endl << now;
    else cout << "No" << endl << a;
    return 0;
}

错误原因:因为之前传参;

以后一定再外边就用了它,不能放在里面

#include//错误代码
using namespace std;
int m, s, t, a, b, now = 1, flag, tmp;
void tinky(int m) {//此处传参失败!!!
	if (m >= 10) {
		m -= 10;
		b += 60;
	} else m += 4;
	if (b > a)a = b;
}
int esp( int m,int s, int t) {
	while (now <= t) {
		a += 17;
		tinky(m);
		if (a >= s) {
			flag = 0;
			return now;
		}
		now++;
	}
	flag = 1;
	return a;
}
int main() {
	cin >> m >> s >> t;
	tmp = esp(m,s, t);
	if (flag == 0)cout << "Yes" << endl << now;
	else cout << "No" << endl << a;
	return 0;
}

你可能感兴趣的:(算法,c++,数据结构)