在不使用if,while,do...while,for,switch,?:等操作,实现函数void printLess(int k),返回小于k的所有整数
来源:互联网 发布:太阳能电池知乎 编辑:程序博客网 时间:2024/06/01 12:09
这个题为2014网易C++笔试题,考得很烂,这题现场没做出来,只想到是用递归,但是怎么递归就不知道了。网上搜了一下,方法有两种,一种是用类的方式实现,一种是用&& 或||运算符来实现。
类实现方式:通过类的构造函数来实现小于k的整数输出
class test{
public: static int k;
test(){ cout<<k<<endl; }
}
int test::k=1;
void printLess(int k)
{
test t[k-1];
}
&&或||运算符实现:&&、||的运算规则是左边的值可以确定整个式子的值,则不进行右边式子的运算
所以可以使用递归实现
void printLess(int k)
{
printf("%d",k-1);
(k-1)&&printLess(k-1);
}
- 在不使用if,while,do...while,for,switch,?:等操作,实现函数void printLess(int k),返回小于k的所有整数
- C++常考笔试题:不用if,while,do-while,for,打印出所有大于0小于k的整数.函数原型void printLess(int k);
- if,continue,break,while,do-while, switch,return,foreach,for等条件语句的使用
- if/switch/while/do while/for/等循环及判断的简要用法案例
- 不使用while,for,if等实现加法
- if,switch,do,while,for分析
- if switch do while for分析
- if, switch, do , while, for分析
- 关于 if,switch,do while ,for 你不知道的那点小事
- 在while()/do...while()/for()/switch()环境下,对break的使用。
- 程序流程控制 if switch while do while for
- if switch 具体用法 循环 while do while for
- 控制流程语句if、switch、while、do-while、for
- 循环控制语句for while do-while if switch
- if,switch,while,do..while,for语句在JSP中的用法示例
- 循环与递归--不使用if,while,for,do—while语句来输出所有比已知数N小的自然数
- Swift语法基础:17 - Swift的For循环, While, Do-While, If, Switch
- 不使用for、while等循环实现strlen()函数
- 多线程
- Effective Java读书笔记之clone方法
- 淘宝京东类电商评论标签化的思路
- acfsutil size: ACFS-03008: The volume could not be resized
- [copy]CA认证学习笔记
- 在不使用if,while,do...while,for,switch,?:等操作,实现函数void printLess(int k),返回小于k的所有整数
- 程序员面试、算法研究、编程艺术、红黑树、数据挖掘5大系列集锦
- 【百为学习板】 例程1- LED流水灯
- C# 流概括
- 初用hibernate和struts2结合时的总结
- 在Android 中使用KSOAP2调用WebService
- Android中怎么用纯代码编写布局?
- matlab和数据库连接
- 【Android】使用Chronometer组件实现一个简单的计时器