周赛字符串总结

   今天做的字符串的题,突然觉得好多东西我都不知道,通过这次周赛学的东西还不少,

1、scanf和gets居然有好多不同,后者可以读回车,tab,空格,前者不能,但目前不知道是怎么今天比赛题目,为什么用前者就报错,题目:

Mirror, Mirror on the Wall

Time Limit: 1000MS    Memory limit: 65536K

题目描述

        For most fonts, the lowercase letters b and d are mirror images of each other, as are the letters p and q. Furthermore, letters i, o, v, w, and x are naturally mirror images of themselves. Although other symmetries exists for certain fonts, we consider only those specifically mentioned thus far for the remainder of this problem.
      Because of these symmetries, it is possible to encode certain words based upon how those words would appear in the mirror. For example the word boxwood would appear as boowxod, and the word ibid as bidi. Given a particular sequence of letters, you are to determine its mirror image or to note that it is invalid.

输入

    The input contains a series of letter sequences, one per line, followed by a single line with the # character. Each letter sequence consists entirely of lowercase letters.

输出

    For each letter sequence in the input, if its mirror image is a legitimate letter sequence based upon the given symmetries, then output that mirror image. If the mirror image does not form a legitimate sequence of characters, then output the word INVALID.

示例输入

boowxod
bidi
bed
bbb
#

示例输出

boxwood
ibid
INVALID
ddd

代码:

#include<stdio.h>
#include<string.h>
char a[10000];
char b[10000];
int main()
{
    while(1)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        scanf("%s",a);//错在输入上应该是scanf而不是gets
        if(a[0]=='#')
            break;
        int flag=0;
        int k=0;
        for(int i=strlen(a)-1; i>=0; i--)
        {
            if(a[i]=='b'||a[i]=='d'||a[i]=='p'||a[i]=='q'||a[i]=='i'||a[i]=='o'||a[i]=='v'||a[i]=='w'||a[i]=='x')
            {

                if(a[i]=='b')
                    b[k++]='d';
                if(a[i]=='d')
                    b[k++]='b';
                if(a[i]=='p')
                    b[k++]='q';
                if(a[i]=='q')
                    b[k++]='p';
                if(a[i]=='i'||a[i]=='o'||a[i]=='v'||a[i]=='w'||a[i]=='x')
                    b[k++]=a[i];
            }
            else
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
        printf("INVALID\n");
        else
        {
            for(int i=0;i<k;i++)
            printf("%c",b[i]);
            printf("\n");
        }
    }
    return 0;
}
/*
boowxod
bidi
bed
bbb
#
*/


题目:犯同样的错误,错在输入上

Voting

Time Limit: 1000MS    Memory limit: 65536K

题目描述

       A committee clerk is good at recording votes, but not so good at counting and figuring the outcome            correctly. As a roll call vote proceeds, the clerk records votes as a sequence of letters, with one letter for
every member of the committee:
             Y means a yes vote      
             N means a no vote      
             P means present, but choosing not to vote     
             A indicates a member who was absent from the meeting
Your job is to take this recorded list of votes and determine the outcome.
Rules:
There must be a quorum. If at least half of the members were absent, respond "need quorum". Otherwise votes are counted.    If there are more yes than no votes, respond "yes".    If there are more no than yes votes, respond "no".   If there are the same number of yes and no votes, respond "tie".

输入

   The input contains of a series of votes, one per line, followed by a single line with the # character. Each vote consists entirely of the uppercase letters discussed above. Each vote will contain at least two letters and no more than 70 letters.

输出

    For each vote, the output is one line with the correct choice "need quorum", "yes", "no" or "tie".

示例输入

YNNAPYYNY
YAYAYAYA
PYPPNNYA
YNNAA
NYAAA
#

示例输出

yes
need quorum
tie
no
need quorum

代码:

#include<stdio.h>
#include<string.h>
char a[100];
int main()
{
    int sum1,sum2,sum3,sum4;
    while(1)
    {
        memset(a,0,sizeof(a));
        scanf("%s",a);
        sum1=0;
        sum2=0;
        sum3=0;
        sum4=0;
        if(a[0]=='#')
        break;
        for(int i=0;i<strlen(a);i++)
        {
            if(a[i]=='Y')
            sum1+=1;
            if(a[i]=='N')
            sum2+=1;
            if(a[i]=='P')
            sum3+=1;
            if(a[i]=='A')
            sum4+=1;
        }
        if(sum4>=(strlen(a)+1)/2)
        printf("need quorum\n");
        else
        {
            if(sum1>sum2)
            printf("yes\n");
            if(sum1<sum2)
            printf("no\n");
            if(sum1==sum2)
            printf("tie\n");
        }
    }
    return 0;
}
/*
YNNAPYYNY
YAYAYAYA
PYPPNNYA
YNNAA
NYAAA
#
*/


 

2、对字符串二维的可以用string a[10]定义,排序可以用字典树排序,还有输入的是两个数字,要比较大小,可以当字符串处理,也可以比较大小,字符串还可以加起来(即连起来),例如a[1]='81',a[2]='54',那么string ans1=a1+a2,string ans2=a2+a1;那么ans1='8154',ans2='5481',要比较两个数的大小,只需要if(ans1<ans2) return ans2;即可,题目:

