package com.ywx.count;

import java.util.Scanner;

/**
 * @author Vashon
 * date:20150410
 * 题目:计算1至n的k次方的和
 *
 */
public class Sumdemo03 {
	public static void main(String[] args) {
		System.out.println("请输入两个数(计算1至n的k次方的和):");
		Scanner sc1=new Scanner(System.in);
		Scanner sc2=new Scanner(System.in);
		int n=sc1.nextInt();
		int k=sc2.nextInt();
		System.out.println("1至"+n+"的"+k+"次方之和为:"+sum1(n,k));
	}
	public static long sum1(int n,int k){
		long sum=0;
		for(int i=1;i<=n;i++){
			sum+=sum2(i,k);
		}
		return sum;
	}
	private static long sum2(int i, int k) {
		long power=i;
		for(int j=1;j<k;j++){
			power*=i;
		}
		return power;
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。