把{1,2,3,4,5,6,7,8,9}这九个数每三个一组,组成三个三位数a,b,c。要求三个三位数满足a+b=c c++算法
把{1,2,3,4,5,6,7,8,9}这九个数每三个一组,组成三个三位数a,b,c。要求三个三位数满足a+b=c,用c++列出所有的可能结果。跪求啊,没财富值了,我会采...
把{1,2,3,4,5,6,7,8,9}这九个数每三个一组,组成三个三位数a,b,c。要求三个三位数满足a+b=c,用c++列出所有的可能结果。跪求啊,没财富值了,我会采纳的。谢谢大神了
展开
展开全部
函数声明什么的我就不写了
int a [9] = {1,2,3,4,5,6,7,8,9};
vector<int> vRestult;//存放数据的向量
for (int i = 0; i < 6; i++)
{
int nResult;
int x = a[i];
int y = a[i+1];
int z = a[i+2];
if( x == (z +y) || y == (x + z) || z == (x + y))//判断条件,满足a=b+c,列出6种排列组合放入向量
{
nResult = x * 100 + y * 10 + z;
vRestult.push_back(nResult);
nResult = x * 100 + z * 10 + y;
vRestult.push_back(nResult);
nResult = y * 100 + x * 10 + z;
vRestult.push_back(nResult);
nResult = y * 100 + z * 10 + x;
vRestult.push_back(nResult);
nResult = z * 100 + x * 10 + y;
vRestult.push_back(nResult);
nResult = z * 100 + y * 10 + z;
vRestult.push_back(nResult);
}
}
int a [9] = {1,2,3,4,5,6,7,8,9};
vector<int> vRestult;//存放数据的向量
for (int i = 0; i < 6; i++)
{
int nResult;
int x = a[i];
int y = a[i+1];
int z = a[i+2];
if( x == (z +y) || y == (x + z) || z == (x + y))//判断条件,满足a=b+c,列出6种排列组合放入向量
{
nResult = x * 100 + y * 10 + z;
vRestult.push_back(nResult);
nResult = x * 100 + z * 10 + y;
vRestult.push_back(nResult);
nResult = y * 100 + x * 10 + z;
vRestult.push_back(nResult);
nResult = y * 100 + z * 10 + x;
vRestult.push_back(nResult);
nResult = z * 100 + x * 10 + y;
vRestult.push_back(nResult);
nResult = z * 100 + y * 10 + z;
vRestult.push_back(nResult);
}
}
追问
怎么输出 啊,麻烦大神写清楚哦,我要交作业
追答
不好意思 周末没上网 遍历下vector就行了啊
2014-12-19
展开全部
981 624 357
981 627 354
981 654 327
981 657 324
981 735 246
981 736 245
981 745 236
981 746 235
总计 168 种
int fun(int n){
int i = 0, a[10] = { 0 };
while (n)
{
a[n % 10] += 1;
n = n / 10;
}
for (i = 0; i <= 9; i++)
{
if (a[i]>1||a[0]>0)return 0;
}
return 1;
}
void main(){
int m = 0;
FILE *fp;
fp = fopen("d:\\1.txt", "w");//d盘文件1.txt
if (fp == NULL) return ;
for (int i = 412; i < 987; i++)
{
if (fun(i) == 0)continue; {
for (int j = 100; j <900; j++){
if (fun(i * 1000 + j) == 0)continue;
for (int k = 100; k < 900; k++){
if (fun(i * 1000000 + j * 1000 + k) == 0|| j<k)continue;
if (i == j + k)
{
fprintf(fp, "%d %d %d\n", i, j, k); m++; printf( "%d %d %d\n", i, j, k);
}
}
}
}
}
fprintf(fp, "总计 %d 种\n", m);
fclose(fp);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询