while循环的使用及其和for循环的对比总结
2015-04-06 12:15
543 查看
<1>题目:输入数字m和n,删除第m个数字,并且以下一个数字作为起始,求最后一个数字。
总结:
1,查找下一个元素并没有在while中处理0,而是在setNext中处理的
2,while循环中,数组是从0开始的,因此将m减去1,才是要跳过m个数字,主要因为第m哥其实是仅仅跳过m-1个数字
这样就会让while循环执行m次。其实这样一分析就是变形的for循环啊
<2>循环m次,while循环的写法(注意和for循环对比)总结:
外部变量是0的写法
while主体就是
<3>while 和 for循环对比总结:
int neighborCol = sp->getCol() - 1;
while (neighborCol >= 0) {
AnimationSprite *neighborSp = m_animalMatrix[sp->getRow() * m_width + neighborCol];
if (neighborSp && neighborSp->getIgnoreCheck()) { //如果此链中有标记为setIgnoreCheck(true)的特殊元素则不检测
chainList.clear();
return;
}
if (neighborSp && (neighborSp->getImgIndex() == sp->getImgIndex()) && !neighborSp->getIgnoreCheck()) {
CloudSprite *cloudSp = m_cloudMatrix[neighborSp->getRow() * m_width + neighborSp->getCol()];
if (cloudSp) {
return;
}
m_checkSpL = true;
chainList.push_back(neighborSp);
neighborCol--;
}
else {
break;
}
}比如这个向左检查所有可以连接的串,显然while循环更擅长做:因为并不知道要循环多少次。这样可以循环到直到结束时才停止。
#include<iostream> #include <vector> using namespace std; vector<int> vInt; void setNext(const vector<int>& v, int& nNowIndex){ //cout<<nNowIndex; if(nNowIndex == v.size() - 1){ nNowIndex = 0; }else{ nNowIndex++; } if(v[nNowIndex] == 0){ setNext(v, nNowIndex); } //cout<<"的下一个索引是:"<<nNowIndex<<endl; } void setZeroByNowIndex(vector<int>& v, int& nNowIndex, const int m){ int i = 0; while(i < m){ setNext(v, nNowIndex); i++; } //cout<<"删除:"<<v[nNowIndex]<<endl; v[nNowIndex] = 0; setNext(v, nNowIndex); //cout<<"下个索引从"<<vInt[nNowIndex]<<"开始"<<endl; } void main(){ int n, m; while(1){ vInt.clear(); cout<<"输入n,m: "; cin>>n>>m; for(int i = 0; i < n; i++){ vInt.push_back(i + 1); } int nNowIndex = 0; for(int j = 0; j < n - 1; j++){ setZeroByNowIndex(vInt, nNowIndex, m-1); //循环是从0开始作为第一个的 } for(int k = 0; k < n; k++){ if(vInt[k] != 0){ cout<<"最后剩余的数字是: "<<vInt[k]<<endl<<endl; break; } } } } /* 输入n,m: 9 5 最后剩余的数字是: 8 输入n,m: 6 4 最后剩余的数字是: 5 */
总结:
1,查找下一个元素并没有在while中处理0,而是在setNext中处理的
2,while循环中,数组是从0开始的,因此将m减去1,才是要跳过m个数字,主要因为第m哥其实是仅仅跳过m-1个数字
这样就会让while循环执行m次。其实这样一分析就是变形的for循环啊
<2>循环m次,while循环的写法(注意和for循环对比)总结:
外部变量是0的写法
while主体就是
int i = 0; while(i < 5){ stagement; i++; }外部变量不是0
int i = 5 while(i > 0){ stagement; i--; }
<3>while 和 for循环对比总结:
int neighborCol = sp->getCol() - 1;
while (neighborCol >= 0) {
AnimationSprite *neighborSp = m_animalMatrix[sp->getRow() * m_width + neighborCol];
if (neighborSp && neighborSp->getIgnoreCheck()) { //如果此链中有标记为setIgnoreCheck(true)的特殊元素则不检测
chainList.clear();
return;
}
if (neighborSp && (neighborSp->getImgIndex() == sp->getImgIndex()) && !neighborSp->getIgnoreCheck()) {
CloudSprite *cloudSp = m_cloudMatrix[neighborSp->getRow() * m_width + neighborSp->getCol()];
if (cloudSp) {
return;
}
m_checkSpL = true;
chainList.push_back(neighborSp);
neighborCol--;
}
else {
break;
}
}比如这个向左检查所有可以连接的串,显然while循环更擅长做:因为并不知道要循环多少次。这样可以循环到直到结束时才停止。
相关文章推荐
- 比较R语言、perl语言,matlab中for循环和while循环的使用
- 使用for循环、while循环和递归写出3个函数来计算给定数列的总和
- Python中for循环和while循环的基本使用方法
- c语言:分别使用for循环和while循环求5!
- c#入门之循环语句使用详解(for循环、do/while)
- SQL Server 中没有For循环,可以使用while循环代替
- c语言:分别使用for循环和while循环求5!
- 使用for循环遍历文件、使用while循环遍历文件
- Python中for循环和while循环的基本使用方法
- 使用for循环代替while循环的场合
- SQL Server 中没有For循环,可以使用while循环代替 .
- 比较R语言、perl语言,matlab中for循环和while循环的使用
- Java 三种循环的流程图画法总结(for,while,do-while循环)
- 使用SQL中的ROW_NUMBER()和while循环对每一行执行操作
- 对while循环使用时需要注意到的小细节
- struts2 counter循环标签的使用 实现for循环
- PHP快速入门教程:WHILE循环的使用示例
- continue 在 switch中使用 (switch嵌套在while循环中)
- MySQL与MSSQl使用While语句循环生成测试数据的代码
- linux shell编程中while循环使用