在题目中,要求输入的格式为
6
3-10 99
11-5 87
102-1 0
102-3 100
11-9 89
3-2 61
一开始我用的cin,自然要想办法去掉 ‘-’ ,于是就煞费苦心写了个函数去了 ‘-’ 。
后来发现,根本不需要去 ‘-’ ,scanf函数就能帮你去。
#include <stdio.h>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char target[20];
int team = 0;
int number = 0;
int number1 = 0;
int score = 0;
int Max = 0;
int Max_t = 0;
int hash[1001] = { 0 };
cin >> number1;
while (number1) {
scanf("%d-%d %d", &number, &team, &score);
hash[number] += score;
if (Max < hash[number]) {
Max = hash[number];
Max_t = number;
}
number1--;
}
cout << Max_t << " " << Max;
}
scanf函数只要把输入参数写成
scanf("%d-%d %d", &number, &team, &score);
他就能只接受数字,跳过 ‘-’ 。
但这种输入方式有一个缺陷,用户每次输入必须吧 ‘-’ 输入,不然就会报错无法识别。在这个题目中,使用这种方式能提高输入效率,但在平时中这种写法可能会导致输入不够简洁,对用户不够友好。
总之,对于特殊的输入格式,这种用法会帮助我们快速输入,而在日常编写中,则应该规避这种写法。