C:找出偶数和正数的最长系列 [英] C: Find the longest series of even and positive numbers

查看:90
本文介绍了C:找出偶数和正数的最长系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。



我需要这个程序的帮助:



输入示例:2 4 6 -1 7 3 -2 1

输出:3 3(最长系列的偶数是3,最长系列的正数是3)



以下是代码:



  #include   <   stdio.h  >  
int even( int x)
{
返回 x% 2 == 0 ;
}

int positive( int x)
{
return x> 0;
}
int longest_series( int a [], int n, int (* f)( int )){
int i;
/ * 当前系列的长度为0或1,具体取决于它是否为数组
是否为空* /

int cs = 0 ;
/ * 最长系列是当前系列* /
int ls = cs;
for (i = 0 ; i< n; i ++){
if ((* f)(a [i]))
cs ++;
else
cs = 0 ;
if (cs> ls)
ls = cs;
}
return ls;
}
int main(){
int a [] = { 2 4 6 , - 1 7 3 , - 2 1 },
n = sizeof (a)/ sizeof( int );
printf( %d%d \ n,longest_series(a,n, & even),
longest_series(a,n,& positive));
return 0 ;
}





如果函数的原型是:
$ b $我的问题是如何编写这段代码b

  void 系列( int  *  array  int  n, int (* s)( int ), int  **开头, int  * length); 





感谢您的回复。

解决方案

< blockquote>这是你的作业。我们不会帮你做一件事。花一些时间来看问题,你一定会找到解决方案。效果是你会学到一些东西。但是,如果你认为这样做是浪费时间,那么你可能在错误的课堂上。然后你最好找到另一个你感兴趣的主题。



手头的问题相对容易。我将首先正确缩进代码,然后了解函数 longest_series 的作用。新函数 series 显然应该返回另外两个项目:指向该最长系列的开头及其长度的指针。为指向最长系列的指针创建一个局部变量,并在找到新的最长系列时更新它。这只是几行代码的问题 - 也是一个很好的练习。


Hi.

I need help with this program:

Example input: 2 4 6 -1 7 3 -2 1
Output: 3 3 (longest series of even is 3, longest series of positive is 3)

Here is the code:

#include <stdio.h>
int even(int x)
 {
return x % 2 == 0;
 }

int positive(int x)
{
    return x>0;
}
int longest_series(int a[], int n, int (*f) (int)) {
int i;
/*Length of the current series is  0 or  1 depending on whether it is a array
empty or not*/
int cs = 0;
/* Longest series is current series */
int ls = cs;
for (i = 0; i < n; i++) {
if ((*f)(a[i]))
cs++;
else
cs = 0;
if (cs > ls)
ls = cs;
}
return ls;
}
int main() {
int a[] = {2,4,6,-1,7,3,-2,1},
n = sizeof(a)/sizeof(int);
printf("%d %d\n", longest_series(a, n, &even),
longest_series(a, n, &positive));
return 0;
}



My question is how to write this code if the prototype of function is:

void series(int *array, int n, int (*s)(int), int **beginning, int *length);



Thanks for replies.

解决方案

This is your homework. We would not do you a favor by doing it for you. Take some time to look at the problem and you will certainly find the solution. The effect is that you will learn something. If, however, you think it is a waste of time doing such exercises, then you are probably in the wrong class. Then you would be better off finding another subject that does interest you more.

The problem at hand is relatively easy. I would start by properly indenting the code and then understand what the function longest_series does. The new function series should obviously return two more items: A pointer to the start of that longest series and the length of it. Create a local variable for the pointer to the longest series and update it whenever you have found a new longest series. It's a matter of a few lines of code -- and a good exercise.


这篇关于C:找出偶数和正数的最长系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