拿奖的问题
#include <bits/stdc++.h>using namespace std;
struct Student {
char name;
int finalscore;
int classscore;
char isworker;
char iswest;
int article;
};
int n;
int maxpay = 0;
char maxstu;
int totalpay = 0;
int pay = 0;
int main(){
scanf("%d", &n);
struct Student student;
for(int i = 0; i < n; i++){
scnaf("%s%d%d%c%c%d", \
&student.name\
&student.finalscore\
&student.classscore\
&student.isworker\
&student.iswest\
&student.article);
}
for(int i = 0; i < n; i++){
if(student.classscore > 80){
if(student.article > 0){
pay += 8000; //院士
}
}
if(student.finalscore > 85){
if(student.classscore > 80){
pay += 4000; //五四
}
if(student.iswest == 'Y'){
pay += 1000; //西部
}
}
if(student.finalscore > 90){
pay += 2000; //成绩
}
if(student.classscore > 80 && student.isworker == 'Y'){
pay += 850;
}
if(pay > maxpay){
maxpay = pay;
strcpy(maxstu, student.name);
}
totalpay += pay;
}
}
printf("%s\n%d\n%d", maxstu, maxpay, totalpay);
return 0;
}
代码如上 , 题目链接在下
https://www.luogu.com.cn/problem/P1051
求助 , 我写的代码老是报错 , 不知道怎么回事...
另外 , 也不知道这个代码能不能解决问题 本帖最后由 zzxhh628 于 2022-3-17 12:59 编辑
#include <stdio.h>
struct Student
{
char name;
int finalscore;
int classscore;
char isworker;
char iswest;
int article;
};
int n;
int maxpay = 0;
char maxstu;
int totalpay = 0;
int pay = 0;
int main()
{
scanf("%d", &n);
struct Student student;
for(int i = 0; i < n; i++)
{
scanf("%s%d%d%c%c%d", \
&student.name,\
&student.finalscore,\
&student.classscore,\
&student.isworker,\
&student.iswest,\
&student.article);
}
for(int i = 0; i < n; i++)
{
if(student.classscore > 80)
{
if(student.article > 0)
{
pay += 8000; //院士
}
}
if(student.finalscore > 85)
{
if(student.classscore > 80)
{
pay += 4000; //五四
}
if(student.iswest == 'Y')
{
pay += 1000; //西部
}
}
if(student.finalscore > 90)
{
pay += 2000; //成绩
}
if(student.classscore > 80 && student.isworker == 'Y')
{
pay += 850;
}
if(pay > maxpay)
{
maxpay = pay;
strcpy(maxstu, student.name);
}
totalpay += pay;
}
printf("%s\n%d\n%d", maxstu, maxpay, totalpay);
return 0;
}
现在能运行了,能不能实现题目要求你自己试试吧。
你这里原来有些小错误,例如函数名错误和大括号的错误,头文件其实用这个也行,你也可以改回去,在stdio.h就不用using namespace std;了。 报错了...{:10_266:} 本帖最后由 jhq999 于 2022-3-17 16:12 编辑
柿子饼同学 发表于 2022-3-17 13:12
报错了...
scanf("%s%d%d%c%c%d", \
student.name,\
&student.finalscore,\
&student.classscore,\
&student.isworker,\
&student.iswest,\
&student.article); #include <iostream>
#include <vector>
#include <utility>
typedef struct Students {
std::string name;
size_t exam, review;
bool leader, western;
short published;
size_t scholarship;
Students(std::string n, size_t e, size_t r, bool l, bool w, short p) :
name(n),
exam(e),
review(r),
leader(l),
western(w),
published(p),
scholarship(0){}
}Students;
int main() {
std::string name;
size_t N, exam, review;
char leader, western;
short published;
std::cin >> N;
std::vector<Students> std;
for (size_t n = 0; n < N; n++) {
std::cin
>> name
>> exam
>> review
>> leader
>> western
>> published;
std.push_back({ name, exam, review, leader == 'Y' ? true : false, western == 'Y' ? true : false, published });
}
size_t i = 0, sum = 0;
for (Students S : std) {
if (S.exam > 80 && S.published)std.scholarship += 8000, sum += 8000;
if (S.exam > 85 && S.review > 80)std.scholarship += 4000, sum += 4000;
if (S.exam > 90)std.scholarship += 2000, sum += 2000;
if (S.exam > 85 && S.western)std.scholarship += 1000, sum += 1000;
if (S.review > 80 && S.leader)std.scholarship += 850, sum += 850;
i++;
}
std::pair<std::string, size_t> highest("", 0);
for (Students S : std) {
if (S.scholarship > highest.second) {
highest.first = S.name;
highest.second = S.scholarship;
}
}
std::cout
<< highest.first << std::endl
<< highest.second << std::endl
<< sum << std::endl;
return 0;
}4
YaoLin 87 82 Y N 0
ChenRuiyi 88 78 N Y 1
LiXin 92 88 N N 0
ZhangQin 83 87 Y N 14
ChenRuiyi
9000
28700 jhq999 发表于 2022-3-17 16:10
答案不对...>﹏< 傻眼貓咪 发表于 2022-3-17 19:09
size_t 不就是无符号整型嘛 , 为什么要这么用呢
另外 , 我觉得我学的还是太少了 , 好多写法我都不知道...{:10_266:} 柿子饼同学 发表于 2022-3-18 09:00
size_t 不就是无符号整型嘛 , 为什么要这么用呢
另外 , 我觉得我学的还是太少了 , 好多写法我都不知道.. ...
是啊,因为数量必然是正整数,其实也可以用 int
只是我想活用 c 语言的所有数据类型,比如我的代码中的 short 傻眼貓咪 发表于 2022-3-18 09:48
是啊,因为数量必然是正整数,其实也可以用 int
只是我想活用 c 语言的所有数据类型,比如我的代码中的...
原来如此啊{:10_277:} 傻眼貓咪 发表于 2022-3-17 19:09
我下课就把代码写一遍 傻眼貓咪 发表于 2022-3-17 19:09
求问大佬是怎么学STL的{:10_266:} 柿子饼同学 发表于 2022-3-18 21:16
求问大佬是怎么学STL的
大佬不敢当。{:10_277:}
我最喜欢看其他大佬的代码,如果发现有新玩意(没有看过的写法),我就非常感兴趣,自己试试用(可能我是好奇宝宝吧{:10_264:})
页:
[1]