zoukankan      html  css  js  c++  java
  • CodeForces 105A Transmigration(排序+浮点误差)

    题意:一个人有n个技能,每个技能有一个等级(0-9999),他要死一次,死一次这n个技能每个变成原来的k(0.01-0.99)倍(取整数),如果这个技能在他死一次之后小于100,那该技能消失,他重生以后可以学m个技能,等级为0(不消失);最后问他拥有多少技能,每个技能的等级是多少

    解题思路:这个题主要是对取整的处理,因为浮点误差的原因,取整上会有一定的错误(少1),所以要做个特判。我是用cmp排个序就行了。

    解题代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #include <ctype.h>
     5 #include <math.h>
     6 struct node{
     7  char str[1000];
     8  int value;
     9 }a[50];
    10 int cmp(const void *a ,const void * b)
    11 {
    12     if(strcmp((*(node *)a).str,(*(node*)b).str) == 0)
    13         return (*(node*)a).value - (*(node*)b).value;
    14     else return strcmp((*(node *)a).str,(*(node*)b).str);
    15 }
    16 double kabs(double temp)
    17 {
    18    if(temp < 0 )
    19        return - temp;
    20    else return temp;
    21 }
    22 int fuck(double temp)
    23 {
    24    if(kabs(temp - (int)temp -1.0) < 1e-8)
    25        return (int)(temp/1) + 1;
    26    else return (int)(temp/1);
    27 }
    28 int main()
    29 {
    30     int n , m ;
    31     double k ; 
    32     scanf("%d %d %lf",&n,&m ,&k);
    33 
    34     for(int i =1 ;i <= n;i ++)
    35     {    
    36         scanf("%s %d",a[i].str,&a[i].value);
    37         double temp = a[i].value*k; 
    38         a[i].value = fuck(temp);
    39         //printf("%d
    ",a[i].value);
    40         if(a[i].value < 100  )
    41             a[i].value = 0; 
    42     }
    43     for(int i = 1;i <= m;i ++){
    44         scanf("%s",a[i+n].str);
    45         a[i+n].value = -1;
    46     }
    47     qsort(a+1,n+m,sizeof(node),cmp);
    48     int ans =  0 ;
    49     
    50     for(int i =1 ;i <= n+m;i++)
    51     {
    52         if(a[i].value != 0 && a[i].value != -1)
    53         {    
    54              ans ++;
    55         }
    56         else if(a[i].value == -1  )
    57              {
    58                 if(strcmp(a[i].str ,a[i+1].str) == 0 && a[i+1].value == 0)
    59                      ans ++;
    60                 if(strcmp(a[i].str ,a[i+1].str) != 0)
    61                   ans ++;
    62              }
    63         
    64     }
    65     printf("%d
    ",ans);
    66     for(int i =1 ;i <= n+m;i++)
    67     {
    68         if(a[i].value != 0 && a[i].value != -1)
    69         {    ans ++;
    70             printf("%s %d
    ",a[i].str,a[i].value);
    71         }
    72         else if(a[i].value == -1  )
    73              {
    74                 if(strcmp(a[i].str ,a[i+1].str) == 0 && a[i+1].value == 0)
    75                     { ans ++;
    76                       printf("%s 0
    ",a[i].str);
    77                     }
    78                 if(strcmp(a[i].str ,a[i+1].str) != 0)
    79                    {ans ++;
    80                     printf("%s 0
    ",a[i].str);
    81                    }
    82              }
    83         
    84     }
    85     return 0;
    86 }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    JS中的constructor、prototype、__proto__的要点理解
    call、apply、bind的源码模拟
    js中的继承方式
    IPV6
    java8 instant localDateTime
    FileInputFormat 的实现之TextInputFormat
    Git服务器,仓库的hook监控
    Linux 开机启动图形界面,shell界面
    java操作Hbase
    Linux 下安装 storm
  • 原文地址:https://www.cnblogs.com/zyue/p/3411132.html
Copyright © 2011-2022 走看看