zoukankan      html  css  js  c++  java
  • 蓝桥杯大纲大题通用样题“Luhn算法”

    Luhn算法

    import java.util.Scanner;
    
    class BigNum{
        
        int []num;  //位数
         
        public BigNum(){
            num = new int[21];
            for(int i = 0; i < num.length; i++) {
                num[i] = 0;
            }
        }
        
        public BigNum(int n) {
            num = new int[n];
        }
        public boolean full() {
            for(int i = 0; i < num.length; i++) {
                if(num[i] != 9) {
                    return false;
                }
            }
            return true;
        }
         
        public void plusOne() {
            int tag = 0;    //从最后一位开始加,如果其9,将其变为0,另高一位加一,
                            //如果高一位是9,其变为0,高一位加一一次类推直到全为9
                            //tag == 0说明从第0位开始
            if(num[tag]!=9) {
                num[tag]++;
                return;
            }else {
                if(full())  return; //如果全为9则溢出
                 
                //关键算法。。进位操作
                while(true) {
                    num[tag]=0;
                    tag++;
                    if(num[tag] != 9) {
                        num[tag]++;
                        return;
                    }
                }
            }
        }
        public void show() {
            for(int i = num.length-1; i >-1; i--) {
                System.out.print(num[i]);
            }
            System.out.println();
        }
        
        
        
        public void setElem(int pos, int n) {
            num[pos] = n;
        }
         
        public boolean isLuhn() {
            int OddPlus=0;  //奇数位和,在数组中为偶数位和
            int EvenPlus=0; //偶数位和,在数组中为奇数位和
            
            for(int i = 0; i < num.length; i+=2) {
                OddPlus+=num[i];
            }
            
            int num2=-999;
            for(int i = 1; i < num.length; i+=2) {
                num2 = num[i]*2;
                if (num2>9) {
                    num2-=9;
                }
                EvenPlus+=num2;
            }
            OddPlus+=EvenPlus;
            if (OddPlus%10==0) {
                return true;
            }
            return false;
        }
    }
    
    
    public class ComformCreditCard {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner scanner = new Scanner(System.in);
            
            //输入字符串
            String str = scanner.next();
            //从字符串提取数字
            BigNum bNum = new BigNum(str.length());
            
            
            
            //放入数据
            for (int j = 0; j < str.length(); j++) {
                bNum.setElem(j, str.charAt(j)-48);
            }
            
            if (bNum.isLuhn()) {
                System.out.print("成功");
            }else {
                System.out.print("失败");
            }
            
            
            
            
        }
    
    }
  • 相关阅读:
    Codeforces 992C(数学)
    Codeforces 990C (思维)
    Codeforces 989C (构造)
    POJ 1511 Invitation Cards(链式前向星,dij,反向建边)
    Codeforces 1335E2 Three Blocks Palindrome (hard version)(暴力)
    POJ 3273 Monthly Expense(二分)
    POJ 2566 Bound Found(尺取前缀和)
    POJ 1321 棋盘问题(dfs)
    HDU 1506 Largest Rectangle in a Histogram(单调栈)
    POJ 2823 Sliding Window(单调队列)
  • 原文地址:https://www.cnblogs.com/jzl123/p/8351054.html
Copyright © 2011-2022 走看看