zoukankan      html  css  js  c++  java
  • HDU 4288

    View Code
     1 #include <iostream>
     2 #include <cstring>
     3 #include <map>
     4 #include <iterator> 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9      int i,j,k,T;
    10      while(cin>>T)
    11      {
    12           int ans = 0;
    13           map <int ,bool > mm;
    14           for(i=1;i<=T;i++)
    15           {
    16                char str[5];
    17                int temp;
    18                cin>>str;
    19                if(strcmp(str,"sum"))
    20                     cin>>temp;
    21                if(strcmp(str,"add")==0)
    22                     mm[temp] = 1;
    23                else if(strcmp(str,"del")==0)
    24                     mm.erase(temp);
    25                else
    26                {
    27                     map <int ,bool >::iterator ptr = mm.begin();
    28                     //下面的有问题,反正j和ptr必须同步自增,比赛时分别加一,结果 TLE 
    29                     //map <int ,bool >::iterator ptr = find(mm.begin(),mm.end(),2);
    30                     //advance(ptr,2);//既然自增,则ptr必须初始化 
    31                     for(ptr = mm.begin(),j=1;ptr!=mm.end();j+=1,ptr++)//条件不是不等于 因为ptr自增5啦 
    32                     if(j%5==3)
    33                          ans += ptr->first;
    34                     cout<<ans<<endl;
    35                     ans = 0;
    36                } 
    37           }
    38      }
    39      return 0;
    40 }              
    41                
    42                     
    43                
    View Code
     1 //继续tle 
     2 #include <iostream>
     3 #include <cstring>
     4 #include <map>
     5 #include <iterator> 
     6 using namespace std;
     7 
     8 int main()
     9 {
    10      int i,j,k,T;
    11      while(cin>>T)
    12      {
    13           int ans = 0;
    14           map <int ,bool > mm;//不能再for循环内部 
    15           for(i=1;i<=T;i++)
    16           {
    17                char str[5];
    18                int temp;
    19                cin>>str;
    20                if(strcmp(str,"sum"))
    21                     cin>>temp;
    22                if(strcmp(str,"add")==0)
    23                     mm[temp] = 1;
    24                else if(strcmp(str,"del")==0)
    25                     mm.erase(temp);
    26                else
    27                {
    28                     map <int ,bool >::iterator ptr = mm.begin();
    29                     advance(ptr,2);//既然自增,则ptr必须初始化 
    30                     for(j=3;j<=mm.size();j+=5,advance(ptr,5))//条件不是ptr!=mm.end(),而ptr<mm.end()会ce 因为ptr自增5啦 
    31                          ans += ptr->first;
    32                     cout<<ans<<endl;
    33                     ans = 0;
    34                } 
    35           }
    36      }
    37      return 0;
    38 }              
    39                
    40                     
    41                
  • 相关阅读:
    LeetCode20 有效的括号
    函数的多个参数
    定义一个函数的基本语法 函数的参数
    函数
    金字塔
    水仙花数
    百鸡百钱
    循环demo
    while适用于不确定循环次数
    浏览器打断点
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2687761.html
Copyright © 2011-2022 走看看