惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
SegmentFault 最新的问题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Attack and Defense Labs
Attack and Defense Labs
F
Full Disclosure
Vercel News
Vercel News
N
News | PayPal Newsroom
The GitHub Blog
The GitHub Blog
H
Hacker News: Front Page
H
Heimdal Security Blog
P
Privacy International News Feed
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cisco Blogs
L
Lohrmann on Cybersecurity
D
Docker
Recent Announcements
Recent Announcements
Security Archives - TechRepublic
Security Archives - TechRepublic
人人都是产品经理
人人都是产品经理
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Proofpoint News Feed
T
Tailwind CSS Blog
C
Check Point Blog
博客园 - 叶小钗
Google Online Security Blog
Google Online Security Blog
Martin Fowler
Martin Fowler
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
S
Secure Thoughts
博客园 - Franky
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
P
Palo Alto Networks Blog
Latest news
Latest news
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
Last Week in AI
Last Week in AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cyberwarzone
Cyberwarzone
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
Hacker News: Ask HN
Hacker News: Ask HN
T
Threatpost
T
Tenable Blog
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学

面试 | 酷 壳 - CoolShell

Leetcode 编程训练 | 酷 壳 - CoolShell C++面试中string类的一种正确写法 | 酷 壳 - CoolShell 为什么我反对纯算法面试题 | 酷 壳 - CoolShell 一个fork的面试题 | 酷 壳 - CoolShell 给程序员新手的一些建议 | 酷 壳 - CoolShell 再谈“我是怎么招聘程序员的”(上) | 酷 壳 - CoolShell 再谈“我是怎么招聘程序员的”(下) | 酷 壳 - CoolShell 面试题:火车运煤问题 | 酷 壳 - CoolShell 又一个有趣的面试题 | 酷 壳 - CoolShell “火柴棍式”程序员面试题 | 酷 壳 - CoolShell 打印质数的各种算法 | 酷 壳 - CoolShell 140个Google的面试题 | 酷 壳 - CoolShell 面试题:布尔变量 | 酷 壳 - CoolShell 我是怎么招聘程序员的 | 酷 壳 - CoolShell
输出从1到1000的数 | 酷 壳 - CoolShell
陈皓 · 2011-01-07 · via 面试 | 酷 壳 - CoolShell

有这样一个面试题——请把从1到1000的数打印出来,但你不能使用任何的循环语句或是条件语句。更不能写1000个printf或是cout用C/C++语言

我相信,大多数人一开始你可能想到的是递归算法:

void f(int n){
    printf("%d\n",n);
    (1000-n) ? f(n+1) : exit(0) ;
}
int main(){
    f(1);
}

当然,题目中说了不能使用条件语句,所以,上面那种解法的不符合题意的,因为还是变向地使用了条件表达式。不过,我们可以用别的方法来让这个递归终止,比如:

除以零,当程序crash,呵呵。

void f(int n){
    printf("%d\n",n);
    n/(1000-n);
    f(n+1);
}

还有这样退出递归的:

void yesprint(int i);
void noprint(int i);

typedef void(*fnPtr)(int);
fnPtr dispatch[] = { yesprint, noprint };

void yesprint(int i) {
    printf("%d\n", i);
    dispatch[i / 1000](i + 1);
}

void noprint(int i) { /* do nothing. */ }

int main() {
      yesprint(1);
}

还有下面这些各种各样的解法:

#include<stdio.h>

/* prints number  i */
void print1(int i) {
    printf("%d\n",i);
}

/* prints 10 numbers starting from i */
void print10(int i) {
    print1(i);
    print1(i+1);
    print1(i+2);
    print1(i+3);
    print1(i+4);
    print1(i+5);
    print1(i+6);
    print1(i+7);
    print1(i+8);
    print1(i+9);
}

/* prints 100 numbers starting from i */
void print100(int i) {
    print10(i);
    print10(i+10);
    print10(i+20);
    print10(i+30);
    print10(i+40);
    print10(i+50);
    print10(i+60);
    print10(i+70);
    print10(i+80);
    print10(i+90);
}

/* prints 1000 numbers starting from i */
void print1000(int i) {
    print100(i);
    print100(i+100);
    print100(i+200);
    print100(i+300);
    print100(i+400);
    print100(i+500);
    print100(i+600);
    print100(i+700);
    print100(i+800);
    print100(i+900);
}

int main() {
        print1000(1);
        return 0;
}

不过,print用得多了一些。我们可以用宏嘛。

#include<stdio.h>
#define Out(i)       printf("%d\n", i++);
#define REP(N)       N N N N N N N N N N
#define Out1000(i)   REP(REP(REP(Out(i))));
void main()
{
    int i = 1;
    Out1000(i);
}

不过,我们应该使用C++的一些特性,比如:

使用构造函数

class Printer
{
public:
    Printer() { static unsigned i=1; cout << i++ << endl;; }

};

int main()
{
    Printer p[1000];
}

或是更为NB的Template:

template<int N>
struct NumberGeneration{
    static void out(std::ostream& os)
    {
        NumberGeneration<N-1>::out(os);
        os << N << std::endl;
    }
};

template<>
struct NumberGeneration<1>{
    static void out(std::ostream& os)
    {
        os << 1 << std::endl;
    }
};

int main(){
    NumberGeneration<1000>::out(std::cout);
}

最后来个BT一点的:

void main(int j) {
    printf("%d\n", j);
    (main + (exit - main)*(j/1000))(j+1);
}

本文来自: http://stackoverflow.com/q/4568645/89806

Loading...