关于使用string输入输出导致超时

输入时:先按照字符数组用scanf输入,再用string()转为字符串。

char s[maxn];
string str;
//cin>>str;     //可能会超时
scanf("%s",s);
str=string(s);

输出时:使用c_str()方法将字符串转为字符数组,再用printf输出。

//cout<<str<<endl;    //可能会超时
printf("%s\n",str.c_str());

例子:PAT 1047 Student List for Course

Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.

Input Specification:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤40,000), the total number of students, and K (≤2,500), the total number of courses. Then N lines follow, each contains a student’s name (3 capital English letters plus a one-digit number), a positive number C (≤20) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K.

Output Specification:
For each test case, print the student name lists of all the courses in increasing order of the course numbers. For each course, first print in one line the course number and the number of registered students, separated by a space. Then output the students’ names in alphabetical order. Each name occupies a line.

Sample Input:

10 5
ZOE1 2 4 5
ANN0 3 5 2 1
BOB5 5 3 4 2 1 5
JOE4 1 2
JAY9 4 1 2 5 4
FRA8 3 4 2 5
DON2 2 4 5
AMY7 1 5
KAT3 3 5 4 2
LOR6 4 2 4 1 5

Sample Output:

1 4
ANN0
BOB5
JAY9
LOR6
2 7
ANN0
BOB5
FRA8
JAY9
JOE4
KAT3
LOR6
3 1
BOB5
4 7
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
5 9
AMY7
ANN0
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1

题目很简单,但是写完提交发现最后一个测试点超时,一开始以为是排序的问题就改为集合set< string >,仍然超时,问题应该出在cin、cout上。又回到最开始的思路,在输入输出时使用字符数组就成功AC了。

#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=40010;

int main(){
    int n,k;
    vector<string> Course[maxn];
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        int num,a;
        char name[10];
        string s;
        scanf("%s%d",name,&num);
        s=string(name);
        for(int j=0;j<num;j++){
            scanf("%d",&a);
            Course[a].push_back(s);
        }
    }
    for(int i=1;i<=k;i++){
        printf("%d %d\n",i,Course[i].size());
        sort(Course[i].begin(),Course[i].end());
        for(int j=0;j<Course[i].size();j++)
            printf("%s\n",Course[i][j].c_str());
    }
    return 0;
}
### 回答1: 题目描述: 给定一个整数n,和n个整数,每个整数都在1到100之间,对于每个整数,输出其质因子分解后因子个数的奇偶性。 输入格式: 第一行包含整数n。 接下来n行,每行包含一个整数。 输出格式: 共n行,每行一个整数,表示其质因子分解后因子个数的奇偶性。 输入样例: 3 2 6 8 输出样例: 1 0 1 代码实现(使用Scanner): ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); while (n-- > 0) { int x = scanner.nextInt(); int cnt = 0; for (int i = 2; i <= Math.sqrt(x); i++) { if (x % i == 0) { cnt++; while (x % i == 0) { x /= i; } } } if (x > 1) { cnt++; } System.out.println(cnt % 2); } } } ``` 上述代码中,我们使用了Scanner类从标准输入中读取数据。当输入数据较多时,可能会出现超时的情况,此时可以考虑使用BufferedReader类进行优化。 ### 回答2: Scanner是Java中的一个类,用于从输入流中读取用户输入的数据。当使用Scanner输入超时时,可以考虑以下几个方法来解决。 方法一:使用线程的join方法 通过创建一个新线程来运行Scanner读取输入的代码,然后设置一个超时时间,当超时时,使用join方法强制结束线程。 ```java import java.util.Scanner; public class TimeoutExample { public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(() -> { Scanner scanner = new Scanner(System.in); System.out.print("请输入:"); String input = scanner.nextLine(); System.out.println("你输入的是:" + input); scanner.close(); }); thread.start(); thread.join(5000); // 设置超时时间为5秒 if (thread.isAlive()) { thread.interrupt(); System.out.println("输入超时!"); } } } ``` 方法二:使用ExecutorService的submit方法和Future的get方法 ExecutorService是Java提供的一个用于管理线程池的类,可以使用它的submit方法来提交需要执行的任务,并通过调用Future的get方法设置超时时间。 ```java import java.util.Scanner; import java.util.concurrent.*; public class TimeoutExample { public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException { ExecutorService executor = Executors.newSingleThreadExecutor(); Future<String> future = executor.submit(() -> { Scanner scanner = new Scanner(System.in); System.out.print("请输入:"); String input = scanner.nextLine(); System.out.println("你输入的是:" + input); scanner.close(); return input; }); try { String result = future.get(5, TimeUnit.SECONDS); // 设置超时时间为5秒 System.out.println("输入的结果是:" + result); } catch (TimeoutException e) { future.cancel(true); System.out.println("输入超时!"); } executor.shutdown(); } } ``` 以上两种方法均可以有效解决Scanner输入超时的问题,可以根据具体需求选择适合的方法来使用。 ### 回答3: 当使用Scanner输入时,可能会遇到输入超时的情况。这种情况多数是由于用户没有按时输入所导致的。 要解决这个问题,可以通过使用线程的方式来实现输入超时的控制。具体步骤如下: 1. 首先,创建一个Scanner对象用于获取用户输入。 2. 然后,创建一个线程,用于在规定时间内接收用户输入。在该线程中,可以使用Scanner的nextLine()方法来等待用户输入,代码如下: ``` Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); ``` 3. 在主线程中,启动上述创建的线程,并同时开始计时。代码如下: ``` Thread inputThread = new Thread(new Runnable() { public void run() { // 向用户显示输入提示 System.out.print("请在5秒内输入:"); // 等待用户输入 input = scanner.nextLine(); } }); inputThread.start(); // 等待用户输入的线程的时间 try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } // 五秒之后,检查用户是否输入 if (input == null) { System.out.println("时间已过,未接收到输入!"); } else { System.out.println("你输入的是:" + input); } ``` 4. 最后,根据需求设置适当的输入超时时间。在上述代码中,我们设置了5秒的超时时间。 通过上述步骤,我们可以实现对用户输入超时的控制。当然,在实际使用中,还需要根据具体的需求对代码进行相应的修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值