多校训练赛-K Time Zone(简单的模拟)

 

【题述】唯二做出来的题,虽然代码行数比题解的多了一倍...但是我的代码相对来讲,更容易理解一些

具体的解释放在代码里面很清楚了。

【题解代码】 

#include <cstdio>

#include <string>

#include <cstring>

#include <iostream>

#include <algorithm>

int main() {

扫描二维码关注公众号,回复: 2584718 查看本文章

  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);

    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;

}

 【我的代码(通过代码)】

#include <stdio.h>

#include <math.h>

#include <string.h>

int main(){

    int a,b,t,hour,min,stanh;

    char c[5];

    while(scanf("%d",&t)!=EOF){

        while(t--){

            scanf("%d %d UTC%s",&a,&b,c);

            int len=strlen(c);

            stanh=a-8;//把输入的时间转化为 utc+0 的时间:   a-8 : b

            //这里开始根据UTC后面的长度来进行判断。

            //+/- X的情况(x是1位数)

            if(len==2){

                if(c[0]=='+'){

                    hour=stanh+c[1]-'0';

                    min=b;

                }

                else{

                    hour=stanh-(c[1]-'0');

                    min=b;

                }

            }

            //+/- X的情况(x>9,x是两位数)

            else if(len==3){

                if(c[0]=='+'){

                    hour=stanh+(c[1]-'0')*10+(c[2]-'0');

                    min=b;

                }

                else{

                    hour=stanh-(c[1]-'0')*10-(c[2]-'0');

                    min=b;

                }

            }

            // +/- X.Y的情况(x是2位数)

            else if(len==4){

                if(c[0]=='+'){

                    hour=stanh+c[1]-'0';

                    min=b+(c[3]-'0')*6;

                }

                else{

                    hour=stanh-(c[1]-'0');

                    min=b-(c[3]-'0')*6;

                }

            }

            // +/- X.Y的情况(x是3位数)

            else if(len==5){

                if(c[0]=='+'){

                    hour=stanh+(c[1]-'0')*10+(c[2]-'0');

                    min=b+(c[4]-'0')*6;

                }

                else{

                    hour=stanh-(c[1]-'0')*10-(c[2]-'0');

                    min=b-(c[4]-'0')*6;

                }

            }

            //处理需要进位或减位的情况

            if(min<0){

                min+=60;

                hour-=1;

            }

            else if(min>59){

                min-=60;

                hour+=1;

            }

            if(hour<0){

                hour+=24;

            }

            else if(hour>=24){

                hour-=24;

            }

            printf("%02d:%02d\n",hour,min);

           

        }

    }

    return 0;

}

Chiaki often participates in international competitive programming contests. The time zone becomes a big problem. 
Given a time in Beijing time (UTC +8), Chiaki would like to know the time in another time zone ss. 

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/81333589
今日推荐