简体   繁体   English

我没有得到C程序的期望输出

[英]I am not getting desired output for a C program

I am doing ac program but the sample input is not giving the sample output if I use the. 我正在执行ac程序,但如果使用,示例输入将不提供示例输出。 I think my program is not calling the function. 我认为我的程序没有调用该函数。 I guess I have declared it incorrectly. 我想我已经错误地声明了。 Or my logic is wrong. 否则我的逻辑是错误的。 What I am getting is a zero as output. 我得到的是输出为零。 The question is given below. 问题在下面给出。

Write a C function to find the kth occurrence of an integer n in a sequence of non-negative integers, and then call your function from main. 编写一个C函数,以查找非负整数序列中第n个整数n的第k个出现,然后从main调用函数。

Your function should be according to the following declaration: 您的功能应根据以下声明:

int find(int n, int k);

Input 输入项

You are given the input in two lines: 您将获得两行输入:

The first line contains a non-negative integer, say n, and a positive integer k, in that order. 第一行按此顺序包含一个非负整数(例如n)和一个正整数k。 You have to find the kth occurrence of n in the sequence below. 您必须在以下序列中找到n的第k个出现。

The second line consists of a sequence of non-negative integers, terminated with a -1. 第二行由一个以-1结尾的非负整数组成。 The -1 is not part of the sequence. -1不是序列的一部分。

Output 输出量

If n occurs k times in the sequence, then output the index of the kth occurrence of n in the sequence. 如果n在序列中出现k次,则输出在序列中第k次出现n的索引。

If n does not occur k times in the sequence, then output -1. 如果n在序列中未出现k次,则输出-1。

(For example, the second occurrence of the integer 3 in the sequence 1 1 3 2 3 -1 is at position 4 in the sequence. The first index in the sequence is 0.) (例如,整数1在序列1 1 3 2 3 -1中的第二次出现在序列中的位置4。序列中的第一个索引是0。)

Input: 输入:

3 2
1 1 2 3 3 -1

Output: 输出:

4

Code: 码:

#include<stdio.h>

int check(int a,int n ,int k ){
  int f;
   int value;
   int counter=0;
    counter++;

  if (a==n)
  {
    f++;
  }
  if(f==k)
  {
    value= counter;
  }
 return value;
}

int main(void)
{
  int n , k,a;

  int tempo;

  scanf("%d",&n);
  scanf("%d",&k);
  while(a!=-1)
  {
    scanf("%d",&a);

    tempo=check(a,n,k);

  }
  printf("%d",tempo);
         return 0;
         } 

Your check function has numerous problems: 您的check功能有很多问题:

int check(int a,int n ,int k ){

Your prototype does not match the one in the assignment - you're only supposed to take 2 arguments, neither of which is the sequence of values you're checking against. 您的原型与作业中的原型不匹配-您只能采用2个参数,这两个参数都不是您要检查的值序列。 Somehow, someway, you are supposed to access that sequence from within the body of the function, either by referencing a global array ( bad ), or by reading the input sequence from within the body of the function (slightly less bad, and probably the intent of the exercise 1 ). 无论如何,应该通过引用全局数组( bad )或通过从函数体内读取输入序列(稍差一些,并且可能是通过练习的目的1 )。

  int f;           
   int value;      

auto variables are not implicitly initialized in a declaration - their initial value is indeterminate (it may be 0 , it may be a trap representation, it may be a valid non-zero integer value). auto变量未在声明中隐式初始化-它们的初始值不确定 (它可以0 ,它可以是一个陷阱表示,它可以是有效的非零整数值)。 This will cause problems later. 这将在以后引起问题。

   int counter=0;
    counter++;

I think I know what you're trying to go for here, and it won't work as written 2 - counter only exists for the lifetime of the function. 我想我知道您想要在这里做什么,并且它不会像编写2那样起作用- counter仅在函数的生存期内存在。 Each time you call check , a new instance of counter is created and initialized to 0 . 每次调用check ,都会创建一个counter实例并将其初始化为0 It won't remember the value stored in it from a previous call. 它不会记住上一次调用中存储的值。

  if (a==n)
  {
    f++;

f isn't guaranteed to be 0 at this point (or any other specific value). f在这一点上不能保证为0 (或任何其他特定值)。 It could be 0 , or it could be any non-zero value, or even a trap representation (a bit pattern that does not correspond to a valid integer value). 可以0 ,也可以是任何非零值,甚至可以是陷阱表示 (不对应于有效整数值的位模式)。

  }
  if(f==k)
  {
    value= counter;
  }
 return value;
}

