cisco笔试记录

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了cisco笔试记录脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

输入一个字符串,转换为特定的格式,如:2019, 12, 31, 23, 59, 59, 1表示在2019-12-31 23:59:59的1s后时间,即:2020-01-01 00:00:00

咋一看很简单,但还是发现自己的代码基础还需要很大提升,我至少写了2个小时,最大的收获感觉就是不管遇到简单还是复杂的题,一定要先把思路先写清楚,越细节越好,不然写好之后也很难调试。

思路:

    @H_512_7@首先从进位的角度考虑,时分秒的进位都是固定的:24,60,60,可以先用一个循环把秒进位转换成日进位。
  1. 由于每个月的日期数不同,因此可能是:逢31进1,逢30进一,以及2月的闰年逢29进一与普通年份的逢28进一。
  2. 以月为周期,每个周期的日期数都不相同,因此用while循环一个月一个月地消减日期数.
     1 #include<iostream>
     2 #include<string>
     3 #include<vector>
     4 #include<algorIThm>
     5 using namespace std;
     6 
     7 // 时分秒进位
     8 vector<int> fulPRe(int fp, int ndata, int fdata) {
     9     vector<int> res;
    10     res.push_back((fdata + ndata) / fp);    
    11     res.push_back((fdata + ndata) % fp);
    12     return res;
    13 }
    14 
    15 // 按月进位
    16 bool modymd(vector<int>& arr, int zq, int& date) {
    17     int alen = arr.size() - 1;
    18     if ((date + arr[2]) / (zq+1) > 0) {  
    19         date =date- (zq - arr[2])-1;
    20         if (arr[1] == 12) {
    21             arr[0]++;arr[1] = 1;
    22         }
    23         else {
    24             arr[1] = arr[1] + 1;
    25         }
    26         if (date >= 0) { arr[2] = 1; }
    27         return true;
    28     }
    29     else {
    30         arr[2] = date + arr[2];
    31         return false;
    32     }
    33 }
    34 
    35 string getTime(vector<int>& arr) {
    36     int alen = arr.size() - 1;
    37     int afsec = arr[alen];
    38     // 时分秒进位
    39     vector<int> unit = { 60,60,24 }, tmp = { afsec,0 };
    40     for (int i = 0; i < 3; ++i) {
    41         tmp = fulpre(unit[i], arr[5 - i], tmp[0]);
    42         arr[5 - i] = tmp[1];
    43     }
    44     // 年月日进位
    45     int date = tmp[0];
    46     vector<int> tod = { 1,3,5,7,8,10,12 }, tzd = { 4,6,9,11 }, tsed = { 2 };
    47     while (date > 0) {
    48         vector<int>::iterator it;
    49         bool flag = false,tg=false;
    50         if (count(tod.begin(), tod.end(), arr[1]) > 0) flag = modymd(arr, 31, date);
    51         else if (count(tzd.begin(), tzd.end(), arr[1]) > 0) flag = modymd(arr, 30, date);
    52         else {       // 对于二月分,要考虑闰年
    53             if ((arr[0] % 4 == 0 && arr[0] % 100 != 0) || arr[0] % 400 == 0) {
    54                 flag = modymd(arr, 29, date);
    55             }
    56             else flag = modymd(arr, 28, date);
    57         }
    58         if (!flag){
    59             break;
    60         }
    61     }
    62     // 将结果打包成字符串
    63     string s;
    64     string cs[6] = { "-","-"," ",":",":",""};
    65     for (int i = 0; i < alen; ++i) {
    66         if(arr[i]<10) s.apPEnd(to_string(0));   // 小于10的数字,需要首先填充0
    67         s.append(to_string(arr[i])+cs[i]);
    68     }
    69     return s;
    70 }
    71 
    72 int main() {
    73     vector<int> arr;
    74     int tmp;
    75     /*do{
    76         if (cin.get() != ',') {
    77             cin >> tmp;
    78             arr.push_back(tmp);
    79         }
    80     } while (cin.get() != 'n');*/
    81     do {
    82             cin >> tmp;
    83             arr.push_back(tmp);
    84     } while (cin.get() != 'n'); 
    85     cout << getTime(arr);
    86     return 0;
    87 }

    另外还要注意字符串的追加、向量输入(其实这里也可以用getline() 然后分割字符串):这里由于tmp是int,好像自动忽略了','。

脚本宝典总结

以上是脚本宝典为你收集整理的cisco笔试记录全部内容,希望文章能够帮你解决cisco笔试记录所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。