在使用嵌套循环中,一定要注意的:是否有变量的初始值发生了改变,再简单的嵌套只要是没有在内循环中设定初始值,则就有可能出错!
练习的程序:
//求满足下面条件的三位数n,它除以11所得到的商等于n的个位数字的平方和,且其中至少有两位数字相同。
#include<iostream.h>
//using namespace std;
int main()
{
int n;
int i;
int a[4];//存放各个位数
int j=1;
int s=0;//记录各位平方和
int c;//记录除以11的结果
int r;
for(i=101;i<=999;i++)
{//求出整数的各个位数并保存在数组a中
n=i;
//保证数不变
c=i/11;
s=0;//每次循环各位平方和总是要使和为0
while(n!=0)
{j=1;//每次循环,保存个个位数的数组都要从头开始
r=n%10;
a[j]=r;
s+=a[j]*a[j];
n=n/10;
j++;
}
//judge the int
if(c==s)
cout<<"the number of "<<i<<" is right number"<<endl;
//else cout<<"the number is wrong"<<endl;
}
return 0;}