杭电ACM基础题(1984、2052、2054、2072、2074、2087、2140、2163、2203、2206)

1984、Mispelling4

Misspelling is an art form that students seem to excel at. Write a program that removes the nth character from an input string.
Input
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.
Each dataset consists of a single line of input containing M, a space, and a single word made up of uppercase letters only. M will be less than or equal to the length of the word. The length of the word is guaranteed to be less than or equal to 80.
Output
For each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, and the misspelled word. The misspelled word is the input word with the indicated character deleted.
Sample Input

4
4 MISSPELL
1 PROGRAMMING
7 CONTEST
3 BALLOON

Sample Output

1 MISPELL
2 ROGRAMMING
3 CONTES
4 BALOON

Code

//给定一个单词串,将第n个字母去掉,然后输出改变后的单词
#include<iostream>
#include<cstring>
using namespace std;
int main(){
    int t,num=0;
    cin>>t;
    while(t--){
        int n;
        char word[100],s[100];
        cin>>n>>word;
        num++; 
        int len=strlen(word);
        //去掉第n个字母
        int j=0; 
        for(int i=0;i<len;i++){
            if(n==i+1){
                continue;
            }
            s[j]=word[i];
            j++;
        }
        cout<<num<<" ";
        for(int i=0;i<len-1;i++)
            cout<<s[i]; 
        cout<<endl;
    }
    return 0;
} 

2052、Picture[画图]

Give you the width and height of the rectangle,darw it.
Input
Input contains a number of test cases.For each case ,there are two numbers n and m (0 < n,m < 75)indicate the width and height of the rectangle.Iuput ends of EOF.
Output
For each case,you should draw a rectangle with the width and height giving in the input.
after each case, you should a blank line.
Sample Input

3 2

Sample Output

+---+
|   |
|   |
+---+

Code

//画图题
#include<iostream>
using namespace std;
int main(){
    int width,height;
    while(cin>>width>>height){
        for(int i=1;i<=height+2;i++){
            for(int j=1;j<=width+2;j++){
                //第一行和最后一行 
                if(i==1||i==height+2){
                    if(j==1||j==width+2){
                        cout<<"+";
                    }
                    else{
                        cout<<"-";
                    }
                }
                else{
                    if(j==1||j==width+2){
                        cout<<"|";
                    }else{
                        cout<<" ";
                    }
                }
            }
            cout<<endl;
        }
        cout<<endl;
    }
    return 0;
} 

2054、A == B ?[利用字符串存数据]

Give you two numbers A and B, if A is equal to B, you should print “YES”, or print “NO”.
Input
each test case contains two numbers A and B.
Output
for each case, if A is equal to B, you should print “YES”, or print “NO”.
Sample Input

1 2
2 2
3 3
4 3

Sample Output

NO
YES
YES
NO

Code

//判断A==B?,因为A和B的位数可能会很大,故需要借助字符串存储 
#include<iostream>
#include<cstring>
using namespace std;
char a[13000],b[13000];
void change(char c[]){
    int len=strlen(c);
    //判断字符串中是否含有小数点,若有将小数点后的0删除掉 
    if(strstr(c,".")){
        for(int i=len-1;i>=0;i--){
            if(c[i]=='0'){
                c[i]='\0';
                len--; 
            }
            else{
                break;
            }
        }
    }
    //将小数点也去掉
    if(c[len-1]=='.'){
        c[len-1]='\0';
    } 
    
}
int main(){
    while(cin>>a>>b){
        //注意考虑特殊情况如5.00和5,需要去小数点和0
        change(a);
        change(b); 
        if(strcmp(a,b)==0){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
    }
    return 0; 
}

2072、单词数[利用set]

lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input

you are my friend
#
 

Sample Output

4

Code

//统计一行中的不同单词数
//利用set,因为set不允许有重复的元素 
#include<iostream>
#include<string>
#include<set>
using namespace std;
int main(){
    string str;
    while(getline(cin,str)){
        if(str=="#") return 0;
        int len=str.size();
        //直接输换行的情况
        if(len==0){
            cout<<0<<endl;
            continue;
        }
        string s=""; 
        //利用集合set 
        set<string> myset;
        myset.clear();
        bool flag=false;
        for(int i=0;i<len;i++){ 
            while(str[i]>='a'&&str[i]<='z'){
                s+=str[i];
                i++;
                flag=true;
            } 
            if(flag==true){
                myset.insert(s);//将单词插入set中 
                s="";
                flag=false;
            }
        
        }
        cout<<myset.size()<<endl;
    }
    return 0;
} 

2074、叠筐[画图]

需要的时候,就把一个个大小差一圈的筐叠上去,使得从上往下看时,边筐花色交错。这个工作现在要让计算机来完成,得看你的了。
Input
输入是一个个的三元组,分别是,外筐尺寸n(n为满足0<n<80的奇整数),中心花色字符,外筐花色字符,后二者都为ASCII可见字符;
Output
输出叠在一起的筐图案,中心花色与外筐花色字符从内层起交错相叠,多筐相叠时,最外筐的角总是被打磨掉。叠筐与叠筐之间应有一行间隔。
Sample Input

11 B A
5 @ W

Sample Output

 AAAAAAAAA 
ABBBBBBBBBA
ABAAAAAAABA
ABABBBBBABA
ABABAAABABA
ABABABABABA
ABABAAABABA
ABABBBBBABA
ABAAAAAAABA
ABBBBBBBBBA
 AAAAAAAAA 

 @@@ 
@WWW@
@W@W@
@WWW@
 @@@ 

Code

//输出图形--一圈一圈的输出
#include<iostream>
using namespace std;
int main(){
    int n;
    char a,b,c[80];//a为中心花色字符,b为外圈字符 
    char d[80][80]; 
    bool flag=false;
    while(cin>>n>>a>>b){
        if(flag==true){
            cout<<endl;
        }
        //单独考虑n==1 
        if(n==1){
            cout<<a<<endl;
            continue; 
        } 
        //共有t圈 
        int t=n/2+1;
        //t为偶数,则最外圈为b字符 
        if(t%2==0){
            swap(a,b);
        }
        for(int i=1;i<=t;i++){
            if(i%2==1){
                c[i]=a;
            }
            else{
                c[i]=b;
            }
        }
        //从外圈向内圈不断的填充矩阵 
        int m=n-1;
        for(int k=1;k<=t;k++){
            for(int i=k-1;i<=m;i++){
                for(int j=k-1;j<=m;j++){
                    d[i][j]=c[k];
                }
            }
            m=m-1;
        }
        //处理四个角 
        d[0][0]=' ';
        d[0][n-1]=' ';
        d[n-1][0]=' ';
        d[n-1][n-1]=' ';
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cout<<d[i][j];
            }
            cout<<endl;
        } 
        flag=true;
    }
    return 0;
} 

