官方网站: http://www.codeblocks.org/
目前的最新版本是 Code::Blocks 17.12 (2019-02-21)
启动 Code::Blocks, 会显示
然后是 Tip 窗口
CodeBlocks 的主界面是这样的
创建一个新项目, 点击
选择
下一步可以设置跳过
选择
输入项目的标题, 这里我们输入
选择编译器, 这里我们选择 GNU 的 GCC 编译器.
然后得到下面的界面
点击左侧的
然后双击
点击工具栏中的
按钮, 或者按快捷键
也可以从菜单栏找到.
编译后, 在 Logs & Others 中可以看到编译的信息.
点击工具栏中的
按钮运行程序, 或者按快捷键
如果不小心关闭了
或者按
这里判断素数采用蛮做的办法.
//==================================
// f0618.cpp
// 求 1 ~ 100,000,000 之内的素数个数
//==================================
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int n){
int sqrtn=sqrt(n*1.0);
for(int i=2;i<=sqrtn;++i)
if(n%i==0) return false;
return true;
}
int main()
{
int num=0;
int N=100000000;
for(int i=2;i<=N;++i)
if(isPrime(i))num++;
cout << "小于等于 " << N << " 的素数个数有 " << num << " 个." << endl;
return 0;
}
试一下这个程序运行的时间, 大概要几十分钟,效率太低。
原因在于判断素数的工作没有积累, 对每个自然数, 其测试都是从头做起, 哪怕单测一个整数是否是素数的效率再高, 对于大量数据测试的工作, 其算法仍显得有些笨拙.
//==================================
// f0619.cpp
// 求 1 ~ 100,000,000 之内的素数个数
// 使用埃拉托色尼筛法(The Sieve of Eratosthenes)
// 这里用到了 bitset
//==================================
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<100000000>* p=new bitset<100000000>;
p->set();//将位集容器中所有元素置为 1.
//p->reset(); 是将位集容器中所有元素置为 0.
//p->test(i); 是取出第 i 个元素的值.
int M=10000;
int N=100000000;
for(int i=2;i<=M;++i)
{
if(p->test(i))
for(unsigned int j=i*i;j<p->size();j+=i)
p->reset(j);
}
int num=0;
for(int i=2;i < N;++i)
{
if(p->test(i))
num++;
}
cout << "小于等于 " << N << " 的素数个数有 " << num << " 个." << endl;
return 0;
}
//==================================
// f0621.cpp
// 求 1 ~ 100,000,000 之内的素数个数
// 使用埃拉托色尼筛法(The Sieve of Eratosthenes)
// 低级编程
//==================================
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int count(unsigned int a){
int sum=0;
for(unsigned int x=a;x;x>>=1)
if(x & 1) sum++;
return sum;
}
void sieve(unsigned int * p){
for(int i=2; i<=10000; ++i)
{
if(p[i/32]&(1 << i%32)){
for(int j=i*i;j < 100000000;j+=i)
p[j/32] &= ~(1 << j%32);
}
}
}
int main()
{
clock_t start=clock();
unsigned int* p =(unsigned int*)malloc(12500000);
if(!p){
printf("no enough memory.\n");
return 1;
}
memset(p,255,12500000);
sieve(p);
int num=-2;
for(int i=0;i < 12500000/4;++i)
num+=count(p[i]);
free(p);
printf("%d, %7.3f\n",num,(clock()-start)/CLK_TCK);
return 0;
}
如果我们要对两个超过 32 位以上的整数(大于
假设
此时我们通常简记为
设
则
注意当
商
于是
将
将 (5) 代入 (4), 得
依次进行下去, 最终得到
注意:
这里进位
rho:=0; for i:=0 to n do begin temp:=x_i+y_i+rho; z_i:=temp mod b; if temp < b then rho:=0 else rho:=1 end; if rho > 0 then overflow
void add(string & x, const string & y)
{
int rho=0;//进位
for(int i=0,j=y.length()-1; j>=0||rho; ++i,--j)
{
rho +=x[i]-'0'+(j>=0?y[j]-'0':0);
a[i]=rho%10+'0';
rho /=10;
}
}