解题思路:
1.要为所有员工工资准备的钱的张数最少,则发给每个员工的工资里钱的张数要最少;
2.要发给每个员工的工资里钱的张数要最少,则要先发面值最大的钱,再发次大的,再发次次大的;
3.假设某员工薪水为121;(则有以下递归关系)
(1):薪水大于100,先发给面值为100的钱,所发张数为:121/100=1张......21元;(还需发21元)
(2):21>大于10,小于50,故发面值为10的钱,所发张数为:21/10=2张......1元;(还需发1元)
(3):1等于1,故发面值为1的钱,所发张数为:1/1=1张......0元;
4.1张+2张+1张=4张 :即该员工所需最少的钱的张数为4张;
6.求出每个员工所需张数再加起来,就是总的所需最少张数;
参考代码:
#include <stdio.h> void fun_ction( int salary, int *Count ); int main() { int num_ofpeople; int salary; int Count; while ( scanf( "%d", &num_ofpeople ) != EOF && num_ofpeople != 0 ) { //因为之前有题目以0结束,但是提交输出超限,加上EOF正确,所以这里也加了一个以文件结束符结束避免出错 Count = 0; for ( int i = 0; i < num_ofpeople; i++ ) { scanf( "%d", &salary ); fun_ction( salary, &Count ); } printf( "%d\n", Count ); } return(0); } /*---------------------------------------------------------------*/ void fun_ction( int salary, int *Count ) { if ( salary == 0 )//发完返回 return; if ( salary == 1 ) {//直接加1,并返回,不用再递归到:fun_ction(salary%1,Count); (*Count)++; return; }else if ( salary >= 2 && salary < 5 ) { (*Count) += (salary / 2); fun_ction( salary % 2, Count ); }else if ( salary >= 5 && salary < 10 ) { (*Count) += (salary / 5); fun_ction( salary % 5, Count ); }else if ( salary >= 10 && salary < 50 ) { (*Count) += (salary / 10); fun_ction( salary % 10, Count ); }else if ( salary >= 50 && salary < 100 ) { (*Count) += (salary / 50); fun_ction( salary % 50, Count ); }else if ( salary >= 100 ) { (*Count) += (salary / 100); fun_ction( salary % 100, Count ); } return; }
别忘点赞哦-.-
非递归
(在使用非递归方法的时候,每个if条件之间不能用else连接,因为上一个if中salary改变后的值,要作为后面的判断条件,加了else的话,执行一个后,其后面的就都不执行了。)
#include <stdio.h> void fun_ction( int salary, int *Count ); int main() { int num_ofpeople; int salary; int Count; while ( scanf( "%d", &num_ofpeople ) != EOF && num_ofpeople != 0 ) { Count = 0; for ( int i = 0; i < num_ofpeople; i++ ) { scanf( "%d", &salary ); fun_ction( salary, &Count ); } printf( "%d\n", Count ); } return(0); } /*---------------------------------------------------------------*/ void fun_ction( int salary, int *Count ) { if ( salary >= 100 ) { (*Count) += (salary / 100); salary%=100; } if ( salary >= 50 && salary < 100 ) { (*Count) += (salary / 50); salary%=50; } if ( salary >= 10 && salary < 50 ) { (*Count) += (salary / 10); salary%=10; } if ( salary >= 5 && salary < 10 ) { (*Count) += (salary / 5); salary%=5; } if ( salary >= 2 && salary < 5 ) { (*Count) += (salary / 2); salary%=2; } if ( salary == 1 ) { (*Count)++; } return; }
别忘点赞哦-.-
0.0分
19 人评分
/*发工资 贪心算法 */ #include <stdio.h> int main() { int num,a,sum=0; scanf("%d",&num); int b[6]={100,50,10,5,2,1}; int i,j; while(num!=0) { for(j=0;j<num;j++) { scanf("%d",&a); for(i=0;i<6;i++) { sum=sum+a/b[i]; a=a-(a/b[i])*b[i]; } } printf("%d\n",sum); sum=0; scanf("%d",&num); } return 0; }
其实贪心算法可以直接反复除取余就可以了 #include <stdio.h> int main(){ int n,salary; int x; int i; while(scanf("%d",&n)!=EOF){ x=0; if(n==0) break; for(i=0;i<n;i++){ scanf("%d",&salary); x+=salary/100; salary%=100; x+=salary/50; salary%=50; x+=salary/10; salary%=10; x+=salary/5; salary%=5; x+=salary/2; salary%=2; x+=salary/1; } printf("%d\n",x); } return 0; }
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:632 |
C语言程序设计教程(第三版)课后习题7.1 (C语言代码)浏览:539 |
C语言程序设计教程(第三版)课后习题8.6 (C语言代码)浏览:631 |
1908题解浏览:680 |
计算质因子 (C语言代码)浏览:778 |
链表数据求和操作 (C语言代码)浏览:1035 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:532 |
找出最长的字符串来 (C语言代码)浏览:1840 |
printf基础练习 (C语言代码)浏览:2268 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:669 |