c语言中maxlen-1是什么意思

Python061

c语言中maxlen-1是什么意思,第1张

c语言中maxlen-1是变量的意思。

一个变量让len和1按位做与运算,在这里实际上maxlen-1的作用是,将len按二进制展开,看其最后一位是0还是1,亦即maxlen是偶数还是奇数。

#include<iostream.h>

#include<stdlib.h>

#define QElemType int

#define OVERFLOW -2

#define OK 1

#define ERROR 0

#define MAXQSIZE 100

typedef struct{

QElemType *base

int front

int rear

int length

int maxsize

}SqQueue

int InitQueue(SqQueue &Q,int n)

{

Q.base=new QElemType[n]

if(Q.base==0)return 0

Q.rear=Q.front=0

Q.length=0

Q.maxsize=n

return 1

}//构造空队列Q

int QueueLength (SqQueue Q){

return (Q.rear -Q.front +MAXQSIZE) % MAXQSIZE

}//返回Q的元素个数,即队列的长度

int EnQueue(SqQueue &Q,QElemType e)

{

if(Q.length==Q.maxsize)return 0

Q.base[Q.rear]=e

Q.rear=(Q.rear++)%Q.maxsize

Q.length++

return 1

}//插入元素e为Q的新的队尾元素

int DeQueue(SqQueue &Q,QElemType &e)

{

if(Q.length==0)return 0

e=Q.base[Q.front]

Q.front=(Q.front+1)%Q.maxsize

Q.length--

return 1

}//删除Q的队头元素,用e返回其值

int GetHead(SqQueue Q,QElemType &e)

{

if(Q.length==0)return 0

e=Q.base[Q.front]

return 1

}

bool Empty(SqQueue Q)

{

if(Q.length==0)return true

return false

}

void print(SqQueue Q)

{

int k=Q.front

for(int i=1i<=Q.lengthi++)

{

cout<<Q.base[k]<<" "

k=(k+1)%Q.maxsize

}

cout<<endl

}

void main()

{

SqQueue Q

QElemType e,i

InitQueue(Q,10)

cout<<"1:输入一队列"<<endl

cout<<"2:队列的删除"<<endl

cout<<"3:队列的插入"<<endl

cout<<"4:返回元素个数"<<endl

cout<<"5:退出"<<endl

int s

cin>>s

while(s!=5){

switch (s){

case 1:

cout<<"输入6个元素组成队列: "

for(i=0i<6i++)

{

cin>>e

EnQueue (Q,e)

}

print(Q)

break

case 2:

if(Empty(Q))

cout<<"要删除的元素不存在"

else

{

GetHead( Q,e)

cout<<"要删除的元素为 :"<<e<<endl

DeQueue(Q,e)

cout<<"元素删除后的队列为: "

print(Q)

}

break

case 3:

cout<<"输入准备插入的元素: "

cin>>e

EnQueue(Q,e)

cout<<"插入后的队列为: "

print(Q)break

case 4:

cout<<"元素个数为:"

cout<<QueueLength ( Q)

cout<<endlbreak

case 5:

cout<<"退出程序"

//return

default: cout<<"输入错误,请重新输入!"<<endl

}

cout<<endl<<"请继续选择:"<<endl

cin>>s

}

}

#include<cstdio>

#include<cstring>

using namespace std

const int maxn=100 //这个表示单词可能的最大个数

const int maxl=100 //这个表示单词可能的最长长度,需要根据题目要求确定

int n,maxlen

int len[maxn]

char ch[maxn][maxl]

int max(int a,int b){

return a>b?a:b

}

int main(){

int k=0

while(~scanf("%c",&ch[n][k])){ //每次输入一个字符,n表示当前是第几个单词,k表示是这个单词的第几个字母

if(ch[n][k]!=' ' && ch[n][k]!='\n'){ //不是空格或空行说明还没有读完,k++,接着读下一个

k++continue

}

len[n]=k-1 //计算这个单词的长度

maxlen=max(maxlen,len[n]) //更新最长的单词长度

if(ch[n][k]==' ') k=0,n++ //如果读到空格,说明这个单词读完了

else{ //如果读到空行,说明这组数据读完了,开始输出这组数据的答案

for(int i=0i<=ni++)

if(len[i]==maxlen) //如果长度等于最长的单词长度,这个单词就是最长单词

printf("%s",ch[i]) //输出即可

putchar('\n')

memset(ch,0,sizeof(ch)) //将原来的数组清空

n=0maxlen=0k=0

}

}

return 0

}

我自己测了一组输入数据,答案应该没什么问题了:

Hello sir are you satisfied with my answer ?

Yes I feel thankful of your answer .

How does it feel ?

It feels very good !

输出:

satisfied  

thankful  

does feel  

feels

等等,我才发现是按字典序输出...我这是按输入顺序输出的....

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std

const int maxn=100 //这个表示单词可能的最大个数

const int maxl=100 //这个表示单词可能的最长长度,需要根据题目要求确定

int n,maxlen

struct Word{

int len

char ch[maxl]

void clean(){

memset(ch,0,sizeof(ch))

}

}word[maxn]

int max(int a,int b){

return a>b?a:b

}

bool cmp(const Word &a,const Word &b){//比较两个单词的函数

return strcmp(a.ch,b.ch)<0

}

int main(){

int k=0

while(~scanf("%c",&word[n].ch[k])){ //每次输入一个字符,n表示当前是第几个单词,k表示是这个单词的第几个字母

if(word[n].ch[k]!=' ' && word[n].ch[k]!='\n'){ //不是空格或空行说明还没有读完,k++,接着读下一个

k++continue

}

word[n].len=k-1 //计算这个单词的长度

maxlen=max(maxlen,word[n].len) //更新最长的单词长度

if(word[n].ch[k]==' ') k=0,n++ //如果读到空格,说明这个单词读完了

else{ //如果读到空行,说明这组数据读完了,开始输出这组数据的答案

sort(word,word+n+1,cmp) //将所有单词按照字典序排序

for(int i=0i<=ni++)

if(word[i].len==maxlen) //如果长度等于最长的单词长度,这个单词就是最长单词

printf("%s",word[i].ch) //输出即可

putchar('\n')

for(int i=0i<=ni++)

word[i].clean() //将原来的数组清空

n=0maxlen=0k=0

}

}

return 0

}

上面这个是修改稿。

测试数据:

Hello sir are you satisfied with my answer ?

Yes I feel thankful of your answer .

How does it feel ?

It feels very good !

What feel it does ?

输出结果:

satisfied  

thankful  

does feel  

feels  

What does feel

最后一个What先输出不是错误哦...

因为W是大写...所以字典序比其他的小