算法及算法设计要求

 

style="WIDTH: 89.81%; HEIGHT: 64px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&dt=1192819750500&lmt=1192819750&prev_fmts=468x60_as&format=468x15_0ads_al_s&output=html&correlator=1192819750437&channel=1741427766&pv_ch=1741427766%2B&url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&color_bg=FFFFFF&color_text=000000&color_link=0000FF&color_url=008000&color_border=336699&ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&cc=100&ga_vid=1025434795.1192631677&ga_sid=1192817968&ga_hid=1871659779&ga_fc=true&flash=8&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=2&u_java=true">

style="WIDTH: 44.33%; HEIGHT: 259px" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-4577827332549849&cpa_choice=CAEaCB94-nvUZWENUB9QugJQtwRQTVAgULcCUB4&oe=gb2312&dt=1192720966468&lmt=1192720966&format=250x250_as&output=html&correlator=1192720966453&channel=2735220158&url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-67-1-1.html&color_bg=FFFFFF&color_text=000000&color_link=0000FF&color_url=008000&color_border=336699&ad_type=text_image&region=_google_cpa_region_&ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&cc=100&ga_vid=1025434795.1192631677&ga_sid=1192720940&ga_hid=1155529757&ga_fc=true&flash=8&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=2&u_java=true">一、算法的定义及特性

1、定义:

ispass(int num[4][4])

{ int i,j;
for(i=0;i<4;i++)

for(j=0;j<4;j++)

if(num[j]!=i*4+j+1)/*一条指令,多个操作*/

return 0;

return 1;
}/*上面是一个类似华容道游戏中判断游戏是否结束的算法*/

算法是对特定问题求解步骤的一种描述,它是指令的有限序列,其中每一条指令表示一个或多个操作;此外,一个算法还具有下列五个重要特性:

2、算法的五个特性:

有穷性 一个算法必须总是(对任何合法的输入值)在执行有穷步之后结束,且每一步都可在有穷时间内完成;
确定性 算法中每一条指令必须有确切的含义,读者理解时不会产生二义性。有任何条件下,算法只有唯一的一条执行路径,即对于相同的输入只能得出相同的输出。
可行性 一个算法是能行的,即算法中描述的操作都是可以通过已经实现的基本运算执行有限次来实现的。
输入 一个算法有零个或多个的输入,这些输入取自于某个特定的对象的集合。
输出 一个算法有一个或多个的输出。这些输出是同输入有着某些特定关系的量。

例:

有穷性 haha()
{/*only a joke,do nothing.*/
}
main()
{printf("请稍等...您将知道世界的未日...");
while(1)
haha();
}

确定性 float average(int *a,int num)
{int i;long sum=0;
for(i=0;i<num;i++)
sum+=*(a++);
return sum/num;
}
main()
{int score[10]={1,2,3,4,5,6,7,8,9,0};
printf("%f",average(score,10);
}


可行性  
输入  
输出 getsum(int num)
{
int i,sum=0;
for(i=1;i<=num;i++)
sum+=i;
} /*无输出的算法没有任何意义,


二、算法设计的要求

1、正确性

算法正确性的四个层次
程序不含语法错误。 max(int a,int b,int c)
{
if (a>b)
{if(a>c) return c;
else return a;
}
}

程序对于几组输入数据能够得出满足规格说明要求的结果。 max(int a,int b,int c)
{
if (a>b)
{if(a>c) return a;
else return c;
}
} /* 8,6,7 */ /* 9,3,2 */

程序对于精心选择的典型、苛刻而带有刁难性的几组输入数据能够得出满足规格说明要求的结果。 max(int a,int b,int c)
{
if (a>b)
{if(a>c) return a;
else return c;
}
else
{if(b>c) return b;
else return c;
}
}

程序对于一切合法的输入数据都能产生满足规格说明要求的结果。  

2、可读性

3、健壮性

4、效率与低存储量需求

效率指的是算法执行时间。对于解决同一问题的多个算法,执行时间短的算法效率高。

存储量需求指算法执行过程中所需要的最大存储空间。

两者都与问题的规模有关。

  算法一 算法二
在三个整数中求最大者 max(int a,int b,int c)
{if (a>b)
{if(a>c) return a;
else return c;
}
else
{if(b>c) return b;
else return c;
}/*无需额外存储空间,只需两次比较*/ max(int a[3])
{int c,int i;
c=a[0];
for(i=1;i<3;i++)
if (a>c) c=a;
return c;
}
/*需要两个额外的存储空间,两次比较,至少一次赋值*/

/*共需5个整型数空间*/ 
求100个整数中最大者 同上的算法难写,难读 max(int a[100])
{int c,int i;
c=a[0];
for(i=1;i<100;i++)
if (a>c) c=a;
return c;
}
/*共需102个整型数空间*/ 

三、总结

1、算法的特性

2、算法设计要求:正确性、可读性、健壮性、效率与低存储量需求。

style="WIDTH: 52.39%; HEIGHT: 263px" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-4577827332549849&cpa_choice=CAEaCKcC4yuPlq5lUDRQDVAtUK4BUENQCA&oe=gb2312&dt=1192819388296&lmt=1192819388&format=300x250_as&output=html&correlator=1192819388281&channel=2735220158&url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fviewthread.php%3Ftid%3D70%26page%3D1%26extra%3Dpage%253D1&color_bg=FFFFFF&color_text=000000&color_link=0000FF&color_url=008000&color_border=336699&ad_type=text_image&region=_google_cpa_region_&ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fpost.php%3Faction%3Dedit%26fid%3D16%26tid%3D70%26pid%3D70%26page%3D1%26extra%3Dpage%253D1&cc=100&ga_vid=1025434795.1192631677&ga_sid=1192817968&ga_hid=1527165455&ga_fc=true&flash=8&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=5&u_java=true">

style="WIDTH: 82.23%; HEIGHT: 74px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&dt=1192818497343&lmt=1192818497&format=468x60_as&output=html&correlator=1192818497343&channel=1741427766&url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&color_bg=FFFFFF&color_text=000000&color_link=0000FF&color_url=008000&color_border=336699&ad_type=text_image&ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforumdisplay.php%3Ffid%3D16%26page%3D1&ui=rc%3A6&cc=100&ga_vid=1025434795.1192631677&ga_sid=1192817968&ga_hid=1000484876&ga_fc=true&flash=8&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=6&u_java=true">

style="WIDTH: 58.41%; HEIGHT: 156px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&dt=1192819750656&lmt=1192819750&prev_fmts=468x60_as%2C468x15_0ads_al_s%2C234x60_as&format=200x90_0ads_al_s&output=html&correlator=1192819750437&channel=1741427766&pv_ch=1741427766%2B&url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&color_bg=FFFFFF&color_text=000000&color_link=0000FF&color_url=008000&color_border=336699&ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&cc=100&ga_vid=1025434795.1192631677&ga_sid=1192817968&ga_hid=1871659779&ga_fc=true&flash=8&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=2&u_java=true"> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值