POJ 2190 ISBN

Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

 Status

Description

Farmer John's cows enjoy reading books, and FJ has discovered that his cows produce more milk when they read books of a somewhat intellectual nature. He decides to update the barn library to replace all of the cheap romance novels with textbooks on algorithms and mathematics. Unfortunately, a shipment of these new books has fallen in the mud and their ISBN numbers are now hard to read.  

An ISBN (International Standard Book Number) is a ten digit code that uniquely identifies a book. The first nine digits represent the book and the last digit is used to make sure the ISBN is correct. To verify that an ISBN number is correct, you calculate a sum that is 10 times the first digit plus 9 times the second digit plus 8 times the third digit ... all the way until you add 1 times the last digit. If the final number leaves no remainder when divided by 11, the code is a valid ISBN.  

For example 0201103311 is a valid ISBN, since  
10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 55.  

Each of the first nine digits can take a value between 0 and 9. Sometimes it is necessary to make the last digit equal to ten; this is done by writing the last digit as X. For example, 156881111X is a valid ISBN number.  

Your task is to fill in the missing digit from a given ISBN number where the missing digit is represented as '?'.  

Input

* Line 1: A single line with a ten digit ISBN number that contains '?' in a single position  

Output

* Line 1: The missing digit (0..9 or X). Output -1 if there is no acceptable digit for the position marked '?' that gives a valid ISBN.  

Sample Input

15688?111X

Sample Output

 

1

Source

USACO 2003 Fall Orange

 

/*
00000000?2
-1
156881111?
X
02011?3312
2*/
#include<stdio.h>
#include<string.h>
int main (void)
{
    char a[20];
    scanf("%s",a);
    int i,j,flag, sum=0;
    for(i=0; i<=8; i++)
    {
        if(a[i]=='?')
        {
            flag=10-i;
        }
        else
        {
            sum+=(a[i]-'0')*(10-i);// printf("sum %d\n   ",sum);
        }
    }
    if(a[9]=='X') sum+=10;
   /* if(a[9]>='0'&&a[9]<='9')
    {
        sum=sum+a[9]-'0';
    }*/
    if(a[9]=='?')
    {
        if(sum%11==0)
        {
         printf("0");
         return 0;
        }

        else
        {
            for(i=0; i<=10; i++)
            {
                if(i==10&&(sum+i)%11==0)
                {
                    printf("X");
                    return 0;//break;
                }
                else //if((sum+i)%11==0)
                {
                    if((sum+i)%11==0)
                    {
                         printf("%d",i);//----------------------
                      return 0;//break;
                    }

                }
            }
        }
    }
    if(a[9]!='X') sum+=(a[9]-'0');
    //把所有的已知值加起来sum
    //printf("sum %d\n",sum);
    if(flag==1)//如果最后一位是?
    {
        if(sum%11==0)
        {
            printf("0");return 0;
        }
        if(sum%11!=0)
        {
            for(i=1; i<=10; i++)
            {
                if((sum+i)%11==0)
                {
                    if(i==10)
                    {
                        printf("X");
                        return 0;//break;
                    }
                    else
                    {
                        printf("%d",i);
                        return 0;//break;
                    }
                }
                else
                {
                    printf("-1");
                    return 0;//break;
                }
            }
        }
    }
    if(flag!=1)
    {
       // printf("flag %d\n",flag);
        if(sum%11==0)
        {
             printf("0");
             return 0;
        }

        if(sum%11!=0)
        {

            for(i=1; i<=9; i++)
            {
                //printf("11111111111111111");
                if((sum+i*flag)%11==0)
                {
                    printf("%d",i);
                    return 0;//break;
                }

                /*{

                    printf("-1**");//---------------------
                    return 0;//break;
                }*/
            }
            printf("-1");
            return 0;
        }
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值