At this point, counter is only ever going to be 1 - you initialize it to 0 at function entry and immediately increment it, then you never touch it again . 此时, counter永远只会是1在函数输入时将其初始化为0并立即将其递增,然后再也无需触摸它 So value is only ever going to be indeterminate or 1 . 因此, value只会是不确定的1

So, how should you proceed from here and satisfy the requirements of the assignment? 那么,您应该如何从这里着手满足作业要求?

The less bad option is to read the sequence from within the check (or find ) function, although that's still pretty bad (again, I/O should be a separate operation, and we're assuming all input comes through stdin ). 最糟糕的选择是从check (或find )函数中读取序列,尽管那仍然很糟糕(同样,I / O应该是一个单独的操作,并且我们假设所有输入都来自stdin )。

int find( int n, int k )
{
  int next; // next value in the sequence
  ... // additional items for index, number of matches, etc.

  while ( scanf( "%d", &next ) == 1 && next != -1 )
  {
     // update index, does next match n, is it the k'th match, etc.
  }
  ...
}

scanf is a poor tool for interactive input, but at this point is the simpler approach. scanf是用于交互式输入的较差的工具,但目前是较简单的方法。


  1. Which, honestly, isn't any better than keeping a global array. 老实说,这并没有比保留全局数组更好。 I/O should be factored out from computation whenever possible, and if a function *is* required to read from an input stream, that stream should be specified as a parameter to the function - you shouldn't force your code to assume all input comes through stdin . 应该尽可能地从计算中排除I / O,并且如果需要从输入流中读取某个函数*,则应将该流指定为该函数的参数-您不应强迫您的代码承担所有输入通过stdin
  2. counter would need to be declared static for it to retain its value from call to call. counter将需要声明为static ,以保持每次调用之间的值。

My solution is totally extension of what John Bode said above and as John Bode said, you are using more parameters than the preferred. 我的解决方案完全是对John Bode所说的内容的扩展,正如John Bode所说的,您使用的参数比首选的更多。 You should stick to only 2 parameters. 您应该只坚持两个参数。 And as you have two parameters n(for search element) and K(k th occurrence) you cant pass an sequential array to that function, So you should start reading(scanning) the sequence inside the find(). 并且由于您有两个参数n(用于搜索元素)和K(第k个出现),因此无法将顺序数组传递给该函数,因此您应该开始读取(扫描)find()中的序列。

As the program clearly says it terminates with -1. 正如程序明确指出的那样,它以-1结尾。 You can use this to end the loop in terminating the find function. 您可以使用它来结束查找功能的循环。 Scan function returns true as long as it reads. 只要扫描功能读取,它就会返回true。 even for -1 it returns true so you should use the value!=-1. 即使对于-1,它也会返回true,因此您应该使用值!=-1。 And inside the loop you can use your logic of matching and finding the index number. 在循环内部,您可以使用匹配和查找索引号的逻辑。

int find(int n, int k){
   int next;
   int match=0;
   int  index=0; //for parsing the sequence
   while( scanf("%d", &next) ==1 && next!=-1){
       if(next == n){
           match++;
           if(match==k)
              return index;
        }
        index++; //move the index
    }
    return -1;
 }

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

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