Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

C++: 两种思路:1. 字符串版本,2.数字版本

Posted by MonkAndMonkey at 2017-02-06 16:15:03 on Problem 1002
我的思路是采用字符串版本,C++,一直WA,后来看讨论版说是cin,cout,问题,改了之后还是不行,最后发现是逻辑错误:当重复电话只有1个,且出现在最后时,需要当心,这是我犯的错误。
我的经验:
1. Q和Z 可以不用管,AC之后我又删掉了对QZ的判断,仍然AC。
2. No duplicates.的情况
3. 电话用int表示会大大减少内存占用,提高效率, 输出时前面要用0补齐。
4. C++ AC,G++仍然TLE。
====================================================================
1. 字符串版本:
// 1002. 487-3279
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;


string getNbr(string nbr){
    string nbr1;
    int j = 0;
    for(int i=0; i<nbr.length(); i++){
        char ch = nbr[i];
        if(ch>='0' && ch<='9'){
            nbr1.append(1, ch);
        }else if(ch != '-'){
            int n = 0;
            if(ch <= 'P' && ch >= 'A'){
                n = (nbr[i]-'A')/3+2;
            }else if(ch > 'P'){
                n = (nbr[i]-'A'- 1)/3+2;
            }
            nbr1.append(1, '0' + n);
        }
    }

    return nbr1;
}

int main(){
    int N = 0;
    bool NO_DUP = true;
    vector<string> nbrs;
    cin>>N;
    for(int i=0; i<N; i++){
        string nbr;
        cin>>nbr;
        nbrs.push_back(getNbr(nbr));
    }
    // int排升序
    sort(nbrs.begin(), nbrs.end());

    // 重复次数
    string cur_nbr = "";

    int times = 0;
    for(int i=0; i<nbrs.size(); i++){
        if(cur_nbr != nbrs[i]){
            if(times > 1){
                NO_DUP = false;
                string dup_nbr = cur_nbr;
                cout<<dup_nbr.insert(3,1,'-')<<' '<<times<<endl;
            }
            cur_nbr = nbrs[i];
            times = 1;
        }else{
            times++;
        }
	}
    if(times > 1){
        NO_DUP = false;
        string dup_nbr = cur_nbr;
        cout<<dup_nbr.insert(3,1,'-')<<' '<<times<<endl;
    }
    // No duplicates.
    if(NO_DUP){
        cout<<"No duplicates."<<endl;
    }
	return 0;
}
====================================================================
2. 数字版本:
// 1002. 487-3279
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;


int getNbr(string nbr){
    string nbr1;
    int j = 0;
    for(int i=0; i<nbr.length(); i++){
        char ch = nbr[i];
        if(ch>='0' && ch<='9'){
            nbr1.append(1, ch);
        }else if(ch != '-' && ch != 'Q' && ch != 'Z'){
            int n = 0;
            if(ch <= 'P' && ch >= 'A'){
                n = (nbr[i]-'A')/3+2;
            }else if(ch > 'P'){
                n = (nbr[i]-'A'- 1)/3+2;
            }
            nbr1.append(1, '0' + n);
        }
    }
    int n = atoi(nbr1.c_str());
    return n;
}

int main(){
    int N = 0;
    bool NO_DUP = true;
    vector<int> nbrs;
    //cin>>N;
    scanf("%d", &N);
    for(int i=0; i<N; i++){
        string nbr;
        cin>>nbr;
        nbrs.push_back(getNbr(nbr));
    }
    // int排升序
    sort(nbrs.begin(), nbrs.end());

    // 重复次数
    int cur_nbr = -1;
    int times = 0;
    for(int i=0; i<nbrs.size(); i++){
        if(cur_nbr != nbrs[i]){
            if(times > 1){
                NO_DUP = false;
                printf("%0.3d-%0.4d %d\n", cur_nbr/10000,cur_nbr%10000,times);
            }
            cur_nbr = nbrs[i];
            times = 1;
        }else{
            times++;
        }
	}
    if(times > 1){
        NO_DUP = false;
        printf("%0.3d-%0.4d %d\n", cur_nbr/10000,cur_nbr%10000,times);
    }
    // No duplicates.
    if(NO_DUP){
        printf("No duplicates.\n");
    }
	return 0;
}



Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator