160A題目網址
題目解析
1.輸入硬幣的個數,分配硬幣,使拿最小的硬幣數比剩下的硬幣金額大
舉例:
輸入:
2
3 3
輸出
2
2.注意點:
1)接收整型數組時要使用&,因為只有字符數組是使用指針傳遞首地址的
scanf("%d",&a[i]);
2)使用冒泡排序,將數組從大到小排序
for(int j=0;j<n-1;j++)
for(int k=0;k<n-1-j;k++)
3)使用count_all去計算所有的硬幣金額,使用count_now去計算現在拿的硬幣金額,再將它與剩下的硬幣金額比較
代碼
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main()
{int n=0,count_all=0,count_now=0,count=0;scanf("%d",&n);int a[100]={0};for(int i=0;i<n;i++){//字符串才可以 char a[i],字符數組才使用了指針scanf("%d",&a[i]);//&count_all+=a[i];}for(int j=0;j<n-1;j++){for(int k=0;k<n-1-j;k++){if(a[k]<a[k+1]){int temp=0;temp=a[k];a[k]=a[k+1];a[k+1]=temp;}}}for(int m=0;m<n;m++){count_now+=a[m];count++;if(count_now>(count_all-count_now)){break;}}printf("%d",count);getchar();system("pause");return 0;
}