C++程序 递增到一定指后递减,然后递减到一定值后停止

来源:百度知道 编辑:UC知道 时间:2024/05/16 17:58:51
请哪位编成高手告诉小弟一下
最后有编码,谢谢!
也可以发给我ljy112ljy@hotmail.com
谢谢一楼大哥的程序,好像是个死循环吧,这样就停不下来了
我的意思是要,一个变量的值从0递增到15,然后再从15变回0,然后变量值就停在0这个指上

给,已经在VC6上编译运行确认:
#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{
int count=0;
int flag=0;
do
{
cout<<count<<endl;
if( !flag ) count++;
else count--;

if(count==15) flag=1;
} while (count>=0);

system("pause");
return 1;
}

for(int i=0,t=0;i==下限值&&t==-1;i+=t) {
if(递增条件) {
t=1;
}else{
t=-1;
}
//其他代码写在这儿
}

死循环?没有啊,到(i==下限值&&t==-1)满足时就结束啦啊!只要你的递增条件可以为假....

template<class T>
T fun(T x,T max,T min)//传入x,递增到max时递减,递减到min时停止传回T
{
while(x<max)
++x;
while(x>min)
--x;
return x;
}

template<class T>
T fun(T x,T max,T min)//传入x,递增到max时递减,递减到min时停止传回T
{
return min;
}