首页 > 专题系列 > Java解POJ > POJ 2498 StuPId [解题报告] Java
2013
11-11

POJ 2498 StuPId [解题报告] Java

StuPId

问题描述 :

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).

输入:

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.

输出:

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.

样例输入:

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

样例输出:

Scenario #1:
1390272

Scenario #2:
335678

Scenario #3:
345778

Scenario #4:
3146592

温馨提示:

Huge input and output,scanf and printf are recommended.

解题代码:

//* @author: [email protected]
import java.util.*;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
  int a=in.nextInt();
  int count=1;
  int[] arr=new int[]{9,3,7};
  while((a--)!=0)
  {
   String s=in.next();
   int total=0;
   int k=0;
   int p=0;
   int t=s.length();
   for(int i=t-1;i>-1;i--)
   {
	char c=s.charAt(i);
	if(c!='?')
	{
         int w=c-48;
	 total+=w*arr[(t-1-i)%3];
	}
	if(c=='?')
	{
	 k=arr[(t-1-i)%3];
	 p=i;
	}
    }
    for(int i=0;i< 10;i++)
    {
	if((total+i*k)%10==0)
	{
	 System.out.println("Scenario #"+count+":");
	 System.out.println(s.substring(0,p)+i+s.substring(p+1));
	 System.out.println();
	 break;
	}
     }
	count++;
			
   }
 }
}

  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. #include <cstdio>
    #include <algorithm>

    struct LWPair{
    int l,w;
    };

    int main() {
    //freopen("input.txt","r",stdin);
    const int MAXSIZE=5000, MAXVAL=10000;
    LWPair sticks[MAXSIZE];
    int store[MAXSIZE];
    int ncase, nstick, length,width, tmp, time, i,j;
    if(scanf("%d",&ncase)!=1) return -1;
    while(ncase– && scanf("%d",&nstick)==1) {
    for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
    std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
    for(time=-1,i=0;i<nstick;++i) {
    tmp=sticks .w;
    for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
    if(j==time) { store[++time]=tmp; }
    else { store[j+1]=tmp; }
    }
    printf("%dn",time+1);
    }
    return 0;
    }