2087、剪花布条[判断字符串中包含的子字符串个数]

一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
Sample Input

abcde a3
aaaaaa  aa
#

Sample Output

0
3

Code

//计算字符串a中包含多少个子字符串b
#include<iostream>
#include<string.h> 
#include<cstring>
using namespace std;
char s1[1200],s2[1200];
int main(){
    while(cin>>s1){
        if(s1[0]=='#') return 0;
        cin>>s2;
        //判断是s2是否是s1的子串 
        if(strstr(s1,s2)==NULL){
            cout<<0<<endl;
            continue;
        }
        int len1=strlen(s1); 
        int len2=strlen(s2);
        int j=0,num=0;
        //将字符串不断的进行分隔为长度为len的字符串 
        for(int i=0;i<len1;i++){
            if(s1[i]==s2[j]){
                j++;
                if(j==len2){
                    num++;
                    j=0;
                }
            }
            else{
                j=0;
            }
        }
        cout<<num<<endl;
    }
    return 0;    
}

2140、Michael Scofield’s letter[字符串按规则解码]

I believe many people are the fans of prison break. How clever Michael is!! In order that the message won’t be found by FBI easily, he usually send code letters to Sara by a paper crane. Hence, the paper crane is Michael in the heart of Sara. Now can you write a program to help Sara encode the letter from Michael easily?
The letter from Michael every time is a string of lowercase letters. You should encode letters as the rules below:
b is ’ ', q is ‘,’, t is ‘!’, m is l, i is e, c is a, a is c, e is i, l is m. It is interesting. Are you found that it is just change michael to leahcim?
Input
The input will consist of several cases, one per line.
Each case is a letter from Michael, the letteres won’t exceed 10000.
Output
For each case, output the encode letter one line.
Sample Input

pmicsibforgevibliqbscrct
ebmovibyout

Sample Output

please forgive me, sara!
i love you!

Code

//字符串解码
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
char letter[11000];
int main(){
    while(cin>>letter){
        int len=strlen(letter);
        string s="";
        for(int i=0;i<len;i++){
            switch(letter[i]){
                case 'b':
                    s+=' ';
                    break;
                case 'q':
                    s+=',';
                    break;
                case 't':
                    s+='!';
                    break;
                case 'm':
                    s+='l';
                    break;
                case 'i':
                    s+='e';
                    break;
                case 'c':
                    s+='a';
                    break;
                case 'a':
                    s+='c';
                    break;
                case 'e':
                    s+='i';
                    break;
                case 'l':
                    s+='m';
                    break;
                default:
                    s+=letter[i];
                    break;
            }
        }
        cout<<s<<endl;
    }
    return 0;
}

2163、Palindromes[回文字符串]

