2021-04-18

牛客小白月赛33

比赛地址:https://ac.nowcoder.com/acm/contest/11210?from=acm_calendar

A-字符统计

题目大意:给一个一段字符统计这段字符有多少行 多少单词 和多少字符

思路:输入采用一行一行输入,这样多少行就统计出来了,统计单词可以设一个标志位以空格或换行符为准统计个数,字符可以直接用函数计算

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    getchar();
    while(t--)
    {
        int ans=0,sum=0,world=0;
        string s;
        while(getline(cin,s)){
        
        if(s=="=====")break;
        ans++;
        sum+=s.size();
        int f=0;
            for(int i=0;i<s.size();i++){
                if(s[i]==' '||s[i]=='\n'){
                    if(f) world++;
                    f=0;
                }
                else f=1;
            }
            if(f)world++;
        }
        cout << ans << " " << world << " " << sum << endl;
    }
}

B-连分数
题目大意:给你一个可以可以写成连分数形式的分数,要你将它的连分数表示出来。

思路:自己再草稿纸上模拟一下就会发现是怎么表示的,其实就是每次都将大的变成分子,小的变成分母,如果分子可以整除分母就停止

代码:

#include <iostream>
typedef long long ll;
using namespace std;
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b,c,d,cnt=0;
        cin>>a>>b;
        cout<<a<<'/'<<b<<" = ";
        c=a/b;
        d=a%b;
        cout<<c;
        while(d)
        {
            cnt++;//统计要输出多少个'}'
            a=b;
            b=d;
            if(a%b!=0)
                cout<<"+1/{"<<a/b;
            else if(a%b==0)
                cout<<"+1/";
            c=a/b;
            d=a%b;
        }
        if(cnt)
            cout<<c;
        for(int i=0; i<cnt-1; i++)
            cout<<'}';
        cout<<endl;
    }
    return 0;
}

D-购物

题目大意:
给出 S 种商品以及每种商品的数量,N 个人,已知这 N 个人都已有哪些商品,这些人只会购买他们没有的商品,求这 N 个人购买完成后,剩余的商品种类数,

思路:因为需要将商品与其数量一起保存起来,所以可以选择map来储存商品及其数量,然后输入n个人不需要买的商品,对应的商品数量增加,最后遍历一遍map表,将其数量大于n的的商品进行计数,输出计数器即可。

代码:

#include <iostream>
#include <cstdio>
#include<cstring>
#include<algorithm>
#include<map>
typedef long long ll;
using namespace std;
int main()
{
    int t;
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    scanf("%d",&t);
    while(t--)
    {
        int s,n,x,ans=0,num;
        string op;
        map<string,int>arr;
        scanf("%d%d",&s,&n);
        for(int i=0;i<s;i++)
        {
            cin>>op;
            scanf("%d",&num);
            arr[op]=num;
        }
        for(int i=0;i<n;i++)
        {
            cin>>x;
            while(x--)
            {
                cin>>op;
                arr[op]++;
            }
        }
        map<string,int>::iterator it;
        for( it=arr.begin();it!=arr.end();it++)
        {
            if((it->second)-n>0)
                ans++;
        }
        if(ans==0)
            printf("Need to be lucky\n");
        else printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值