2015-10-13 23:58

# 怎么用递归解决ACM的发工资问题，求各位大神帮助

Sample Input
3
1 2 3
0

Sample Output
4

#include

using namespace std;
int salary(int a)
{
static int sum=0;
if(a==0)
{
cout< return sum;
}
if(a>=100)
{
sum+=a/100;
a=a%100;
return salary(a);
}
else if(a>=50)
{
sum+=a/50;
a=a%50;
return salary(a);
}
else if(a>=10)
{
sum+=a/10;
a=a%10;
return salary(a);

}
else if(a>=5)
{
sum+=a/5;
a=a%5;
return salary(a);
}
else if(a>=2)
{
sum+=a/2;
a=a%2;
return salary(a);
}
else
{
sum+=a;
cout<<sum<<endl;
return sum;
}

}
int main()
{
int m;
while(cin>>m)

{
if(m==0)
continue;
int sum=0;
int a[m];
for(int i=0; i<m; i++)
cin>>a[m];
for(int i=0; i<m; i++)
{
salary(a[i]);
}

}

}

• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答

#### 2条回答默认 最新

• lx624909677 2015-10-14 02:28
已采纳
#include <iostream>
using namespace std;
int main()
{
int n;
while (cin >> n && n != 0)
{
int m[100]; // 开100

for (int i = 0; i < n; i++)
{
cin >> m[i];
}
int sum [100]; // 开100
int sar = 0;
for (int j = 0; j < n; j++)
{
int a = 0,b = 0,c = 0,d = 0,e = 0,f = 0;
a = m[j]/100;
b = (m[j] - a*100)/50;
c = (m[j] - a*100 - b*50)/10;
d = (m[j] - a*100 - b*50 - c*10)/5;
e = (m[j] - a*100 - b*50 - c*10 - d*5)/2;
f = (m[j] - a*100 - b*50 - c*10 - d*5 - 2*e);
sum[j] = a + b + c + d + e + f;
sar += sum[j];
}
cout << sar << endl;
}
return 0;
}

点赞 评论
• WorldMobile 2015-10-14 00:51

sum+=a/100;
a=a%100;
return salary(a);

目测你的算法，只能得出多少张，没有计算出各种钞票多少张

点赞 评论