Write a program to determine whether a word is a palindrome. A palindrome is a sequence of characters that is identical to the string when the characters are placed in reverse order. For example, the following strings are palindromes: “ABCCBA”, “A”, and “AMA”. The following strings are not palindromes: “HELLO”, “ABAB” and “PPA”.
Input
The input file will consist of up to 100 lines, where each line contains at least 1 and at most 52 characters. Your program should stop processing the input when the input string equals “STOP”. You may assume that input file consists of exclusively uppercase letters; no lowercase letters, punctuation marks, digits, or whitespace will be included within each word.
Output
A single line of output should be generated for each string. The line should include “#”, followed by the problem number, followed by a colon and a space, followed by the string “YES” or “NO”.
Sample Input

ABCCBA
A
HELLO
ABAB
AMA
ABAB
PPA
STOP

Sample Output

#1: YES
#2: YES
#3: NO
#4: NO
#5: YES
#6: NO
#7: NO

Code

//判断给定的字符串是否是回文字符串
#include<iostream>
#include<cstring>
#include<string> 
using namespace std;
char s[60];
int main(){
    int num=0;
    while(cin>>s){
        string str=s;//将字符数组转化为字符串 
        if(str=="STOP") break;
        bool flag=true;
        num++;
        int len=strlen(s);
        for(int i=0;i<len/2;i++){
            if(s[i]==s[len-1-i]){
                continue;
            }
            else{
                flag=false;
                break;
            }
        }
        if(flag==true){
            cout<<"#"<<num<<": YES"<<endl; 
        }
        else{
            cout<<"#"<<num<<": NO"<<endl;
        }
    }
    return 0;
} 

2203、亲和串

人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现,现在长大了却不知道怎么去判断亲和串了,于是他只好又再一次来请教聪明且乐于助人的你来解决这个问题。
亲和串的定义是这样的:给定两个字符串s1和s2,如果能通过s1循环移位,使s2包含在s1中,那么我们就说s2 是s1的亲和串。
Input
本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二行包含输入字符串s2,s1与s2的长度均小于100000。
Output
如果s2是s1的亲和串,则输出"yes",反之,输出"no"。每组测试的输出占一行。
Sample Input

AABCD
CDAA
ASD
ASDF

Sample Output

yes
no

Code:

//判断亲和串-
//给定两个字符串s1和s2,如果能通过s1循环移位,使s2包含在s1中,那么我们就说s2 是s1的亲和串。
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
char a[200000],b[200000];
int main(){
    while(cin>>a>>b){
        int len1=strlen(a);
        int len2=strlen(b);
        if(len2>len1){
            cout<<"no"<<endl;
            continue;
        }
        //将第一个字符串复制两份用来表示循环移位的结果 
        int len=len1*2;
        for(int i=len1;i<len;i++){
            a[i]=a[i-len1];
        }
        a[len]='\0'; 
        if(strstr(a,b)){
            cout<<"yes"<<endl;
        }
        else{
            cout<<"no"<<endl;
        }
    }
    return 0;
} 

2206、IP地址[判断一个IP 地址是否为有效地址]

在网络课程上,我学到了很多有关IP的知识。IP全称叫网际协议,有时我们又用IP来指代我们的IP网络地址,现在IPV4下用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此不需要用正号出现),如192.168.100.16,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
但是粗心的我,常常将IP地址写错,现在需要你用程序来判断。
Input
输入有多个case,每个case有一行,不超过100个字符。
Output
对于每个case,判断输入的IP是否正确,如果正确输入YES,否则NO。
Sample Input

192.168.100.16

Sample Output

YES

Code:

/*
判断输入的IP地址是否正确 
不正确的IP地址
1、输入的字符是非法字符,即输入的字符只能是.和数字字符 
2、不能以.开头以及不能连续有...,以及.的数量只能为3个 
3、每个.分隔的数字的范围必须是[0,255],且最多有3位数字 
*/ 
#include<iostream>
#include<cstring>
using namespace std;
char ip[100];
int main(){
    while(gets(ip)){
        int len=strlen(ip);
        //正确的ip地址形式如192.168.1.18; 
        int sum=0,num=0,j=0;
        bool flag=true;
        for(int i=0;i<len;i++){
            //不能以.开头 
            if(ip[0]=='.'){
                flag=false;
                break;
            } 
            //ip地址输入的字符不对
            if(ip[i]!='.'&&(ip[i]<'0'||ip[i]>'9')){//注意这里ip[i]<'0'不是数字0
                flag=false;
                break;
            }
            if(ip[i]=='.'){
                //不能连续出现....
                if(sum<0||sum>255){
                    flag=false;
                    break;
                }
                j=0;
                sum=0;
                num++;
                continue;
            }
            else{
                j++;
                sum=sum*10+(ip[i]-'0');
                //数的范围不对 
                if(sum<0||sum>255){
                    flag=false;
                    break;
                }
                //不能连续出现4个非.字符 
                if(j==4){
                    flag=false;
                    break;
                }
            }
        }
        //.不够或.多了 
        if(num<=2||num>3){
            flag=false;
        }
        if(flag){
            cout<<"YES"<<endl; 
        }
        else{
            cout<<"NO"<<endl;
        }
    }
    return 0;
} 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值