poj2498

StuPId

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 5507 Accepted: 2839

Description

Background 
At DUT, the Dreamland University of Technology, all students have personal id, numbers with six or seven digits. But they're not just any numbers. Only those that have a checksum with a zero as last digit can be valid ids. 
Problem 
Here's how to compute the checksum of an id number. Multiply the digits from back to front (!) with repeating factors 9, 3, 7. Then simply add the products. Example: 

id number : 1 3 9 0 2 7 2
factors : 9 7 3 9 7 3 9
products : 9 21 27 0 14 21 18

Here the checksum is 9+21+27+0+14+21+18 = 110. The last digit is zero, so the id is valid. Sometimes students have very bad handwriting and the teaching assistents have a hard time identifying the id’s. You're asked to help in special cases, where exactly one digit is unreadable. In that case, the missing digit can be computed (there's always exactly one correct digit, thanks to 9, 3 and 7 being relatively prime to 10). Note that the students always begin concentrated and thus the first digit will always be readable (and not zero).

Input

The first line contains the number of scenarios. Each scenario is a single line that contains an id number with one digit replaced by a question mark and with six or seven digits length.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the correct id number. Terminate the output for the scenario with a blank line.

Sample Input

4
13?0272
3?5678
345?78
314?592

Sample Output

Scenario #1:
1390272

Scenario #2:
335678

Scenario #3:
345778

Scenario #4:
3146592

Hint

Huge input and output,scanf and printf are recommended.

Source


 
水过
#include <cstdio>
#include <cstring>

using namespace std;

const int fac[7]={9,3,7,9,3,7,9};
char temp[10];
int temp_len;
int id[7];
int unread, sum;
int cas;

int main()
{
    scanf("%d",&cas);

    for(int cas_no=1; cas_no<=cas; cas_no++)
    {
        scanf("%s",temp);
        temp_len=strlen(temp);
        sum=0;
        for(int i=0; i<temp_len; i++)
        {
            if(temp[i]!='?')
            {
                id[temp_len-i-1]=temp[i]-'0';
                sum+=id[temp_len-i-1]*fac[temp_len-i-1];
            }
            else
            {
                unread=temp_len-i-1;
            }
        }
        for(int i=0; i<=9; i++)
        {
            if((sum+i*fac[unread])%10==0)
            {
                id[unread]=i;
                break;
            }
        }
        printf("Scenario #%d:\n",cas_no);
        for(int i=temp_len-1; i>=0; i--)
            printf("%d",id[i]);
        printf("\n\n");
    }
    return 0;
}

你可能感兴趣的:(poj)