QR13. 日本旅行
描述
限制条件
0≤ C1,C5,C10,C50,C100,C500≤1000000000
0≤A≤1000000000
依次输入C1,C5,C10,C50,C100,C500和A,以空格分隔,输出最少所需硬币数,如果该金额不能由所给硬币凑出,则返回NOWAY
输入描述
依次输入C1,C5,C10,C50,C100,C500和A,以空格分隔输出描述
输出最少所需硬币数,如果该金额不能由所给硬币凑出,则返回NOWAY示例1
输入:
3 2 1 3 0 2 620
输出:
6
C 解法, 执行用时: 2ms, 内存消耗: 304KB, 提交时间: 2021-08-29
#include<stdio.h> int main() { long int a[2][7]; long int i,num=0; for(i=0;i<7;i++) { scanf("%ld ",&a[0][i]); } a[1][0]=1,a[1][1]=5,a[1][2]=10,a[1][3]=50,a[1][4]=100,a[1][5]=500,a[1][6]=a[0][6]; for(i=5;i>=0;i--) { if(a[0][i]*a[1][i]>=a[0][6]) { num=num+a[0][6]/a[1][i]; a[0][6]=a[0][6]%a[1][i]; } else { a[0][6]=a[0][6]-a[0][i]*a[1][i]; num=num+a[0][i]; } if(a[0][6]==0) break; } if(a[0][6]==0) printf("%ld\n",num); else printf("NOWAY"); }
C 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2019-04-01
#include<stdio.h> int main() { int c1,c5,c10,c50,c100,c500,a,b,c,d,e,f,g,zz; scanf("%d %d %d %d %d %d %d",&c1,&c5,&c10,&c50,&c100,&c500,&a); b=a/500; a=a-500*b; c=a/100; a=a-100*c; d=a/50; a=a-50*d; e=a/10; a=a-10*e; f=a/5; a=a-5*f; g=a/1; if((g<=c1)&&(f<=c5)&&(e<=c10)&&(d<=c50)&&(c<=c100)&&(b<=c500)) {zz=g+f+e+d+c+b;printf("%d",zz);} else printf("NOWAY"); }
C 解法, 执行用时: 2ms, 内存消耗: 368KB, 提交时间: 2021-10-31
#include <stdio.h> #include <stdlib.h> int main() { int A,C1,C2,C3,C4,C5,C6,a,b,c,d,e,f; int sum; scanf("%d %d %d %d %d %d %d",&C1,&C2,&C3,&C4,&C5,&C6,&A); e = A/500; A = A%500; d = A/100; A = A%100; c = A/50; A = A%50; b = A/10; A = A%10; a = A/5; A = A%5; f = A; if((f <= C1)&&(a <= C2)&&(b <= C3)&&(c <= C4)&&(d <= C5)&&(e <= C6)) { sum = a+b+c+d+e+f; printf("%d\n",sum); } else printf("NOWAY"); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 368KB, 提交时间: 2020-09-27
#include <stdio.h> int i=6; int a[6]={0},b[6]={1, 5, 10, 50, 100, 500}; int sum=0; int fun(int x) { i -= 1; if(x%b[i]==0 && x/b[i]<=a[i]) return sum+x/b[i]; else if(x%b[i]==0 && x/b[i]>a[i]) return -1; else if(x/b[i]<=a[i]) { sum += x/b[i]; x = x-x/b[i]*b[i]; return fun(x); } else { sum += a[i]; x = x-x/b[i]*a[i]; return fun(x); } } int main() { int A; scanf("%d%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5]); scanf("%d", &A); int n; n = fun(A); if(n == -1) printf("NOWAY\n"); else printf("%d\n", n); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 368KB, 提交时间: 2019-04-10
#include <stdio.h> #include <stdlib.h> int main() { int A,C1,C2,C3,C4,C5,C6,a,b,c,d,e,f; int sum; scanf("%d %d %d %d %d %d %d",&C1,&C2,&C3,&C4,&C5,&C6,&A); e = A/500; A = A%500; d = A/100; A = A%100; c = A/50; A = A%50; b = A/10; A = A%10; a = A/5; A = A%5; f = A; if((f <= C1)&&(a <= C2)&&(b <= C3)&&(c <= C4)&&(d <= C5)&&(e <= C6)) { sum = a+b+c+d+e+f; printf("%d\n",sum); } else printf("NOWAY"); return 0; }