[英]How can I resolve an issue with freopen and scanf ( C++ )?
我为 USACO 银牌问题“休息站”(2018 年 2 月)编写了一些代码。 但是,当我提交代码时,freopen 和 scanf 存在一些问题。
我尝试更改 to 并添加 return 0,但它们都不起作用。 我包含了 stdio.h、算法和向量库。
using namespace std;
typedef pair < int, int > ii;
int l, n, rf, rb;
vector < pair < int, int > > map;
int main() {
freopen("reststops.in", "r", stdin);
freopen("reststops.out" "w", stdout);
scanf("%d %d %d %d", &l, &n, &rf, &rb);
for(int i = 0; i < n; i ++) {
int x, c;
scanf("%d %d", &x, &c);
map.push_back(ii(x, c));
}
int start_idx = 0; int start = 0;
int maxi = 0; int max_idx = 0;
int ans = 0;
while(true) {
bool flag = false;
for(int i = start_idx; i < map.size(); i ++) {
if(maxi < map[i].second) {
max_idx = i;
maxi = map[i].second;
flag = true;
}
}
if(!flag) {
if(start_idx == map.size() - 1) break;
else {
ans += (rf - rb) * (map[map.size() - 1].first - map[start_idx].first);
break;
}
}
if(start_idx == 0) start = 0;
else start = map[start_idx].first;
int curr_f = (map[max_idx].first - start) * rf;
int curr_b = (map[max_idx].first - start) * rb;
ans += (curr_f - curr_b) * map[max_idx].second;
start_idx = max_idx;
}
printf("%d", ans);
return 0;
}
编译错误
main.cpp: In function ‘int main()’: main.cpp:14:40: error: cannot convert ‘_IO_FILE*’ to ‘const char*’ for argument ‘2’ to ‘FILE* freopen(const char*, const char*, FILE*)’ freopen("reststops.out" "w", stdout); ^ main.cpp:13:40: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’, declared with attribute warn_unused_result [-Wunused-result] freopen("reststops.in", "r", stdin); ^ main.cpp:16:43: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d %d %d %d", &l, &n, &rf, &rb); ^ main.cpp:19:31: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d %d", &x, &c); ^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.