Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 5590 | Accepted: 2892 |
Description
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
Output
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
Source Code
Problem: 2498 Memory: 164K Time: 110MS Language: C Result: Accepted
- Source Code
最重要的還是題意,一開始沒理解好Multiply the digits from back to front (!) with repeating factors 9, 3, 7.(就是把ID號倒過來)這句調了很久。#include<stdio.h> #include<string.h> int main() { int n,num[3]={9,3,7},i,ans,k,sum,x,y,w; char s[9]; scanf("%d",&n); x=n; while(n--) { sum=0,y=0; scanf("%s",s); for(i=strlen(s)-1;i>=0;i--) if(s[i]!='?') { sum+=(s[i]-'0')*num[y%3]; y++; } else { w=i; k=y; y++; } for(i=0;i<=9;i++) { ans=num[k%3]*i; if((sum+ans)%10==0) { s[w]=i+48; printf("Scenario #%d:\n%s\n\n",x-n,s); break; } } } return 0; }