zoukankan      html  css  js  c++  java
  • 11.28天梯赛补题报告

    L1-6 吃火锅 (15分)
     

    chg.jpg

    以上图片来自微信朋友圈:这种天气你有什么破事打电话给我基本没用。但是如果你说“吃火锅”,那就厉害了,我们的故事就开始了。

    本题要求你实现一个程序,自动检查你朋友给你发来的信息里有没有 chi1 huo3 guo1

    输入格式:

    输入每行给出一句不超过 80 个字符的、以回车结尾的朋友信息,信息为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。当读到某一行只有一个英文句点 . 时,输入结束,此行不算在朋友信息里。

    输出格式:

    首先在一行中输出朋友信息的总条数。然后对朋友的每一行信息,检查其中是否包含 chi1 huo3 guo1,并且统计这样厉害的信息有多少条。在第二行中首先输出第一次出现 chi1 huo3 guo1 的信息是第几条(从 1 开始计数),然后输出这类信息的总条数,其间以一个空格分隔。题目保证输出的所有数字不超过 100。

    如果朋友从头到尾都没提 chi1 huo3 guo1 这个关键词,则在第二行输出一个表情 -_-#

    输入样例 1:

    Hello!
    are you there?
    wantta chi1 huo3 guo1?
    that's so li hai le
    our story begins from chi1 huo3 guo1 le
    .
     

    输出样例 1:

    5
    3 2
     

    输入样例 2:

    Hello!
    are you there?
    wantta qi huo3 guo1 chi1huo3guo1?
    that's so li hai le
    our story begins from ci1 huo4 guo2 le
    .
     

    输出样例 2:

    5
    -_-#

    解题思路:一开始不知道string类查找函数的这个用法,傻傻的用字符串数组记录字符然后挨个遍历比较,它就运行超时了。。。。学到了学到了~
    ac代码:
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<map>
    #include<algorithm>
    using namespace std;
    int main(){
        string s;
        int k=0,n=0,flag=0,first;
        while(getline(cin,s)){
            if(s=="."){
                break;
            }
            n++;
            if(s.find("chi1 huo3 guo1")!=string::npos){
                k++;
                if(flag==0){
                    flag=1;
                    first=n;
                }
            }
        }
        cout<<n<<endl;
        if(k){
            cout<<first<<" "<<k<<endl;
        }
        else{
            cout<<"-_-#"<<endl;
        }
        return 0;
    }
    View Code
    L2-1 简单计算器 (25分)
     

    cal.jpg

    本题要求你为初学数据结构的小伙伴设计一款简单的利用堆栈执行的计算器。如上图所示,计算器由两个堆栈组成,一个堆栈 S1​​ 存放数字,另一个堆栈 S2​​ 存放运算符。计算器的最下方有一个等号键,每次按下这个键,计算器就执行以下操作:

    1. 从 S1​​ 中弹出两个数字,顺序为 n1​​ 和 n2​​;
    2. 从 S2​​ 中弹出一个运算符 op;
    3. 执行计算 n2​​ op n1​​;
    4. 将得到的结果压回 S1​​。

    直到两个堆栈都为空时,计算结束,最后的结果将显示在屏幕上。

    输入格式:

    输入首先在第一行给出正整数 N(1),为 S1​​ 中数字的个数。

    第二行给出 N 个绝对值不超过 100 的整数;第三行给出 N1 个运算符 —— 这里仅考虑 +-*/ 这四种运算。一行中的数字和符号都以空格分隔。

    输出格式:

    将输入的数字和运算符按给定顺序分别压入堆栈 S1​​ 和 S2​​,将执行计算的最后结果输出。注意所有的计算都只取结果的整数部分。题目保证计算的中间和最后结果的绝对值都不超过 1。

    如果执行除法时出现分母为零的非法操作,则在一行中输出:ERROR: X/0,其中 X 是当时的分子。然后结束程序。

    输入样例 1:

    5
    40 5 8 3 2
    / * - +
     

    输出样例 1:

    2
     

    输入样例 2:

    5
    2 5 8 4 4
    * / - +
     

    输出样例 2:

    ERROR: 5/0

    解题思路:分别用数组去存,然后错位进行加减乘除运算即可,要注意是倒序运算。
    ac代码:
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<map>
    #include<algorithm>
    using namespace std;
    int main(){
        int n,i,a[1005],sum;
        char s[1005];
        cin>>n;
        for(i=0;i<n;i++){
            cin>>a[i];
        }
        for(i=0;i<n-1;i++){
            cin>>s[i];
        }
        int flag=0;
        if(s[n-2]=='+'){
            sum=a[n-1]+a[n-2];
        }
        else if(s[n-2]=='-'){
            sum=a[n-2]-a[n-1];
        }
        else if(s[n-2]=='*'){
            sum=a[n-1]*a[n-2];
        }
        else if(s[n-2]=='/'){
            if(a[n-1]==0){
                cout<<"ERROR: "<<a[n-2]<<"/0"<<endl;
                flag=1;
            }
            else{
                sum=a[n-2]/a[n-1];
            }    
        }
        if(flag==0){
            for(i=n-3;i>=0;i--){
                if(s[i]=='+'){
                    sum=a[i]+sum;
                }
                else if(s[i]=='-'){
                    sum=a[i]-sum;
                }
                else if(s[i]=='*'){
                    sum=a[i]*sum;
                }
                else if(s[i]=='/'){
                    if(sum==0){
                        cout<<"ERROR: "<<a[i]<<"/0"<<endl;
                        flag=1;
                        break;
                    }
                    else{
                        sum=a[i]/sum;
                    }    
                }
                if(flag==1){
                    break;
                }
            }
        }
        if(flag==0){
            cout<<sum<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    CoreJava Reading Note(9:Collection)
    CoreJava Reading Note(8:Generic programming)
    Algorithms 4th Reading Note(3:Find)
    CoreJava Reading Note(7:Exception,Assertions,Logging)
    Algorithms 4th Reading Note(1:Foundation)
    CoreJava Reading Note(6:Interface,lambda and Inner Class)
    Algorithms 4th Reading Note(2:Sort)
    CoreJava Reading Note(5:Inheritance)
    SpringMVC spring-servlet.xml配置
    MySQL 数据库事物隔离级别的设置
  • 原文地址:https://www.cnblogs.com/nanan/p/14090887.html
Copyright © 2011-2022 走看看