您的位置:首页 > 编程语言

【编程珠玑】第六章 程序性能分析

2012-05-14 16:01 155 查看
一,概述

如果要提高软件的性能,需要从下面几个方面入手:

1、算法与数据结构

2、算法调优

3、数据结构重组

4、与系统无关的代码的调优(float取代double)。

5、与系统相关的调优,把经常使用的函数进行加速,比如关键代码使用汇编代替高级语言

6、硬件上使用浮点加速器。

二,习题

2)因子分解

例题:大于1的正整数n可以分解为:n = x1*x2*...*xm。

例如:当n = 12时,共有8中不同的分解式:

12 = 12;

12 = 6 * 2;

12 = 4 * 3;

12 = 3 * 4;

12 = 3 * 2 * 2;

12 = 2 * 6;

12 = 2 * 3 * 2;

12 = 2 * 2 * 3;

编程任务:

对于给定的正整数 n, 编程计算n共有多少种不同的分解式。

Input

有多组输入数据,每组数据的一行,为一个正整数n( 1 < = n < = 2000000000 )。

Output

输入计算出的分解式的数目。每组数据后输出一个回车。

Sample Input

12

Sample Output

8

测试可以输出每个等式的程序(12 = 12需要手动输出,程序没给出)
#include <stdio.h>
int fun(int a,int* aaa,int last=0);
bool Decompose(int a);
int fun(int a,int* aaa,int last)
{
for(int i=a-1;i>1;i--)
{
int num=last;//记录

if(a % i == 0)
{
if(last != 0) //如果进入这个方法,说明因子待分解
{
int temp=a;
for(int j=0;j<last;j++)
temp*=aaa[j];
printf("%d=",temp);
for(int j=0;j<last;j++)
printf("%d*",aaa[j]);
}
else
printf("%d=",a);
aaa[num++]=i;
printf("%d*%d\n",i,a/i);
if(Decompose(a/i))
{
fun(a/i,aaa,num); //递归调用
}
}
}
return 0;
}

bool Decompose(int a)//测试还能不能分解
{
for(int i=a-1;i>1;i--)
{
if(a % i == 0)
{
return true;
}
}
return false;
}
int main()
{
int num=12;
int numlist[12];
fun(num,numlist);
return 0;
}


仅仅返回个数的程序(递归调用)

#include<stdio.h>
int count;
int num;
void derive(int a) //采用递归方法得到所要的数值
{
int i;
for(i=a-1;i>1;i--)
if((a % i)==0)
{
count++;
derive(a/i);
}
}
int main()
{
while(1)
{
printf("\nplease input a number:");
scanf("%d",&num);
if(num==0) break;
count=1;
derive(num);
printf("\nthere are %d comforted",count);
}
return 0;
}


6)效率永远排在正确性后面?

一个大型程序,今天有10个已知的错误,下个月又会有10个新的错误。如果让你在更改当前10个错误和使程序提速10倍,你会选择哪一个?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: