附上在线IDE http://www.shucunwang.com/RunCode/cpp/
1 求1000以内所有3或5的倍数的和
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for(int i=0; i<1000; ++i)
{
if(i%3==0 || i%5==0)
{
sum += i;
}
}
cout << "sum:" << sum << endl;
return 0;
}
2 斐波那契数列中不超过四百万的项,求其中为偶数的项之和
#include <iostream>
using namespace std;
int fibonacci(int first, int second, int& total_sum)
{
if(first <= 4000000)
{
int tmp_sum = first + second;
total_sum += first%2==0?first:0;
return fibonacci(second, tmp_sum, total_sum);
}
return first;
}
int main() {
int sum = 0;
fibonacci(1,2,sum);
cout << "even-sum:" << sum << endl;
return 0;
}
3 600851475143最大的质因数是多少
#include <iostream>
using namespace std;
int largest_prime_factor(long n)
{
int min = 2;
while (n > min) {
for (int i = min; i <= n; i++) {
if (n % i == 0) {
n = n / i;
min = i;
break;
}
}
}
return min;
}
int main() {
int max_prime_factor = largest_prime_factor(600851475143);
cout << "max:" << max_prime_factor << endl;
return 0;
}
4 由两个3位数相乘得到的最大回文乘积
#include <sstream>
#include <iostream>
using namespace std;
bool is_palindromic(int num)
{
std::ostringstream out;
out << num;
string num_str = out.str();
int max_size = num_str.size();
for(int i=0; i<max_size/2; ++i)
{
if(num_str[i] != num_str[max_size-1-i])
{
return false;
}
}
return true;
}
int get_max_palindromic()
{
int max = 1;
for(int i=999; i>=1; --i)
{
for(int j=999; j>=1; --j)
{
int mul_nm = i*j;
bool is_palindromic_num = is_palindromic(mul_nm);
if(is_palindromic_num)
{
max = mul_nm > max ? mul_nm : max;
}
}
}
return max;
}
int main()
{
int max_palindromic = get_max_palindromic();
cout << "result:" << max_palindromic << endl;
return 0;
}
5. 最小的能够被1到20整除的正数--最小倍数
#include <iostream>
#include <map>
#include <cmath>
using namespace std;
void get_num_prime(int num, map<int,int>& out_all_prime)
{//获取一个数的所有质数
map<int,int> num_prime;
while(num >= 2)
{
for(int i=2; i<=num; ++i)
{
if(num%i==0)
{//找到一个质数,存储
if(num_prime.find(i) == num_prime.end())
{
num_prime[i] = 1;
}
else
{
num_prime[i]++;
}
num = num/i;
}
}
}
//合并获得的质数
map<int,int>::iterator num_it=num_prime.begin();
for(; num_it!=num_prime.end(); ++num_it)
{
int cur_prime = num_it->first; //质数
int cur_prime_count = num_it->second; //质数的个数
if(out_all_prime.find(cur_prime) == out_all_prime.end())
{
out_all_prime[cur_prime] = cur_prime_count;
}
else
{
int num_prime_count = out_all_prime[cur_prime];
out_all_prime[cur_prime] = max(cur_prime_count, num_prime_count);
}
}
}
int main() {
map<int, int> all_prime;
for(int i=1; i<=20; ++i)
{
get_num_prime(i, all_prime);
}
int num = 1;
map<int, int>::iterator it = all_prime.begin();
for(; it != all_prime.end(); ++it)
{
num *= pow(it->first, it->second);
}
cout << "smallest positive number: " << num << endl;;
return 0;
}
6.平方的和与和的平方之差
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
int square_sum = 0;
int sum_square = 0;
for(int i=1; i<=100; ++i)
{
square_sum += pow(i*1.0,2);
sum_square += i;
}
sum_square = pow(sum_square*1.0,2);
int diff_val = abs(square_sum - sum_square);
cout << "diff value: " << diff_val << endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int get_prime(int n)
{
int *p_r = new int[n];
int count = 1;
p_r[0] = 2;
p_r[1] = 3;
for(int i=3; count!=n; i+=2)
{
int sqrt_i = floor(sqrt(i*1.0));
int j = 1;
for(j=1; sqrt_i>=p_r[j] && i%p_r[j]!=0; ++j);
if(sqrt_i<p_r[j])
{
p_r[count++] = i;
}
}
return p_r[count-1];
}
int main()
{
cout << "prime:" << get_prime(10001) << endl;
return 0;
}
8.连续数字最大乘积
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
//找出连续n位最大的字符串
long get_max_num(const string& src, int n)
{
if (src.empty())
{
return 0;
}
string max_str = src.substr(0, n);
for (int idx=1; idx<src.size()-n; ++idx)
{
long mul_ret1 = 1;
long mul_ret2 = 1;
//计算n位数的乘积
string com_str = src.substr(idx, n);
for (int i=0; i<n; ++i)
{
if (!max_str.substr(i,1).empty())
{
mul_ret1 *= atoi(max_str.substr(i,1).c_str());
}
if (!com_str.substr(i,1).empty())
{
mul_ret2 *= atoi(com_str.substr(i,1).c_str());
}
}
cout << "max_str:" << max_str << " com_str:" << com_str << endl;
if (mul_ret1 < mul_ret2)
{//赋值比较后的最大值
max_str = com_str;
}
}
cout << "max_str:" << max_str << endl;
return atol(max_str.c_str());
}
int main()
{
string num_str = "73167176531330624919225119674426574742355349194934"
"96983520312774506326239578318016984801869478851843"
"85861560789112949495459501737958331952853208805511"
"12540698747158523863050715693290963295227443043557"
"66896648950445244523161731856403098711121722383113"
"62229893423380308135336276614282806444486645238749"
"30358907296290491560440772390713810515859307960866"
"70172427121883998797908792274921901699720888093776"
"65727333001053367881220235421809751254540594752243"
"52584907711670556013604839586446706324415722155397"
"53697817977846174064955149290862569321978468622482"
"83972241375657056057490261407972968652414535100474"
"82166370484403199890008895243450658541227588666881"
"16427171479924442928230863465674813919123162824586"
"17866458359124566529476545682848912883142607690042"
"24219022671055626321111109370544217506941658960408"
"07198403850962455444362981230987879927244284909188"
"84580156166097919133875499200524063689912560717606"
"05886116467109405077541002256983155200055935729725"
"71636269561882670428252483600823257530420752963450";
static int MAX_STR_LENGTH = 13;
long max_num = get_max_num(num_str, MAX_STR_LENGTH);
long mul_ret = 1;
for (int i=0; i<MAX_STR_LENGTH; ++i)
{
int cur_int = max_num%10;
mul_ret *= cur_int;
max_num /= 10;
}
cout << "mul_ret|" << mul_ret << "|" << endl;
return 0;
}
9.特殊毕达哥拉斯三元组
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int num1,num2,num3;
for(int i=1; i<1000; ++i)
{
for(int j=1;j<1000; ++j)
{
int tmp = i*i + j*j;
int k = sqrt(1.0*tmp);
if(i+j+k == 1000 && tmp==k*k && i<j && j<k)
{
num1 = i;
num2 = j;
num3 = k;
break;
}
}
}
int mul_val = num1*num2*num3;
cout << "mul_val:" << mul_val << endl;
return 0;
}
10.素数的和
#include <iostream>
#include <cmath>
using namespace std;
bool is_prime(int num)
{
if (num < 2)
{
return false;
}
int tmp = sqrt(1.0*num);
for (int i=2; i<=tmp; ++i)
{
if (num%i == 0)
{
return false;
}
}
return true;
}
int main() {
long long sum = 0;
for (int i=2; i<=2000000; ++i)
{
if (is_prime(i))
{
sum += i;
}
}
cout << "sum:" << sum << endl;
return 0;
}