专题一 递归调用与枚举算法的例子

BNUOJ4116

Input

输入一个N,求出 (0, N] 以内的质数。

输入数据只有一个数N ,范围: N>=0 && N<=30000

Output

输出(0, N] 以内所有的质数,一个数占一行。

我先后写了两个版本:
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <math.h>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     int a,i;  
  9.     while(a!=EOF)  
  10.     {  
  11.         cin >> a;  
  12.         for( i=2;i<=int(sqrt(a));++i)  
  13.         {  
  14.             if (a%i==0){break;}  
  15.         }  
  16.             if (i==int(sqrt(a))+1) {cout << a << "是素数" << endl;}  
  17.             else{cout << a << "不是素数" << endl;}  
  18.     }  
  19.     return 0;  
  20. }  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <math.h>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     int a,i;  
  9.     while(a!=EOF)  
  10.     {  
  11.         cin >> a;  
  12.         for( i=2;i<=int(sqrt(a));++i)  
  13.         {  
  14.             if (a%i==0){break;}  
  15.         }  
  16.             if (i==int(sqrt(a))+1) {cout << a << "是素数" << endl;}  
  17.             else{cout << a << "不是素数" << endl;}  
  18.     }  
  19.     return 0;  
  20. }  
注意到前者特别考虑2和3,其实是不必要的,只需要调整for的执行内容;这个例子主要在于体会for和break的运用;前者因为cout在for里面,所以必须单独考虑2,3,后者因为cout在for执行语句之外,所以简易。

另外贴出直接判断一个输入的数是否是质数的代码,定一个数是否是质数,体会for和break的运用:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <math.h>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     int a,i;  
  9.     while(a!=EOF)  
  10.     {  
  11.         cin >> a;  
  12.         for( i=2;i<=int(sqrt(a));++i)  
  13.         {  
  14.             if (a%i==0){break;}  
  15.         }  
  16.             if (i==int(sqrt(a))+1) {cout << a << "是素数" << endl;}  
  17.             else{cout << a << "不是素数" << endl;}  
  18.     }  
  19.     return 0;  
  20. }  

BNUOJ4117

Input

两个数,N和K

Output

按大小输出所有的方案。 每个方案一行。

该题可帮助体会函数定义及其调用;

注意体会程序流程(我的见解写于代码注释);

  1. #include <stdio.h>  
  2. int n,k;  
  3. int tt[1];  
  4. /*  tt[pt]表示第pt个数的取值  */  
  5. void di(int pt)  
  6. {  
  7.     int i;  
  8.     if (pt>k) /* 如果前k个数的值都已确定,则输出 */  
  9.     {  
  10.        for(i=1; i<=k; ++i)  
  11.           printf("%d",tt[i]);  
  12.           printf("\n");  
  13.     }  
  14.   
  15.     else  
  16.     {  
  17.         for(i=1;i<=n;++i)  
  18.         {  
  19.             tt[pt] = i;  
  20.             di(pt+1);  
  21.         }  
  22.     }  
  23. }  
  24.   
  25. int main()  
  26.   
  27. {  
  28.     scanf("%d%d",&n,&k);  
  29.     di(1);  
  30.     return 0;  
  31. }  
  32. //注意体会这里的pt,实际上是位数的感觉,一旦位数超过k就左移位数,否则右进位数,一定注意pt和i的值并不是覆盖储存  
  33. //注意,C语言不检查数组的边界,因此程序可以在数组的两边越界。如上例可以写 int tt[1],尽管这导致溢出:  

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值