我试图在不使用循环的情况下打印1到100但是我没有实现它。如何使用循环和条件打印数字从1到100并在c ++中返回到1。
答案 0 :(得分:2)
这有效:
#include <iostream>
template <int N>
void print()
{
std::cout << N << std::endl;
print<N+1>();
}
template <>
void print<101>()
{
}
int main()
{
print<1>();
}
使用&#34; clang ++ -std = c ++ 1y -o test print.cpp; ./test"
&#34;回到1&#34;部分内容可以类似地完成,因为它是一个我已经足够记录的面试问题。
答案 1 :(得分:2)
RAII的力量
class Printer
{
Printer()
{
++i;
std::cout << i << std::endl;
}
~Printer()
{
std::cout << i << std::endl;
--i;
}
static int i = 0;
};
int main()
{
std::vector<Printer> p(100);
return 0;
}
答案 2 :(得分:1)
有一些限制吗?静态成员怎么样?
这样就可以了:
std::cout<<1<<std::endl;
std::cout<<2<<std::endl;
std::cout<<3<<std::endl;
std::cout<<4<<std::endl;
std::cout<<5<<std::endl;
std::cout<<6<<std::endl;
std::cout<<7<<std::endl;
std::cout<<8<<std::endl;
std::cout<<9<<std::endl;
std::cout<<10<<std::endl;
std::cout<<11<<std::endl;
std::cout<<12<<std::endl;
std::cout<<13<<std::endl;
std::cout<<14<<std::endl;
std::cout<<15<<std::endl;
std::cout<<16<<std::endl;
std::cout<<17<<std::endl;
std::cout<<18<<std::endl;
std::cout<<19<<std::endl;
std::cout<<20<<std::endl;
std::cout<<21<<std::endl;
std::cout<<22<<std::endl;
std::cout<<23<<std::endl;
std::cout<<24<<std::endl;
std::cout<<25<<std::endl;
std::cout<<26<<std::endl;
std::cout<<27<<std::endl;
std::cout<<28<<std::endl;
std::cout<<29<<std::endl;
std::cout<<30<<std::endl;
std::cout<<31<<std::endl;
std::cout<<32<<std::endl;
std::cout<<33<<std::endl;
std::cout<<34<<std::endl;
std::cout<<35<<std::endl;
std::cout<<36<<std::endl;
std::cout<<37<<std::endl;
std::cout<<38<<std::endl;
std::cout<<39<<std::endl;
std::cout<<40<<std::endl;
std::cout<<41<<std::endl;
std::cout<<42<<std::endl;
std::cout<<43<<std::endl;
std::cout<<44<<std::endl;
std::cout<<45<<std::endl;
std::cout<<46<<std::endl;
std::cout<<47<<std::endl;
std::cout<<48<<std::endl;
std::cout<<49<<std::endl;
std::cout<<50<<std::endl;
std::cout<<51<<std::endl;
std::cout<<52<<std::endl;
std::cout<<53<<std::endl;
std::cout<<54<<std::endl;
std::cout<<55<<std::endl;
std::cout<<56<<std::endl;
std::cout<<57<<std::endl;
std::cout<<58<<std::endl;
std::cout<<59<<std::endl;
std::cout<<60<<std::endl;
std::cout<<61<<std::endl;
std::cout<<62<<std::endl;
std::cout<<63<<std::endl;
std::cout<<64<<std::endl;
std::cout<<65<<std::endl;
std::cout<<66<<std::endl;
std::cout<<67<<std::endl;
std::cout<<68<<std::endl;
std::cout<<69<<std::endl;
std::cout<<70<<std::endl;
std::cout<<71<<std::endl;
std::cout<<72<<std::endl;
std::cout<<73<<std::endl;
std::cout<<74<<std::endl;
std::cout<<75<<std::endl;
std::cout<<76<<std::endl;
std::cout<<77<<std::endl;
std::cout<<78<<std::endl;
std::cout<<79<<std::endl;
std::cout<<80<<std::endl;
std::cout<<81<<std::endl;
std::cout<<82<<std::endl;
std::cout<<83<<std::endl;
std::cout<<84<<std::endl;
std::cout<<85<<std::endl;
std::cout<<86<<std::endl;
std::cout<<87<<std::endl;
std::cout<<88<<std::endl;
std::cout<<89<<std::endl;
std::cout<<90<<std::endl;
std::cout<<91<<std::endl;
std::cout<<92<<std::endl;
std::cout<<93<<std::endl;
std::cout<<94<<std::endl;
std::cout<<95<<std::endl;
std::cout<<96<<std::endl;
std::cout<<97<<std::endl;
std::cout<<98<<std::endl;
std::cout<<99<<std::endl;
std::cout<<100<<std::endl;
std::cout<<99<<std::endl;
std::cout<<98<<std::endl;
std::cout<<97<<std::endl;
std::cout<<96<<std::endl;
std::cout<<95<<std::endl;
std::cout<<94<<std::endl;
std::cout<<93<<std::endl;
std::cout<<92<<std::endl;
std::cout<<91<<std::endl;
std::cout<<90<<std::endl;
std::cout<<89<<std::endl;
std::cout<<88<<std::endl;
std::cout<<87<<std::endl;
std::cout<<86<<std::endl;
std::cout<<85<<std::endl;
std::cout<<84<<std::endl;
std::cout<<83<<std::endl;
std::cout<<82<<std::endl;
std::cout<<81<<std::endl;
std::cout<<80<<std::endl;
std::cout<<79<<std::endl;
std::cout<<78<<std::endl;
std::cout<<77<<std::endl;
std::cout<<76<<std::endl;
std::cout<<75<<std::endl;
std::cout<<74<<std::endl;
std::cout<<73<<std::endl;
std::cout<<72<<std::endl;
std::cout<<71<<std::endl;
std::cout<<70<<std::endl;
std::cout<<69<<std::endl;
std::cout<<68<<std::endl;
std::cout<<67<<std::endl;
std::cout<<66<<std::endl;
std::cout<<65<<std::endl;
std::cout<<64<<std::endl;
std::cout<<63<<std::endl;
std::cout<<62<<std::endl;
std::cout<<61<<std::endl;
std::cout<<60<<std::endl;
std::cout<<59<<std::endl;
std::cout<<58<<std::endl;
std::cout<<57<<std::endl;
std::cout<<56<<std::endl;
std::cout<<55<<std::endl;
std::cout<<54<<std::endl;
std::cout<<53<<std::endl;
std::cout<<52<<std::endl;
std::cout<<51<<std::endl;
std::cout<<50<<std::endl;
std::cout<<49<<std::endl;
std::cout<<48<<std::endl;
std::cout<<47<<std::endl;
std::cout<<46<<std::endl;
std::cout<<45<<std::endl;
std::cout<<44<<std::endl;
std::cout<<43<<std::endl;
std::cout<<42<<std::endl;
std::cout<<41<<std::endl;
std::cout<<40<<std::endl;
std::cout<<39<<std::endl;
std::cout<<38<<std::endl;
std::cout<<37<<std::endl;
std::cout<<36<<std::endl;
std::cout<<35<<std::endl;
std::cout<<34<<std::endl;
std::cout<<33<<std::endl;
std::cout<<32<<std::endl;
std::cout<<31<<std::endl;
std::cout<<30<<std::endl;
std::cout<<29<<std::endl;
std::cout<<28<<std::endl;
std::cout<<27<<std::endl;
std::cout<<26<<std::endl;
std::cout<<25<<std::endl;
std::cout<<24<<std::endl;
std::cout<<23<<std::endl;
std::cout<<22<<std::endl;
std::cout<<21<<std::endl;
std::cout<<20<<std::endl;
std::cout<<19<<std::endl;
std::cout<<18<<std::endl;
std::cout<<17<<std::endl;
std::cout<<16<<std::endl;
std::cout<<15<<std::endl;
std::cout<<14<<std::endl;
std::cout<<13<<std::endl;
std::cout<<12<<std::endl;
std::cout<<11<<std::endl;
std::cout<<10<<std::endl;
std::cout<<9<<std::endl;
std::cout<<8<<std::endl;
std::cout<<7<<std::endl;
std::cout<<6<<std::endl;
std::cout<<5<<std::endl;
std::cout<<4<<std::endl;
std::cout<<3<<std::endl;
std::cout<<2<<std::endl;
std::cout<<1<<std::endl;