问题及代码:
/*
*Copyright (c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作 者:王艺霖
*完成日期:2016年3月16日
*版 本 号:v1.0
*
*问题描述:猜数字
*
*输入描述:无输入
*输出描述:输出所有可能结果
*/
#include <iostream>
using namespace std;
int main()
{
int dou,yao,xue,c;
cout <<"都"<<"要"<<"学"<<"C"<<endl;
for(dou = 0; dou <= 9; ++dou)
{
for(yao = 0; yao <= 9; ++yao)
{
for(xue = 0; xue <= 9; ++xue)
{
for(c = 0; c <= 9; ++c)
{
if((dou*1000+yao*100+xue*10+c) + (yao*100+xue*10+c) + (xue*10+c) + c == 2008)
cout << dou <<" "<<yao<<" "<<xue<<" "<<c<<endl;
}
}
}
}
return 0;
}
运行结果:
知识点总结:
穷举法