假設老師的工資都是正整數,單位元,人民幣一共有100元、50元、10元、5元、2元和1元六種。
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]);
}
}
}
這是我自己寫的遞歸代碼,但關於函數裡遞歸變量 我沒有搞清楚。。請大神指正我的錯誤,謝謝了
#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;
}