STL deque容器不支持通过指针偏移读取元素(cplusplus.com意见反馈)

问题反馈过程

http://www.cplusplus.com 是介绍标准c++语言的一个权威网站,在其对deque容器的介绍中,有这样一句(反馈后现已更改):

But, unlike vectors, deques are not guaranteed to store all its elements in contiguous storage locations, thus not allowing direct access by offsetting pointers to elements.

意思是deque容器与vector容器不同,它不保证将元素的存储在连续的内存区域内,所[……]

>> 阅读全文…

Python中一个正整数和一个负整数相除问题(易错点)

针对Python 2.7 版本,Python3 中不存在该问题

下面这段代码输出结果是什么?

a = 1
b = -2
c = a / b
print c
print int(c)
print int(-0.5)

输出结果为:-1, -1, 0

如果是在C++中

int a = 1;
int b = -2;
int c = a/b;
cout<<c<<endl; //输出 0

这说明一个正整数和一个负整数相除,结果不是期望的结果(-1,而不是期望的0),即在强制转换过程中没有得到预期的强制转换结果。

解决的方法就是:计算过程转换为浮点数,再将结果转化为整数。

a[......]

>> 阅读全文...

类对象数组的初始化与赋值(易错点)

下述代码中声明类Example对象的数组e[2],然后新建了Example(1)对象并赋值给e[0]。通过这种方式设置类对象数组,称为赋值,而不是初始化。

这个过程实际上赋值过程,存在临时对象Example(1)的构造和析构。而通过类对象数组初始化就不存在生成临时对象这一过程。先看类对象数组声明和赋值的代码:

#include <iostream>
using namespace std;

class Example
{
public:
    Example():id(0){cout<<"Example():0"<<endl;} //[......]

>> 阅读全文...

Problem 49 Prime permutations

Problem 49: Prime permutations

https://projecteuler.net/problem=49

The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.

There ar[……]

>> 阅读全文…

Problem 48 Self powers

Problem 48: Self powers

https://projecteuler.net/problem=48

The series, 11 + 22 + 33 + … + 1010 = 10405071317.

Find the last ten digits of the series, 11 + 22 + 33 + … + 10001000.

分析

求 11 + 22 + 33 + … + 10001000 结果的最后10位数字

Python的一行代码即可。但是 c\c++\golang 处理起来就比较麻烦了。但是题目只要求给出结果的最后10位数字即可,问题就简[……]

>> 阅读全文…

Problem 47 Distinct primes factors

Problem 47: Distinct primes factors

https://projecteuler.net/problem=47

The first two consecutive numbers to have two distinct prime factors are:

14 = 2 × 7
15 = 3 × 5

The first three consecutive numbers to have three distinct prime factors are:

644 = 2² × 7 × 23
645 = 3 × 5 × 43
646 = 2 × 17 × 19[……]

>> 阅读全文…

Problem 46 Goldbach’s other conjecture

Problem 46: Goldbach’s other conjecture

https://projecteuler.net/problem=46

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.

  • 9 = 7 + 2×12
  • 15 = 7 + 2×22
  • 21 = 3 + 2×32
  • 25 = 7 + 2×32
  • 27 = 19 + 2×22
  • 33 = 31 + 2×12

It turns[……]

>> 阅读全文…

Problem 45 Triangular, pentagonal, and hexagonal

Problem 45: Triangular, pentagonal, and hexagonal

https://projecteuler.net/problem=45

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, …
Pentagonal Pn=n(3n−1)/2 1, 5, 12, 22, 35, …
Hexagonal Hn[……]

>> 阅读全文…

Problem 43 Sub-string divisibility

Problem 43: Sub-string divisibility

https://projecteuler.net/problem=43

The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.

Let d1 be the 1st digit, d2 be the[……]

>> 阅读全文…

Problem 42 Coded triangle numbers

Problem 42: Coded triangle numbers

https://projecteuler.net/problem=42

The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …

By converting each letter in a word to a number corresponding to its al[……]

>> 阅读全文…

Problem 41 Pandigital prime

Problem 41: Pandigital prime

https://projecteuler.net/problem=41

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.

What is the largest n-digit pandigital prime that exists?

分析

如果一个 n[……]

>> 阅读全文…

Problem 40 Champernowne’s constant

Problem 40: Champernowne’s constant

https://projecteuler.net/problem=40

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021…

It can be seen that the 12th digit of the fractional part is 1.

If dn represents the nth digit of the fra[……]

>> 阅读全文…

Problem 39 Integer right triangles

Problem 39: Integer right triangles

https://projecteuler.net/problem=39

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions max[……]

>> 阅读全文…

Problem 38 Pandigital multiples

Problem 38: Pandigital multiples

https://projecteuler.net/problem=38

Take the number 192 and multiply it by each of 1, 2, and 3:

192 × 1 = 192
192 × 2 = 384
192 × 3 = 576

By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 a[……]

>> 阅读全文…

Problem 37 Truncatable primes

Problem 37: Truncatable primes

https://projecteuler.net/problem=37

The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797,[……]

>> 阅读全文…