005: Cubic sum of integers
Total time limit: 1000ms Memory limit: 65536kB
Description
Given a positive integer k (1<k<10), find the cube and m from 1 to k. That is, m=1+2 2 2+...+k k k.
Input
There is only one line of input, which contains a positive integer k.
Output There
is only one line of output, which contains the sum of cubes from 1 to k.
Sample Input
5
Sample Output
225
Source
Introduction to Calculation 05-Mock Exam 1
Basic operation: 1 Note that s needs to be assigned at the beginning.
2 Since the data range is relatively small, the int type is used.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int k;
int s=0;
cin >> k;
for(int i=1;i<=k;i++)
{
s+=i*i*i;}
cout<<s<<endl;
return 0;
}
The second method:1 uses a function, and the pow function is also suitable for a small data range.
2: Note the need to write the header file
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int k;
int s=0;
cin >> k;
for(int i=1;i<=k;i++)
{
s+=pow(i,3);
}
cout<<s<<endl;
return 0;
}