假期编程

此博客链接:https://www.cnblogs.com/ping2yingshi/p/12288877.html

1.A/B?(4min)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2075

Problem Description
正整数A是否能被正整数B整除,不知道为什么xhd会研究这个问题,来帮帮他吧。
Input
输入数据的第一行是一个数据T,表示有T组数据。
每组数据有两个正整数A和B(A,B<10^9)。
Output
对于每组输入数据,输出"YES"表示可以被整除,"NO"表示不能被整除。
Sample Input
2
4 2
5 3
Sample Output
YES
NO
题解:
        方法:整除。
        思想:利用两个整数相除,余数为零做这道题。
代码如下:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
    int n; 
    scanf("%d",&n); 
    while(n>0)
    { 
        int A;
        int B; 
        scanf("%d %d",&A,&B);
        if(A%B==0)
            printf("YES");
        else
            printf("NO");
        printf("\n");
        n--;            
     }
    return 0;
    
}

2.Max   Num (29min)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2071

Problem Description
There are some students in a class, Can you help teacher find the highest student .
Input
There are some cases. The first line contains an integer t, indicate the cases; Each case have an integer n ( 1 ≤ n ≤ 100 ) , followed n students’ height.
Output
For each case output the highest height, the height to two decimal plases;
Sample Input
2
3 170.00 165.00 180.00
4 165.00 182.00 172.00 160.00
Sample Output
180.00
182.00

题解:

         方法:找最大值下标。

         思路:定义一个数组,把输入的数存到一个数组中,这里需要考虑的是,输入n后,后面跟着一个空格,输入n个数时,每个数中间都有一个空格,使用scanf时,可以把前面n-1个数输入时,后面都加入一个空格,输入最后一个数时,不加空格。

         耗时原因:我耗时那么多是因为输入的数定义为double类型,在scanf()输入时,使用浮点数%0.2lf作为scanf()的参数。

代码如下:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
    int t; 
    scanf("%d",&t); 
    while(t>0)
    { 
        int n;
        double cases[100]={0};
        scanf("%d ",&n);
        int i=0;
        int Max=0;
        for(i=0;i<n-1;i++)
        {
             scanf("%lf ",&cases[i]);    
        }
        scanf("%lf",&cases[n-1]);
        
        for(i=1;i<n;i++)
        {
            if(cases[Max]<cases[i])
                Max=i;
        }
        printf("%0.2lf",cases[Max]);
        printf("\n");
        t--;            
        }
  
    return 0;
    
}

 3.三角形(54min)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2039

Problem Description
给定三条边,请你判断一下能不能组成一个三角形。
Input
输入数据第一行包含一个数M,接下有M行,每行一个实例,包含三个正数A,B,C。其中A,B,C <1000;
Output
对于每个测试实例,如果三条边长A,B,C能组成三角形的话,输出YES,否则NO。 
Sample Input
2
1 2 3
2 2 2
Sample Output
NO
YES
 题解:
        方法:逆向思维。
        构成三角形条件:两边之和大于第三边,两边之差小于第三边。
        思路:判断构成三角形条件比较多,我们可以逆向思维,判断每两条边之和如果不大于第三边,或者每两条边之差不小于第三遍,那么说明这三条边                   不能构成三角形。
        耗时原因:提交后一直显示时结果错误,我真不知道那里错了。
代码如下:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
    int t; 
    scanf("%d",&t); 
    while(t>0)
    { 
        int a,b,c; 
        int dis=0;
        scanf("%d %d %d",&a,&b,&c);
        int flag=1;
        if(a+b<=c||b+c<=a||a+c<=b)
            flag=0;
        dis=a-b;
        if(dis<0)
            dis=-dis;
        if(dis>=c)
            flag=0;    
        dis=b-c;
        if(dis<0)
            dis=-dis;
        if(dis>=a)
            flag=0;    
        dis=a-c;
        if(dis<0)
            dis=-dis;
        if(dis>=b)
            flag=0;
        if(flag==1)    
            printf("YES");
        if(flag==0)    
            printf("NO");
        printf("\n");
        t--;            
        }
  
    return 0;
    
}

 

posted @ 2020-02-09 21:35  萍2樱释  阅读(123)  评论(0编辑  收藏  举报