hdu 6308

怪不得自己一直t,原来是有好多函数自己不会用,学到了

注:时钟问题只需要化为分钟来做,就会简便很多

C/C++中字符串与数值相互转换

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

int main()
 {
  std::ios::sync_with_stdio(false);
 
 int T;
  std::cin >> T;
 
 for (int cas = 1; cas <= T; ++cas)
 {
    int sgn;
    int a, b, x, y(0);
  
  std::string s;
   
 std::cin >> a >> b >> s;
   
 sgn = (s[3] == '+' ? 1 : -1);
  
  s = s.substr(4);//将从下标为4一直到字符串结束赋值给新字符串 
 
   if (s.size() > 2) 
{
      y = s.back() - '0';
    
      s.pop_back();
    
      s.pop_back();
    }
   
 x = std::stoi(s);//将字符串转为整数 

  
  int m = (a + 24) * 60 + b;

  
  int c = 80;
  
  int d = sgn * (x * 10 + y);

 int e = d - c;
   
 int f = e * 6;
   
 int g = (m + f) % 1440;

 
   printf("%02d:%02d\n", g / 60, g % 60);
  
}
 
 return 0;
}
			

猜你喜欢

转载自blog.csdn.net/pinkair/article/details/81183536
今日推荐