Java实现求2的100次方

package cn.itchg;

import java.util.ArrayList;

/**
 * Created by CHG on 2017-02-27 16:57.
 */
public class 求2的100次方 {
    public static void main(String[] args) {

        //次方数
        final int COUNT = 100;
        ArrayList list = new ArrayList();
        list.add(1);

        for (int i = 0; i < COUNT; i++) {
            // 进位标志位
            int flag = 0;
            for (int j = list.size() - 1; j >= 0; j--) {
                int p = list.get(j) * 2 + flag;
                flag = p / 10;
                p %= 10;
                list.set(j, p);
                System.out.println("i:"+i+"\tj:" + j + "\tflag:" + flag + "\tp:" + p + "\tlist:" + list.toString());
            }

            // 最高位添加进位
            if (flag == 1) {
                list.add(0, 1);
                System.out.println("\t"+list.toString());
            }
        }

        // 结果
        System.out.println(list.toString());
    }


}

你可能感兴趣的:(java)