The Balance

The Balance

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6235    Accepted Submission(s): 2564


Problem Description
Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of all the weights.
 

Input
The input consists of multiple test cases, and each case begins with a single positive integer N (1<=N<=100) on a line by itself indicating the number of weights you have. Followed by N integers Ai (1<=i<=N), indicating the quality of each weight where 1<=Ai<=100.
 

Output
For each input set, you should first print a line specifying the number of qualities which cannot be measured. Then print another line which consists all the irrealizable qualities if the number is not zero.
 

Sample Input
3 1 2 4 3 9 2 1
 

Sample Output
0 2 4 5
 
 母函数,但是这一点要讲一下!不同之处,这里又多了一个减法

(除法),所以无从下手,一开始用深搜,数据太大,之间崩溃

了!
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n,sum,b[100005],a[1005],j,flag,sum1,c[100005];
bool visit[1005];
int compare(int a,int b)
{
    return a>b;
}
void dfs(int pos)
{
    int i;
    for(i=pos;i<n;i++)
    {
        sum1+=a[i];
        if(sum1==a[0])
        {
            flag++;
        }
        if(flag==2)
        {
            return;
        }
        b[j++]=sum1;
        visit[i]=1;
        dfs(i+1);
        sum1-=a[i];
        visit[i]=0;
    }
}
int main()
{
    int i,k,x,y,flag2,sum2,sum3;
    while(cin>>n)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        memset(visit,0,sizeof(visit));
        x=flag=j=sum1=sum=0;
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            sum+=a[i];
        }
        sort(a,a+n,compare);
        dfs(0);
      /*  for(i=0;i<n;i++)
        {
            sum2=0;
            for(k=i+1;k<n;k++)
            {
                sum3=a[k];
                sum2+=sum3;
                if(sum2<a[i])
                {
                    b[j++]=a[i]-sum2;
                }
                if(sum3<a[i]&&sum3!=sum2)
                {
                    b[j++]=a[i]-sum3;
                }
            }
        }*/
        for(i=1;i<=sum;i++)
        {
            y=0;
            for(k=0;k<j;k++)
            {
                if(i==b[k])
                {
                    y=1;
                    break;
                }
            }
            if(y==0)
            {
                c[x++]=i;
            }
        }
        if(x==0)
        {
            cout<<0<<endl;
        }
        else
        {
            cout<<x<<endl;
            for(i=0;i<x-1;i++)
            {
                cout<<c[i]<<" ";
            }
            cout<<c[x-1]<<endl;
        }
    }
}
看到网上的代码,感觉写的很好!也深深地体会到了母函数
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a[105],b[10005],c[10005],ans[10005];
int main()
{
    int i,j,f,n,k,sum;
    while(cin>>n)
    {
        f=sum=0;
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
            sum+=a[i];
        }
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        b[0]=1;b[a[1]]=1;//初始化
        for(i=2;i<=n;i++)
        {
            for(j=0;j<=sum;j++)
            {
               for(k=0;k+j<=sum&&k<=a[i];k+=a[i])//这里之所以限制,所以设置k<=a[i]实际上是为了避免多次加a[i]
               {
                   c[k+j]+=b[j];
                   c[abs(j-k)]+=b[j];
               }
            }
            for(j=0;j<=sum;j++)
            {
                b[j]=c[j];
                c[j]=0;
            }
        }
        for(i=1;i<=sum;i++)
        {
            if(!b[i])
            {
                ans[f++]=i;
            }
        }
        if(f==0)
        {
            cout<<0<<endl;
        }
        else
        {
            cout<<f<<endl;
            for(i=0;i<f-1;i++)
            {
                cout<<ans[i]<<" ";
            }
            cout<<ans[f-1]<<endl;
        }
    }
}


Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e., debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction (i.e., credit or debit). Create an inheritance hierarchy containing base class Account and derived classes SavingsAccount and CheckingAccount that inherit from class Account. Base class Account should include one data member of type double to represent the account balance. The class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0.0. If not, the balance should be set to 0.0 and the constructor should display an error message, indicating that the initial balance was invalid. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account and ensure that the debit amount does not exceed the Account’s balance. If it does, the balance should be left unchanged and the function should print the message "Debit amount exceeded account balance." Member function getBalance should return the current balance. Derived class SavingsAccount should inherit the functionality of an Account, but also include a data member of type double indicating the interest rate (percentage) assigned to the Account. SavingsAccount’s constructor should receive the initial balance, as well as an initial value for the SavingsAccount’s interest rate. SavingsAccount should provide a public member function calculateInterest that returns a double indicating the amount of interest earned by an account. Member function calculateInterest should determine this amount by multiplying the interest rate by the account balance. [Note: SavingsAccount should inherit member functions credit and debit as is without redefining them.] Derived class CheckingAccount should inherit from base class Account and include an additional data member of type double that represents the fee charged per transaction. CheckingAccount’s constructor should receive the initial balance, as well as a parameter indicating a fee amount. Class CheckingAccount should redefine member functions credit and debit so that they subtract the fee from the account balance whenever either transaction is performed successfully. CheckingAccount’s versions of these functions should invoke the base-class Account version to perform the updates to an account balance. CheckingAccount’s debit function should charge a fee only if money is actually withdrawn (i.e., the debit amount does not exceed the account balance). [Hint: Define Account’s debit function so that it returns a bool indicating whether money was withdrawn. Then use the return value to determine whether a fee should be charged.] After defining the classes in this hierarchy, write a program that creates objects of each class and tests their member functions. Add interest to the SavingsAccount object by first invoking its calculateInterest function, then passing the returned interest amount to the object’s credit function.
最新发布
05-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值