전화번호 지역 분류
Contact_info 구조체는 다음과 같다.
struct Contact_info { string name; string phone; };substr()함수를 이용하여 지역번호를 추출한다.
substr()함수 사용법 s = "abcdef"; s1 = s.substr(0, 3); //s1에 s의 0번째 부터 3개의 문자를 넣는다. cout << s1 << endl; 출력 : abc
Input
입력종료 문자가(EOF)가 입력되면 종료된다.Output
출력형식은 다음과 같다.area code 063 이름1 전화번호1 이름2 전화번호2 ... not area code 063 이름1 전화번호1 이름2 전화번호2 ... 출력조건은 다음과 같다. 1) 지역번호가 "063"이면 area code 063에, 그렇지 않다면 not area code 063에 출력한다. 2) 출력 순서는 area code 063, not area code 063순이다. 3) 이름은 가장 긴 사람에게 맞춘다. 4) area code 063, not area code 063은 해당하는 사람이 없을 경우에는 출력하지 않는다.
Sample Input1
Kim 063-000-0000 Park 062-000-0001 HongGilDong 063-000-0002
Sample Output1
area code 063 HongGilDong 063-000-0002 Kim 063-000-0000 not area code 063 Park 062-000-0001
Sample Input2
Jo 062-000-0003 HongGilDong 062-000-0002
Sample Output2
not area code 063 HongGilDong 062-000-0002 Jo 062-000-0003
접기|
#include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; #define UINT unsigned int // 전화번호 구조체 typedef struct _contact_info { string name; string phone; } Contact_info; bool compareTo( const Contact_info &A, const Contact_info &B ); int main(void) { string name; string phone; UINT max_length = 0; // 가장 긴 이름 vector<Contact_info> chonbuk; // 전화번호가 063인 vector list vector<Contact_info> elselocal; // 전화번호가 063이 아닌 vector list typedef vector<Contact_info>::iterator contact_iter; while( cin >> name ) { if( max_length < name.size() ) // 가장 긴 이름 저장 max_length = name.size(); cin >> phone; Contact_info contact; contact.name = name; contact.phone = phone; string local = phone.substr(0, 3); // 앞의 지역번호 3자리만 따오기 if( local == "063" ) chonbuk.push_back(contact); // 063이면 chonbuk vectlist 에 push else elselocal.push_back(contact); // 아니면 elselocal vector list에 push } // max_length - iter->name.size() 는 가장 긴 이름에서 현재 이름을 뺀 // 길이만큼의 공백에 기본공백 1개를 더함 if( chonbuk.size() > 0 ) { sort( chonbuk.begin(), chonbuk.end(), compareTo ); // 정렬 cout << "area code 063" << endl; for(contact_iter iter = chonbuk.begin() ; iter != chonbuk.end(); iter++ ) // vector 리스트 순회하면서 출력 { string name = iter->name + string(max_length - iter->name.size() + 1, ' '); cout << name << iter->phone << endl; } cout << endl; } if( elselocal.size() > 0 ) { sort( elselocal.begin(), elselocal.end(), compareTo ); cout << "not area code 063" << endl; for(contact_iter iter = elselocal.begin() ; iter != elselocal.end(); iter++ ) { string name = iter->name + string(max_length - iter->name.size() + 1, ' '); cout << name << iter->phone << endl; } } return 0; } bool compareTo( const Contact_info &A, const Contact_info &B ) { return A.name < B.name; }
단어들을 입력받아 단어가 숫자를 포함하는지(Include digit), 특수문자를 포함하는지(Include special character), 알파벳으로만 되어 있는지(Only alphabet)에 따라 3 그룹으로 분류해서 단어를 사전순으로 출력하는 프로그램을 작성하시오. cctype헤더파일에는 다음 함수들이 포함되어 있다. isspace(c) : c가 공백문자이면 true 반환 isalpha(c) : c가 알파벳이면 true 반환 isdigit(c) : c가 숫자이면 true 반환 isalnum(c) : c가 알파벳이나 숫자이면 true 반환 Input 입력종료문자(EOF)가 입력되면 종료된다. Output 출력형식은 다음과 같다. Include digit 단어1 단어2 ... Include special character 단어1 단어2 ... Only alphabet 단어1 단어2 ... 출력 조건은 다음과 같다. 1) 그룹 출력 순서는 Include digit, Include special character, Only alphabet순이다. 2) 그룹명은 해당 단어가 하나도 없으면 출력하지 않는다. Sample Input1 tiger! lion monkey 4.bear cat Sample Output1 Include digit 4.bear Include special character 4.bear tiger! Only alphabet cat lion monkey Sample Input2 java ruby C++ language Sample Output2 Include special character C++ Only alphabet java language ruby
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <cctype> using namespace std; int main(void) { vector<string> digit; vector<string> special; vector<string> alphabet; string word; while(cin >> word) { bool flag = true; for(string::iterator i = word.begin(); i != word.end(); i++) { if(isdigit(*i)) { digit.push_back(word); flag = false; break; } } for(string::iterator i = word.begin(); i != word.end(); i++) { if(!isalnum(*i)) { special.push_back(word); flag = false; break; } } if( flag == true ) alphabet.push_back(word); } if(digit.size() > 0) { sort(digit.begin(), digit.end()); cout << "Include digit" << endl; for(vector<string>::iterator i = digit.begin(); i != digit.end(); i++) cout << (*i) << endl; cout << endl; } if(special.size() > 0) { sort(special.begin(), special.end()); cout << "Include special character" << endl; for(vector<string>::iterator i = special.begin(); i != special.end(); i++) cout << (*i) << endl; cout << endl; } if(alphabet.size() > 0) { sort(alphabet.begin(), alphabet.end()); cout << "Only alphabet" << endl; for(vector<string>::iterator i = alphabet.begin(); i != alphabet.end(); i++) cout << (*i) << endl; } return 0; }
Posted by LaLuna