NOIP201401 abacus and mental arithmetic test solution to a problem

This question may seem simple, but there are still pits!

This is my best start of the code:

 

#include<bits/stdc++.h>
using namespace std;
int n;
int a[101];
int flag[101]={true};
int i,j;
int k;
int ans=0;
inline void Read()
{
    cin>>n;
    for(i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
}
inline void solve()
{
    for(i=0;i<n;i++)
    {
        for(j=i;j>=0;j--)
        {
            for(k=j;k>=0;k--)
            {
                if(k==j) continue;
                else if(a[j]+a[k]==a[i])
                {
                    ans++;
                }
            }
        }
    }
    cout<<ans<<endl; 
}
int main()
{
    Read();
    solve();
}

 

But only 30 points, WA is heard crying out.

So what is it?

Reading a good question!

How many of a number exactly equal to the set the other two (different) and the number of?

The above code is the number of request methods, such as 5,2 + 6 + 4 = 1, the two calculations.

The correct code is as follows:

#include<iostream>
#include<algorithm>
using namespace std;
int main( )
{
	int n,ans=0;
	int i,j,k,flag=0;
	int a[1010];
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n);
	for(i=n-1;i>=0;i--)
	{
		for(j=0;j<i;j++)
		{
			for(k=j+1;k<i;k++)
			{
				if(a[i]==a[j]+a[k])
				{
					ans++;
					break;
				}
			}
			if(a[i]==a[j]+a[k])
			{
				break;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

End! ! !

 

Guess you like

Origin www.cnblogs.com/wangshiyao/p/11226788.html