Seeking s = a + aa + aaa + aaaa + aa ... the value of a, where a is a number.

Title: seeking s = a + aa + aaa + aaaa + aa ... the value of a, where a is a number.
E.g. 2 + 22 + 222 + 2222 + 22222 (in this case the sum total of the number 5), the number added by several keyboard.


Program Analysis: The key is to calculate the value of each item.

 

public  class Q8 calculating the added value aaaa {
     public  static  void main (String [] args) { 
        of System.out.print ( "Enter two digits:" ); 
        Scanner in = new new Scanner (the System.in);
         int n-in.nextInt = (); // number size 
        int m = in.nextInt (); // number m of addition 
        Calc (n-, m); 
        in.close (); 
    } 
    / ** 
     * calculation polynomial the value, for example 33 + 3 + 333 + 3333 and print 
     * @param n-digit size 
     * @param how many representative of the sum m
      * / 
    public  static void Calc ( int n-, int m) {
         int Total = 0; // count the total number 
        for ( int I = m; I> 0; i-- ) { 
            System.out.println (the getValue (n-, I)); 
            Total = + the getValue (n-, I); 
        } 
        System.out.println ( "and as:" + Total); 
    } 
    / ** 
     * get the value of the individual, for example, 3333 
     * @param a digital size 
     * @param B several number 
     * @return 
     * / 
    public  static  int getValue (int a, int b) {
        //存储aaaa的值
        int value = 0;
        for(int i= b; i>0; i--) { //3333 = 3000+300+30+3
            value += a;
            a *=10;
        }
        return value;
    }
}

 

Guess you like

Origin www.cnblogs.com/zjulanjian/p/10949439.html
AA
AA