注册 登录
编程论坛 C++教室

猴子分桃问题

呜呜1 发布于 2013-09-22 15:17, 677 次点击
问题描述:甲、乙、丙3个猴子带着21个篮子去摘桃子。回来以后,发现有7个篮子装满了桃子,还有7个篮子装了半篮桃子,另外7个篮子是空的。假设7个满篮中桃子的重量都相同为a千克,7个半篮中桃子的重量也相同为b千克。在不将桃子倒出的前提下,如何将桃子平分成3份?(注释中写清分析过程)#include<iostream>
using  namespace  std;
int  main()
{
    int a[3][7]={{6,6,6,6,6,6,6},{3,3,3,3,3,3,3},{0,0,0,0,0,0,0}};
    int i,j;
    cout<<"array a:"<<endl;
   
    for(i=0;i<3;i++)
        for(j=0;j<7;j++)
            cout<<a[i][j]<<" ";
        for(j=0;j<7;j++)
        {
        for(;a[0][j]>a[2][j];)
            {
                   a[0][j]--;
                 a[2][j]++;
            }
        }
        
         
          for(i=0;i<3;i++)
              for(j=0;j<7;j++)
             cout <<" a[i][j]"<<" ";
     
              return 0;
}#include<iostream>
using  namespace  std;
int  main()
{
    int a[3][7]={{6,6,6,6,6,6,6},{3,3,3,3,3,3,3},{0,0,0,0,0,0,0}};
    int i,j;
    cout<<"array a:"<<endl;
   
    for(i=0;i<3;i++)
        for(j=0;j<7;j++)
            cout<<a[i][j]<<" ";
        for(j=0;j<7;j++)
        {
        for(;a[0][j]>a[2][j];)
            {
                   a[0][j]--;
                 a[2][j]++;
            }
        }
        
         
          for(i=0;i<3;i++)
              for(j=0;j<7;j++)
             cout <<" a[i][j]"<<" ";
     
              return 0;
}
运行结果只显示原数组,即a[3][7]={{6,6,6,6,6,6,6},{3,3,3,3,3,3,3},{0,0,0,0,0,0,0}};,为什么啊?我怎样把我的运行结果框发上来呢


6 回复
#2
3037709572013-09-22 17:11
这样试试:
#include<iostream>
using  namespace  std;
int  main()
{
    int a[3][7]=
    {
        {6,6,6,6,6,6,6},
        {3,3,3,3,3,3,3},
        {0,0,0,0,0,0,0}
    };
    int i,j;

    cout<<"array a:"<<endl;

    for(i=0; i<3; i++)
        for(j=0; j<7; j++)
            cout<<a[i][j]<<" ";

    for(j=0; j<7; j++)
    {
        for(; a[0][j]>a[2][j];)
        {
            a[0][j]--;
            a[2][j]++;
        }
    }

    cout<<endl<<"array a to b:"<<endl;
    for(i=0; i<3; i++)
        for(j=0; j<7; j++)
            cout <<a[i][j]<<" ";

    return 0;
}
运行结果:
只有本站会员才能查看附件,请 登录



[ 本帖最后由 303770957 于 2013-9-22 17:15 编辑 ]
#3
sky流光2013-09-22 21:47
回复 2楼 303770957
for(; a[0][j]>a[2][j];)
前面的;是什么意思
#4
blueskiner2013-09-22 22:12
for(; a[0][j]>a[2][j];)

改成while (a[0][j]>a[2][j]) 能看懂吗?
#5
呜呜12013-09-23 20:59
回复 2楼 303770957
你修改的程序只是增加了cout<<endl<<"array a to b:"<<endl;,而且你没有定义b,还可以运行出来???为啥我的原程序不能运行啊???
   请教一下,怎样把运行结果复制粘贴啊??
#6
qunxingw2013-09-23 22:00
每个桃子重量没有说是多少?应该用交换妥当点
#7
尹金魁20312013-09-24 22:15
1