foj部分题解

1001 Duplicate Pair

#include<cstdio>
#include<iostream>
#include<string>
#include<string.h>

using namespace std;

bool arr[1000010];
int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n;
    int i,j;
    int num;

    while(scanf("%d",&n)!=EOF)
    {
        memset(arr,0,sizeof(arr));

        for(i = 0; i < n; i++)
        {
            scanf("%d",&num);
            if(!arr[num])
                arr[num] = true;
            else
                j = num;
        }

        printf("%d\n",j);
    }   
    return 0;
} 

1002 HangOver

#include<cstdio>
#include<iostream>
#include<string>
#include<string.h>

using namespace std;

double num;
double sum = 0; 
double cards = 0;
int car(double num)
{
    while(sum < num)
    {
        cards++;
        sum+= 1/(cards+1);
    }
    return cards;
}


int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);


    while(cin >> num)
    {
        cards = 0;
        sum = 0;
        if(num)
        {
            cout << car(num)<<" card(s)"<<endl;
        }
    }

    return 0;   
}

1008 Hay Points

#include<iostream>
#include<cstdio>
#include<string>
#include<map>
using namespace std;
int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);
    map<string,int> mm;
    int i,j;
    int m,n;
    cin >> m >> n;
    string s;
    int num;
    for(i = 0; i < m; i++)
    {
        cin >> s >> num;
        mm[s] = num;
    }

    int sum = 0;
    for(i = 0; i < n; i++)
    {
        sum = 0;
        while(cin >> s && s[0] != '.')
        {
            sum+=mm[s];
        }
        cout << sum<<endl;
    }

    return 0;   
} 

1054 阅读顺序

#include<iostream> 
#include<cstdio>
#include<string>
using namespace std;

int main()
{
//  freopen("xx.in","r",stdin);
//  freopen("xx.out","w",stdout);

    int n;
    scanf("%d\n",&n);
    string s;
    while(n--)
    {
        getline(cin,s);
        for(int i = s.length()-1; i >=0 ; i--)
            cout << s[i];
        cout <<endl;
    }

    return 0;
}

1055 赋值问题

#include<cstdio>
#include<iostream>
#include<string>
#include<string.h>

using namespace std;

bool arr[10010];
int main()
{
//  freopen("xx.in","r",stdin);
//  freopen("xx.out","w",stdout);
    int i,j;
    int n;

    while(scanf("%d",&n) && n!=-1)
    {
            getchar(); 
            memset(arr,false,sizeof(arr));

            arr['a'] = true;
            char c1,c2;
            for(i = 0; i < n; i++)
            {
                scanf("%c=%c",&c1,&c2);
                arr[c1] = arr[c2];
                getchar();
            }

            bool flag = false;
            int num;
            for(i = 'a'; i <= 'z';i++)
            {
                if(arr[i])
                {
                    flag= true;
                    num = i;
                    printf("%c",i);
                    break;
                }
            }

            for(i = num+1; i <='z';i++)
            {
                if(arr[i])
                {
                    printf(" %c",i);
                }
            }

            if(!flag) cout << "none";

            cout <<endl;    

    }
    return 0;
} 

1056 扫雷游戏

#include<cstdio>
#include<iostream>
#include<string>
#include<string.h>

using namespace std;

int step[8][2] = {{-1,-1},{0,-1},{1,-1},{-1,0},{1,0},{-1,1},{0,1},{1,1}};
char arr[110][110] = {'0'};
int main()
{

    int i,j;
    int m,n;
    int count = 0;
    while(scanf("%d %d",&m,&n)!=EOF)
    {
        memset(arr,'0',sizeof(arr));
        if(m == 0 && n == 0) break;
        getchar();
        for(i = 1; i <= m; i++)
        { 
            for(j = 1; j <= n; j++)
                scanf("%c",&arr[i][j]);
            getchar();
        } 


        for(i = 1; i <= m; i++)
            for(j = 1; j <= n; j++)
            {
                if(arr[i][j] == '.')
                {
                    arr[i][j] = '0';
                    for(int k = 0; k <8;k++)
                    {
                        if(arr[i+step[k][0]][j+step[k][1]] == '*')
                            count++;            
                    }
                    arr[i][j]+=count;
                    count = 0;
                }
            }

        for(i = 1; i <= m; i++)
        { 
            for(j = 1; j <= n; j++)
                printf("%c",arr[i][j]);
            printf("\n");
        } 
        printf("\n");
    }

    return 0; 
} 

1406 凯撒密码

#include<iostream>
#include<cstdio>
#include<string>
#include<cctype>
using namespace std;
int main()
{
    string s;
    while(getline(cin,s))
    {
        for(int i = 0; i < s.length(); i++)
        {
            if(isalpha(s[i]) && (( s[i] <= 'c' && s[i]>='a' ) || (s[i] <= 'C'&& s[i] >= 'A')))
                printf("%c",(s[i]-3)+26);
            else if(isalpha(s[i]))
                printf("%c",s[i]-3);
            else
                printf("%c",s[i]);
        }
        cout <<endl;
    }

    return 0;
}

1664 Top K different numbers

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

bool compare(const int &a,const int &b)
{
    return a>b;
}

int arr[10010];
int arr2[10010];

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n,k;
    while(cin >> n >> k)
    {
        memset(arr,0,sizeof(arr));
        memset(arr2,0,sizeof(arr2));
        int i,j;
        int num;
        for(i = 0; i < n; i++)
            cin >> arr[i];

        sort(arr,arr+n,compare); 

        arr2[0]=arr[0];
        j=0;
        for(i = 1; i < n;i++)
        {
            if(arr[i]!=arr2[j]) 
            {
                arr2[++j] = arr[i];
            }
            if(j>=k-1) break;
        }

        if(j < k-1) cout <<"-1"<<endl;
        else 
        {
            sort(arr2,arr2+k);
            cout <<arr2[0];
            for(i = 1; i <k ;i++)
                cout << " "<<arr2[i];
            cout <<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值