奖学金统计是学校教务管理中的常见需求。这个程序需要处理多种奖学金类型的叠加计算,并输出最高获奖者和总金额。核心逻辑在于正确实现每种奖学金的判断条件。
程序处理的五种奖学金条件如下:
关键点在于这些条件是"叠加"而非"互斥"的,一个学生可能同时满足多个条件。
cpp复制int schoolship(string name, int fs, int cs, char isg, char iswestern, int sci) {
int sum = 0;
if (fs > 80 && sci >= 1) sum += 8000; // 院士奖学金
if (fs > 85 && cs >= 80) sum += 4000; // 五四奖学金
if (fs > 90) sum += 2000; // 成绩优秀奖
if (fs > 85 && iswestern == 'Y') sum += 1000; // 西部奖学金
if (cs > 80 && isg == 'Y') sum += 850; // 班级贡献奖
return sum;
}
注意事项:
cpp复制int main() {
int N;
while (cin >> N) {
string max_name;
int max_schoolship = -1, total = 0;
for (int i = 0; i < N; i++) {
string name;
int fs, cs, sci;
char isg, iswestern;
cin >> name >> fs >> cs >> isg >> iswestern >> sci;
int current = schoolship(name, fs, cs, isg, iswestern, sci);
total += current;
if (current > max_schoolship) {
max_schoolship = current;
max_name = name;
}
}
cout << max_name << endl << max_schoolship << endl << total << endl;
}
}
常见问题:
这个加法器需要处理形如"1+2+3"的字符串表达式,计算其总和。核心在于正确解析数字和运算符。
cpp复制int main() {
string s;
while (cin >> s) {
int digit = 0, sum = 0;
bool isnum = false;
for (char c : s) {
if (isdigit(c)) {
digit = digit * 10 + (c - '0');
isnum = true;
} else if (isnum) {
sum += digit;
digit = 0;
isnum = false;
}
}
if (isnum) sum += digit;
cout << sum << endl;
}
}
注意事项:
这个序列生成规则独特:每一轮在相邻和为n的数字间插入n。
cpp复制vector<string> generate_sequence() {
vector<string> res;
vector<int> current = {1, 1};
res.push_back("11");
for (int round = 2; round <= 9; round++) {
vector<int> next;
for (int i = 0; i < current.size() - 1; i++) {
next.push_back(current[i]);
if (current[i] + current[i+1] == round)
next.push_back(round);
}
next.push_back(current.back());
string s;
for (int num : next) s += to_string(num);
res.push_back(s);
current = next;
}
return res;
}
优化建议:
纯粹合数是指其本身和所有"去高位"后的数都是合数。
cpp复制bool isPure(int n) {
int current = n;
while (current > 0) {
if (!isHeShu(current)) return false;
current = Zuigao(current);
}
return true;
}
注意事项:
在数字串中找出不超过4位的最长质数子串。
cpp复制bool isPrime(int n) {
if (n < 2) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i*i <= n; i += 2)
if (n % i == 0) return false;
return true;
}
cpp复制for (int len = 4; len >= 1; len--) {
for (int i = 0; i + len <= s.size(); i++) {
int num = stoi(s.substr(i, len));
if (isPrime(num)) {
// 处理结果
}
}
}
优化建议:
这个翻译规则是:遇到数字n,就将后一个字符重复n+1次。
cpp复制string translate(const string& s) {
string result;
for (int i = 0; i < s.size(); i++) {
if (isdigit(s[i])) {
int count = s[i] - '0' + 1;
if (i + 1 < s.size()) {
result.append(count, s[i+1]);
i++; // 跳过已处理的字符
}
} else {
result += s[i];
}
}
return result;
}
特殊案例处理:
将字符串中的'5'视为分隔符,分割后排序输出。
cpp复制vector<int> splitAndSort(const string& s) {
vector<int> numbers;
int current = 0;
bool building = false;
for (char c : s) {
if (c != '5') {
current = current * 10 + (c - '0');
building = true;
} else if (building) {
numbers.push_back(current);
current = 0;
building = false;
}
}
if (building) numbers.push_back(current);
sort(numbers.begin(), numbers.end());
return numbers;
}
注意事项:
由于浮点数精度问题,不能直接用==比较。
cpp复制bool isEqual(string s1, string s2) {
double a = stod(s1), b = stod(s2);
double epsilon = 1e-10;
return fabs(a - b) < epsilon;
}
精度选择建议:
这些算法问题涵盖了字符串处理、数学计算、条件判断等常见编程场景。在实际应用中,还需要考虑更多边界条件和性能优化。每个解决方案都展示了如何将问题分解为可执行的步骤,并通过代码实现。理解这些基础算法有助于解决更复杂的编程问题。