ACM之路训练赛第一场

ACM之路训练赛第一场

Question A

Description
With given integers a,b,c, you are asked to judge whether
the following statement is true: "For any x, if a⋅+b⋅x+c=0, 
then x is an integer."

Input
The first line contains only one integer T(1≤T≤2000), 
which indicates the number of test cases.
For each test case, there is only one line containing 
three integers a,b,c(−5≤a,b,c≤5).

Output
or each test case, output “YES” if the statement is true, 
or “NO” if not.

Sample
Input

3
1 4 4
0 0 1
1 3 1
Output

YES
YES
NO

Train of thought

条件判断入门题
会if()就会做

代码

wrong answer
i don`t known why

#include<stdio.h>
#include<math.h>
int main()
{
    int n;
    int a,b,c;
    int av;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&a,&b,&c);

        if(a==0){
            if(b==0){
                printf("YES\n");
            }
            else if(c%b==0){
                    printf("YES\n");
            }
            else
            printf("NO\n");
        }
        //涉及到sqrt()函数为小数 
        //()
        else if(a!=0){
            av=b*b-4*a*c;

            if(av>=0){
                double kg=sqrt(av);
                if((int)kg!=kg)printf("NO\n");

                else if(((int)(-b+kg)%(2*a))==0&&((int)(-
                b-kg)%(2*a))==0)
                    printf("YES\n");

                }   
            else 
                printf("NO\n");
        }
    }
        return 0;
 } 
AC代码
#include<stdio.h>
#include<math.h>
int main()
{
    int n;
    int a,b,c;
    int av;
    scanf("%d",&n);

    for(int i=1;i<=n;i++){
        scanf("%d%d%d",&a,&b,&c);
        if(a==0 && b==0 )  {  
            if(c==0){  
                puts("NO");  
                continue;  
            }  
            else{  
                puts("YES");  
                continue;  
            }  
        }  

        else if(a==0 && b!=0){  
            if(c%b !=0){  
                puts("NO");  
                continue;  
            }  
            else{  
                puts("YES");  
                continue;  
            }  
        }  

        else if(a!=0){  
            int av=b*b-4*a*c;  
            if(av<0){  
                puts("YES");  
                continue;  
            }  
          else  
            {  
                double kg=sqrt(av);  
                if(((-b+kg)/(2*a)) - (int)((-b+kg)/(2*a))
                ==0 && ((-b-kg)/(2*a))- (int)((-b-kg)/
                (2*a))==0 )  
                {  
                    puts("YES");  
                    continue;  
                }  
                else  
                {  
                    puts("NO");  
                    continue;  
                }  
            }  
    }

 } 
    return 0;
 }

好烦,真不知道是怎么个回事,明明很简单的一道题,却总是出错。
果然是一做题,就hold不住自己,血气翻腾啊。总是有点急迫

B

Description
Calculate  mod (1000000000+7) for given n,m.

Input
Input contains two integers n,m(1≤n≤1000,0≤m≤10).

Output
Output the answer in a single line.

Sample
Input

10 0
Output

10

代码

#include<stdio.h>
long long f(int x,int n)
{
    long long sum=1;
    for(int i=1;i<=n;i++)
    {
        sum=(sum*x);
        sum=sum%(1000000000+7);
    }
    return sum; 
}int main(){

    int n,m;
    long long sum=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        sum+=f(i,m);
        sum=sum%(1000000000+7);
    }
    printf("%lld\n",sum);
    return 0;
} 

C

Description
Fascinated with the computer games, Gabriel even forgets to study. Now she needs to finish her homework, and there is an easy problem:

f(n)=

She is required to calculate f(n) mod 2 for each given n. Can you help her?

Input
Multiple test cases. Each test case is an integer n(0≤n≤) in a single line.

Output
For each test case, output the answer of f(n)mod2.

Sample
Input

2
Output

1

代码

#include<stdio.h>
#include<string.h>
int main(){
    char da[1010];
    while(scanf("%s",da)!=EOF){

    int sum=0;
    int len=strlen(da); 
    for(int i=0;i<len;i++){  
        sum=(sum*10+(da[i]-'0'))%3;  
        }

    if(sum==0)  
        printf("0\n");
    else if(sum==1||sum==2) 
        printf("1\n");
    }
    return 0;
}

D

There are n kinds of goods in the company, with each of them has a inventory of  and direct unit benefit . Now you find due to price changes, for any goods sold on day i, if its direct benefit is val, the total benefit would be i⋅val.
Beginning from the first day, you can and must sell only one good per day until you can't or don't want to do so. If you are allowed to leave some goods unsold, what's the max total benefit you can get in the end?

Input
The first line contains an integers n(1≤n≤1000).
The second line contains n integers val1,val2,..,valn(−100≤.≤100).
The third line contains n integers cnt1,cnt2,..,cntn(1≤≤100).

Output
Output an integer in a single line, indicating the max total benefit.

Sample
Input

4
-1 -100 5 6
1 1 1 2
Output

51
Hint
sell goods whose price with order as -1, 5, 6, 6, the total benefit would be -1*1 + 5*2 + 6*3 + 6*4 = 51.

Train of thought

代码

#include<bits/stdc++.h>
using namespace std;
#define maxn 100000
int a[maxn],sum[maxn],d[maxn];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int top=n;
        for(int i=0; i<n; i++)
            scanf("%d",a+i);
        for(int i=0; i<n; i++)
        {
            scanf("%d",d+i);
            for(int j=1; j<d[i]; j++)
                a[top++]=a[i];
        }
        sort(a,a+top);

        sum[0]=a[0];
        long long ans=a[0];
        for(int i=1; i<top; i++)
        {
            sum[i]=sum[i-1]+a[i];
            ans+=(i+1)*a[i];
        }
        for(int i=0; i<top; i++)
        {
            long long  s=ans-a[i]-sum[top-1]+sum[i];
            if(s>ans)
                ans=s;
            else break;
        }
        printf("%lld",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yishengchizha/article/details/80288482