数字游戏

Time Limit: 1000MS    Memory limit: 65536K

题目描述

        在现实生活中,我们常常会面对数字,而当给定一系列数字的时候,将他们采用不同的顺序排列在一起,是会找到一个最大值的,比如 数字13 和数字 81 ,他们两个和在一起形成的最大值是8113.

输入

      输入一个整数n(n<=50),代表接下来会输入n个数字,当输入n==0的时候,输入结束

输出

      这n个数字所能形成的最大数字

示例输入

2
13 81
0

示例输出

8113

2、代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
string a[100];
int cmp1(string a,string b)
{
    return a>b;
}
int cmp2(string a,string b)
{
    int len=min(a.length(),b.length());
    int flag;
    for(int i=0; i<len; i++)
    {
        if(a[i]!=b[i])
        {
            flag=i;
            break;
        }
    }
    if(flag<len)
    {
        return a>b;
    }
    else
        return a<b;
}
int main()
{
    int n;
    string ans1;
    string ans2;
    while(scanf("%d",&n)!=EOF)
    {
        ans1="";
        ans2="";
        if(n==0)
            break;
        for(int i=1; i<=n; i++)
            cin>>a[i];
        sort(a+1,a+n+1,cmp1);
        for(int i=1; i<=n; i++)
            ans1+=a[i];
        sort(a+1,a+n+1,cmp2);
        for(int i=1; i<=n; i++)
            ans2+=a[i];
        if(ans1>ans2)
            cout<<ans1<<endl;
        else
            cout<<ans2<<endl;
    }
    return 0;
}


3、处理输出好多数,数与数之间有空格时,可以分是否是第一次输出,也可以是判断后边什么时候输出回车,或者是判断前边什么时候开始输出空格

题目:

Mad Scientist  

Time Limit: 1000MS    Memory limit: 65536K

题目描述

  A mad scientist performed a series of experiments, each having n phases. During each phase, a measurement was taken, resulting in a positive integer of magnitude at most k. The scientist knew that an individual experiment was designed in a way such that its measurements were monotonically increasing, that is, each measurement would be at least as big as all that precede it. For example, here is a sequence of measurements for one such experiment with n=13 and k=6:
 
                              1, 1, 2, 2, 2, 2, 2, 4, 5, 5, 5, 5, 6
  It was also the case that n was to be larger than k, and so there were typically many repeated values in the measurement sequence. Being mad, the scientist chose a somewhat unusual way to record the data. Rather than record each of n measurements, the scientist recorded a sequence P of k values defined as follows. For 1 ≤ j ≤ k, P(j) denoted the number of phases having a measurement of j  or less. For example, the original measurements from the above experiment were recorded as the P-sequence:       
                             2, 7, 7, 8, 12, 13
  as there were two measurements less than or equal to 1, seven measurements less than or equal to 2, seven measurement less than or equal to 3, and so on.
Unfortunately, the scientist eventually went insane, leaving behind a notebook of these P-sequences for a series of experiments. Your job is to write a program that recovers the original measurements for the experiments.

输入

   The input contains a series of P-sequences, one per line. Each line starts with the integer k, which is the length of the P-sequence. Following that are the k values of the P-sequence. The end of the input will be designated with a line containing the number 0. All of the original experiments were designed with 1 ≤ k < n ≤ 26.

输出

   For each P-sequence, you are to output one line containing the original experiment measurements separated by spaces.

示例输入

6 2 7 7 8 12 13
1 4
3 4 4 5
3 0 4 5
5 2 2 4 7 7
0

示例输出

1 1 2 2 2 2 2 4 5 5 5 5 6
1 1 1 1
1 1 1 1 3
2 2 2 2 3
1 1 3 3 4 4 4

 

代码:

//用flag判断是不是第一次输出
#include<stdio.h>
int a[30];
int b[30];
int j;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        break;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    a[0]=0;
    for(int i=1;i<=n;i++)
    {
        b[i]=a[i]-a[i-1];
    }
    int flag=0;
    for(int i=1;i<=n;i++)
    {
            for(j=1;j<=b[i];j++)
            {
                if(flag==0)
                {
                    printf("%d",i);
                    flag=1;
                }
                else
                printf(" %d",i);
            }
    }
    printf("\n");
    }
    return 0;
}


 

//判断什么时候开始输出回车
#include<stdio.h>
#include<string.h>
int a[30];
int b[30];
int main()
{
    int n,j,flag;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
            flag=0;//初始化
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        a[0]=0;
        for(int i=1; i<=n; i++)
        {
            b[i]=a[i]-a[i-1];
        }
        for(int i=n; i>=1; i--)
        {
            if(b[i]==0)
                flag=i-1;
            else
                break;
        }
        for(int i=1; i<=n; i++)
        {
            if(i==flag||i==n)
            {
                for(j=1; j<=b[i]; j++)
                {
                    if(j==b[i])
                        printf("%d\n",i);
                    else
                        printf("%d ",i);
                }
                break;
            }
            else
            {
                for(j=1; j<=b[i]; j++)
                    printf("%d ",i);
            }
        }
    }
    return 0;
}
/*
6 2 7 7 8 12 13
1 4
5 2 2 4 7 7
3 4 4 5
3 0 4 5
0
*/


  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值