供参考:
(有待思考)#define _CRT_SECURE_NO_WARNINGS
#include
#include
#includeusing namespace std;
void index(char* s1, char* s2)
{
int len ,max = 0;
char temp[50];
while (*s1)
{
while (*s1 == ’ ’ && *s1 != ‘\0’) s1++;//过滤空格;
len = 0;
while (*s1 != ' ' && *s1 != '\0')
{
*(temp + len) = *s1;//不能用*temp=*s1,why?
len++;
s1++;
}
*(temp + len) = '\0';//注意这种方式。
if (len > max)
{
max = len;
strcpy(s2, temp);
}
if (*s1 == '\0') break;
}}
int main()
{
char s1[50],s2[50];cin.get(s1,50); index(s1, s2);
cout << “s2:” << s2;
}用队列的方法输出杨辉三角:#include
using namespace std;
const int maxsize = 100;
typedef struct {
int Q[maxsize];//存放数据
int front, rear;
}sequeue;
sequeue qu;
void setkong(sequeue& qq)
{
qq.front = 0;
qq.rear = 0;
}//置队空
void rudui(sequeue& qq, int x)
{
if (qq.front == (qq.rear + 1) % maxsize)
cout << “overflow\n”;
else
{
qq.Q[qq.rear] = x;
qq.rear = (qq.rear + 1) % maxsize;
}
}
void chudui(sequeue &qq, int& x)
{
if (qq.front == qq.rear)
{
cout << “underflow\n”;
}
else
{
x = qq.Q[qq.front];
qq.front = (qq.front + 1) % maxsize;
}
}
void getfront(sequeue qq, int &x)//读取队头元素
{
if (qq.front == qq.rear)
{
cout << “error!\n”;
}
else
{
x = qq.Q[qq.front];
}
}
int empty(sequeue qq)//判断队列是否为空
{
if (qq.front == qq.rear)
return 1;
else
return 0;
}
void yanghui(int n,sequeue qu)
{
int i, j,s,t;
setkong(qu);
rudui(qu, 1); rudui(qu, 1);
cout << endl;
cout.width(4); cout << 1;
cout.width(4); cout << 1<<endl;
for (i = 2; i <= n; i++)//生成并输出杨辉三角第i~n行的数据
{
rudui(qu, 1);
cout.width(4); cout << 1;
chudui(qu, s);
for (j = 2; j <= i; j++)//处理第i行中间的各数据
{
chudui(qu, t);
rudui(qu, s + t);
cout.width(4); cout << s + t;
s = t;
}
rudui(qu, 1);
cout.width(4); cout << 1<<endl;
}
cout << endl;
}
int main()
{
int m;
cin >> m;
yanghui(m, qu);
}
对于找出一组数中的最大值及其出现的次数问题,根据参考资料代码,需要使用两个变量max和count来记录最大值和出现次数。比较每个数与最大值的大小,并根据大小关系进行相应的操作,统计最大值的出现次数。具体解决方案如下:
#include <iostream>
#define maxn 1000001
int a[maxn],n;
int max=-100000000;
int count=0;
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]>max)
{
max=a[i];
count=1;
}
else if(a[i]==max)
{
count++;
}
}
printf("max=%d count=%d\n",max,count);
return 0;
}
改进意见:
读入多行英文字符,统计其中单词的个数。单词用空格或回车分隔,空格或回车数可以是多个,可以在有多个空格或回车的情况下进行计算。但在当前代码的实现中,在是连续空行或连续空格的情况下会计算数量,如果需要解决这个问题,可以将字符转换为整型变量存储,同时每次遇到字符时判断变量的值,若其前一位的变量值为0,说明当前字符为单词的第一个字母,单词总数加1,否则直接跳过。修改后的函数如下:
#include<stdio.h>
#include<string.h>
#include<ctype.h> //字符处理库
#define MAXN 10000 //最大单词长度
char s[MAXN]; //单词字符串
int main()
{
int cnt=0; //有效单词计数
memset(s,0,sizeof(s)); //清空字符数组
while(scanf("%s",s)==1) //读入字符串,到达文件结束符终止 loop
{
int flag=1;
for(int i=0;i<strlen(s);i++) //循环读取每一个字符串
{
if(!isalpha(s[i])) //判断是否字母,不是则后退
{
flag=0;
break;
}
}
if(flag==1) //字母
{
cnt++;
}
memset(s, 0, sizeof(s)); //清空字符数组
}
printf("%d\n", cnt);
return 0;
}
将两个递增的有序链表合并为一个递增有序链表,本题需要开辟三个新的链表,存储两个原有序链表的第i个结点和新生有序链表的最后一个结点。本题解决方案是,比较两个链表的构成部分,将对应信息插入新链表中。插入时采用双指针法,通过两个指针迭代两个链表的构成部分没有重复的部分。
#include<stdio.h>
#include<malloc.h>
typedef struct Node
{
int data;
Node* next;
}LNode,*LinkList;
LinkList heada,headb; //原有序链表
//将两个递增的有序链表合并为一个递增有序链表
Status MergeList(LinkList &La, LinkList &Lb)
{
LinkList head = (LinkList)malloc(sizeof(LNode)); //新生成的链表
LinkList Lc = head; //Lc指向新链表的最后一个结点
LinkList pa = La, pb = Lb; //pa和pb分别指向La、Lb的第一个结点
while (pa && pb)
{
if (pa->data <= pb->data)
{
Lc->next = pa;
Lc = pa;
pa = pa->next;
}
else
{
Lc->next = pb;
Lc = pb;
pb = pb->next;
}
}
Lc->next = pa ? pa : pb;//合并后还有链表没有合并完,next直接连上
return OK;
}
//输出链表
void printlist(LinkList head)
{
while (head->next != NULL)
{
head = head->next;
printf("%d,", head->data);
}
printf("\n");
}
int main()
{
LNode a, b, c, d, e, f, * p = NULL, * q = NULL;
//线性表A, a(1)<a(2)<a(3)<a(4)<a(5)<a(6)<a(7)<a(8)
a.data = 12; b.data = 24; c.data = 36; d.data = 50; e.data = 61; f.data = 77;
a.next = &b; b.next = &c; c.next = &d; d.next = &e; e.next = &f; f.next = NULL;
heada = &a;
//线性表B, b(1)<b(2)<b(3)<b(4)<b(5)<b(6)<b(7)<b(8)
LNode aa, bb, cc, dd, ee, ff;
aa.data = 10; bb.data = 24; cc.data = 28; dd.data = 51; ee.data = 61; ff.data = 72;
aa.next = &bb; bb.next = &cc; cc.next = ⅆ dd.next = ⅇ ee.next = &ff; ff.next = NULL;
headb = &aa;
printf("原有序链表\n");
printlist(heada);
printlist(headb);
printf("新有序链表\n");
MergeList(heada, headb);
printlist(heada->next);
return 0;
}
需要说明的是:本问题的解决方案与题目中给定的段落无关。