2018/7/16 刷题小结

这篇博客总结了作者在codeforces.com上做题的经验,涉及的问题包括找出唯一奇数或偶数的位置、处理只包含两种字符的字符串、字符串输入与输出的方法,以及判断字符串是否包含所有字母。文章提到了scanf、getchar、cin、gets、printf等C/C++输入输出函数的使用技巧,并给出了具体代码示例。

codeforces.com

1.25A IQtest

 

 

题意:找出n (3 ≤ n ≤ 100)个整数中唯一的奇数或偶数,输出位置

个人代码:

 

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int n;
    int a[105];
    int markji=0,markou=0;
    int tolji=0,tolou=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(a[i]%2==0){
            markou=i;
            tolou++;
        }
        else{
            markji=i;
            tolji++;
        }
    }
    if(tolou==1) printf("%d\n",markou);
    else printf("%d\n",markji);
    return 0;
}

 

 

 

2.734A Anton and Danik

 

 

 

题意:  给一串只包含两种字符的字符串,找到字符数更多的一个

个人代码:

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int n;
    char s;
    int a=0,d=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        cin>>s;
        if(s-'A'==0) a++;
        else d++;
    }
    if(a>d) printf("Anton\n");
    else if(a<d) printf("Danik\n");
    else printf("Friendship\n");
    return 0;
}

字符在输入的时候,scanf会把之前的回车当成是第一个字符,getchar可以解决;

换成字符串string输入,不会把回车当成字符串,但是无法用printf输出整个string;

输入字符串可以char s[]或者char *s=(char*)malloc(sizeof(char));可以用scanf,cin输入;

用gets输入字符串需要getchar,因为会接收之前的回车,gets和scanf不同在于gets会把空格也接收进字符串;

scanf输入时带些无用字符时,scanf("\ns=%s",s);可以输入,不需要getchar;

另外char*s和char s[]一样可以使用,string b可以b+=s,来连接字符串,但不能反向赋值;

总结:字符:cin或者getchar+scanf;

        字符串:char *s,char[]:cin,scanf都行,gets+getchar;  

                    string :cin,cout

        特殊的,要输入一些奇怪的东西,用scanf("\ns=%s",s);

3.268A Games

题意:找出两组数据的相同配对的个数

个人代码:

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    int a[35],b[35];
    for(int i=0;i<n;i++){
        scanf("%d%d",&a[i],&b[i]);
    }
    int time=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
            if(a[i]==b[j])
                time++;
    }
    printf("%d\n",time);
    return 0;
}

4.443A Anton and Letters

题意:找出字母种类数

个人代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
set<char>s;
int main()
{
    char a[1000+5];
    gets(a);
    if(strlen(a)==2) printf("0\n");
    else{
        for(int i=1;i<strlen(a);i+=3)
            s.insert(a[i]);
        printf("%d\n",s.size());
    }
    return 0;
}

gets连空格一起输入,set无重性

5.520A Pangram

题意:判断字符串是否全字母出现

个人代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
set<char>s;
int main()
{
    char a[100+5];
    int n;
    scanf("%d",&n);
    s.clear();
    getchar();
    for(int i=0;i<n;i++){
        scanf("%c",&a[i]);
        if(a[i]-'a'<0)
            a[i]+='a'-'A';
        s.insert(a[i]);
    }
    //printf("size=%d\n",s.size());
    if(s.size()==26) printf("YES\n");
    else printf("NO\n");
    return 0;
}

注意之前发现的,scanf会接收回车,记得加getchar,或者直接cin

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值