问题 L: Guess The Numbe

题目描述If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1.
·The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer. For other integers, leading zeros are not allowed.)
·The si-th digit from the left is ci. (i=1,2,⋯,M)
Constraints
·All values in input are integers.
·1≤N≤3
·0≤M≤5
·1≤si≤N
·0≤ci≤9
输入Input is given from Standard Input in the following format:

N M
s1 c1

sM cM

输出Print the answer.
样例输入【样例1】
3 3
1 7
3 2
1 7
【样例2】
3 2
2 1
2 3
【样例3】
3 1
1 0
样例输出
【样例1】
702
【样例2】
-1
【样例3】
-1
提示样例1解释
702 satisfies the conditions - its 1-st and 3-rd digits are 7 and 2, respectively - while no non-negative integer less than 702 satisfies them.

这个题好坏啊,有那么多的限制条件,我叫了好几次都wa了,好烦,不过还好,多考虑几遍,就用这个不太漂亮的代码给搞定了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <math.h>
#define ll long long
using namespace std;
//1.n等于1,m=0的时候输出0
//2.n不等1,给第1个位置要求为0。有前导0。输出-1
//3.n等于1,第一位有限制就输出那个,否则输出0.
//4.n>1,第一位没有限制,第一位肯定为1(不能有前导0)。
struct node
{
    int s,c;
} a[10];
int main()
{
    int n,m,i,j,flag=0,sum=0;
    int b[20]={0};//判断位数是否重复给出
    memset(a,0,sizeof(a));//赋值为0,初始化
    scanf("%d%d",&n,&m);
    for(i=0; i<m; i++)
    {
        scanf("%d%d",&a[i].s,&a[i].c);
        b[a[i].s]=1;
    }
    if(n==1&&!m)//第一种情况
    {
        printf("0");
        return 0;
    }
    if(n==1&&m)//情况3
    {
        for(i=0; i<m; i++)
        {
            for(j=1; j<m; j++)
            {
                if(a[i].s==a[j].s&&a[i].c!=a[j].c)
                {
                    printf("-1");
                    return 0;
                }
            }
        }
        printf("%d",a[0].c);
        return 0;
    }
    for(i=0; i<m; i++)//是否有前导零
    {
        if(a[i].s==1&&a[i].c==0)
        {
            printf("-1");
            return 0;
        }
    }
    for(i=0; i<m; i++)
    {
        for(j=1; j<m; j++)
        {
            if(a[i].s==a[j].s&&a[i].c!=a[j].c)//是否重复并且不一样
            {
                printf("-1");
                return 0;
            }
        }
    }
    for(i=0; i<m; i++)
    {
        int r=1;
        if(a[i].s==1)//判断是否有第一位
            flag=1;
        for(j=0; j<(n-a[i].s); j++)
        {
            r*=10;
        }
        if(b[a[i].s])
        {
              sum+=r*a[i].c;
              b[a[i].s]=0;
        }
        }
        if(!flag)
        {
           int t=1;
           for(i=0;i<n-1;i++)
               t*=10;
               sum+=t;
        }
          printf("%d",sum);
          return 0;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值