zoukankan      html  css  js  c++  java
  • 字符串中最长的连续出现的字符【指针练习】

    链接:http://ica.openjudge.cn/zz/2/

    总时间限制: 1000ms  内存限制: 65536kB
    描述

    求一个字符串中最长的连续出现的字符,输出该字符及其出现次数,字符串中无空白字符(空格、回车和tab),如果这样的字符不止一个,则输出第一个

    输入
    首先输入N,即测试数据的组数
    每组测试数据输入:
    一行,一个不包含空白字符的字符串,字符串长度小于200
    输出
    一行,输出最长的连续出现的字符及其出现次数,中间用空格隔开
    样例输入
    2
    aaaaabbbbbcccccccdddddddddd
    abcdefghigk
    样例输出
    d 10
    a 1

    代码一:不用指针 

     1 #include<stdio.h>
     2 #include<string.h>
     3 int main(int argc, char *argv[])
     4 {
     5     int n,i,j,k,maxIndex;
     6     char str[205];
     7     char count1[205];
     8     int  count2[205];
     9     
    10     freopen("data.in","r",stdin);
    11     scanf("%d",&n);
    12     for(i=0;i<n;i++)
    13     {
    14         scanf("%s",str);
    15         //memset(count1,' ',sizeof(count1));
    16         //memset(count2,0,sizeof(count2));
    17         j=0;
    18         count1[j]=str[0];
    19         count2[j]=1;
    20         for(k=1;str[k]!='';k++)
    21         {
    22             if(str[k]==count1[j])
    23             {
    24                 count2[j]++;
    25             }
    26             else
    27             {
    28                 j++;
    29                 count1[j]=str[k];
    30                 count2[j]=1;
    31             }
    32         }
    33         
    34         maxIndex=0;
    35         for(k=1;k<=j;k++)
    36         {
    37             if(count2[k]>count2[maxIndex])
    38             {
    39                 maxIndex=k;
    40             }
    41         }
    42         printf("%c %d
    ",count1[maxIndex],count2[maxIndex]);
    43     }
    44     return 0;
    45 }

    代码二:用指针和链表。头插法构造链表  

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 struct obj
     4 {
     5     char ch;
     6     int num;
     7     struct obj *next;
     8 };
     9 int main(int argc, char *argv[])
    10 {
    11     int n,i,j,k;
    12     char str[205];
    13     struct obj *head,*temp,*p;
    14     char maxCh;
    15     int maxNum;
    16     freopen("data.in","r",stdin);
    17     scanf("%d",&n);
    18     for(i=0;i<n;i++)
    19     {
    20         scanf("%s",str);
    21         
    22         head=temp=p=NULL;
    23         
    24         temp=(struct obj *)malloc(sizeof(struct obj));
    25         temp->ch=str[0];
    26         temp->num=1;
    27         temp->next=NULL;
    28         head=temp;
    29         for(j=1;str[j]!='';j++)
    30         {
    31             if(str[j]==head->ch) head->num=head->num+1;
    32             else
    33             {
    34                 temp=(struct obj *)malloc(sizeof(struct obj));
    35                 temp->ch=str[j];
    36                 temp->num=1;
    37                 temp->next=NULL;
    38                 
    39                 temp->next=head;
    40                 head=temp;
    41             }
    42         }
    43         
    44         
    45         p=head;
    46         maxCh=head->ch;
    47         maxNum=head->num;
    48         while(p!=NULL)
    49         {
    50             if(p->num>=maxNum)//注意:这里要有等号,因为题目要求多个相等值选第一个输出。这里是头插法,选最后一个输出即可。 
    51             {
    52                 maxNum=p->num;
    53                 maxCh=p->ch;
    54             }
    55             p=p->next;
    56         }
    57         printf("%c %d
    ",maxCh,maxNum);
    58         
    59         p=head;
    60         while(p!=NULL)
    61         {
    62             temp=p;
    63             p=p->next;
    64             free(temp);
    65         }
    66     }
    67     return 0;
    68 }
  • 相关阅读:
    java 线程的终止与线程中断
    java 线程协作 wait(等待)与 notiy(通知)
    java 线程协作 yield()
    java 线程协作 join()
    python学习 文件操作
    linux 学习 常用命令
    linux 学习 设置固定网Ip
    web 安全
    MySQL数据物理备份之tar打包备份
    MySQL数据物理备份之lvm快照
  • 原文地址:https://www.cnblogs.com/huashanqingzhu/p/7244208.html
Copyright © 2011-2022 走看看