简体   繁体   English

C 一行多输入

[英]C Multiple Input in One Line

I am solving Problem 'Red Light Green Light' with code 'DOLL' on Code chef.我正在使用 Code chef 上的代码“DOLL”解决问题“Red Light Green Light”。

Code Chef: Red Light Green Light代码厨师:红灯绿灯

In this Problem, I need to take N inputs in one line.在这个问题中,我需要在一行中接受 N 个输入。 I tried using scanf() but to no avail, It didn't worked.我尝试使用 scanf() 但无济于事,它没有用。 It was talking N inputs in N line which isn't what I want.它在 N 行中谈论 N 个输入,这不是我想要的。 I tried using fgets and strtok.我尝试使用 fgets 和 strtok。 But There was some error coming which I am not understanding.但是出现了一些我不理解的错误。 So How can I do it?那么我该怎么做呢?

void main(){
    int T,N,K,sum_N=0;
    int test_arr[N];
    char name;
    char *token;
    scanf("%d",&T);
    if(T<0||T>100000) printf("Wrong Input Cases");  // Condition Checking for T
    if(sum_N>500000) printf("Exceeded Sum");    //Condition Checking for sum of N
    while(T--){
        scanf("%d %d",&N,&K);
        if((N||K)<0||(N||K)>100000) printf("Wrong Values"); //Conditon Checking for N and K
        // Here N Input in one Line are taken
        fgets(name,sizeof(name),stdin);
        token = strtok(name," ");
        for(int i=0;i<N;i++){
            test_arr[i] = *(token+i);
        }

        int ctr=0;
        for(int i=0;i<N;i++){
            if(test_arr[i]>K) ctr++;
        }
        printf("%d",ctr);
    }
}

错误

You need to ask yourself a couple of questions:你需要问自己几个问题:

  • do you need to check for "invalid" input, and reject cases where the input is split over multiple lines.您是否需要检查“无效”输入,并拒绝输入分成多行的情况。
  • do you need to read all the numbers on a line of input WITHOUT knowing before hand how many there are supposed to be.您是否需要在事先不知道应该有多少的情况下读取一行输入中的所有数字?

If the answer to both of those is no , there's no reason to check or care about what is on one line vs what is on another.如果这两个问题的答案都是no ,那么就没有理由检查或关心一行中的内容与另一行中的内容。 Just use scanf to read numbers and don't worry about spacing or what is on which line.只需使用 scanf 读取数字,不用担心间距或哪一行是什么。

scanf("%d", &test_cases);   // number of test cases
for (int i = 0; i < test_cases; ++i) {
    scanf("%d%d", &N, &height);  // number of values and boundary height
    int val[N];
    for (j = 0; j < N; ++j)
        scanf("%d", &val[j]);

Note that there are never any spaces or newlines in the scanf format strings.请注意,scanf 格式字符串中永远不会有任何空格或换行符。 You don't need them, and they might cause confusion.您不需要它们,它们可能会造成混乱。 You just read numbers.你只读数字。 If you're paranoid, you can check the return value of scanf -- it should always be the number of items read (1, 2, or 1 in the 3 calls above), but if it is not, the only useful thing you can do is print an error message and exit.如果您多疑,可以检查 scanf 的返回值——它应该始终是读取的项目数(上面 3 次调用中的 1、2 或 1),但如果不是,那么您唯一有用的东西能做的就是打印错误信息并退出。

If you do need to care about the newlines (most commonly because you don't know how many numbers will be on a line), use fgets + sscanf:如果您确实需要关心换行符(最常见的原因是您不知道一行中有多少数字),请使用 fgets + sscanf:

char buffer[MAXLINE];  // buffer big enough for the largest line
int vals[MAXVALS];     // array large enough for the maximum number of vals
char *p = fgets(buffer, sizeof(buffer));    // read a line
int N = 0, len;
while (N < MAXVALS && sscanf(p, "%d%n", &vals[N], &len) == 1) {
    p += len;
    ++N; }

Change char name;更改char name; to char name[1024]; char name[1024]; or something similar.或类似的东西。 char name; is not a string but a single character.不是字符串而是单个字符。 fgets and strtok is looking for a pointer to a string but getting a character variable. fgetsstrtok正在寻找指向字符串的指针,但得到的是字符变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM