首页 > 代码库 > ProjectEuler 006题
ProjectEuler 006题
题目:
The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
这个也太简单了,不过如果用笔算的话肯定很难,需要思考一番
1 #include<iostream> 2 using namespace std; 3 4 int main() { 5 int N = 100; 6 int squareofsum = ((1+N)*N/2)*((1+N)*N/2); 7 int sumofaquare = 0; 8 for(int i = 1; i <= N; i++) { 9 sumofaquare += i*i; 10 } 11 cout << squareofsum - sumofaquare << endl; 12 system("pause"); 13 return 0; 14 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。