728x90
1๋ฒ
#include<iostream>
#include<string>
bool palindrome(const std::string& s);
int main()
{
std::string temp;
std::cout << "๋ฌธ์์ด ์
๋ ฅ: ";
getline(std::cin, temp);
if (palindrome(temp))
std::cout <<temp <<"๋ ํ๋ฌธ์
๋๋ค.\n";
else
std::cout <<temp<< "๋ ํ๋ฌธ์ด ์๋๋๋ค.\n";
return 0;
}
bool palindrome(const std::string& s)
{
std::string t(s.rbegin(), s.rend());
return t == s;
}
2๋ฒ
#include<iostream>
#include<string>
#include<cctype>
#include<new>
bool palindrome(const std::string& s);
void change(std::string& s);
int main()
{
std::string temp;
std::cout << "๋ฌธ์์ด ์
๋ ฅ: ";
getline(std::cin, temp);
change(temp);
std::cout << temp << '\n';
if (palindrome(temp))
std::cout <<temp <<"๋ ํ๋ฌธ์
๋๋ค.\n";
else
std::cout <<temp<< "๋ ํ๋ฌธ์ด ์๋๋๋ค.\n";
return 0;
}
bool palindrome(const std::string& s)
{
std::string t(s.rbegin(), s.rend());
return t == s;
}
void change(std::string& s)
{
std::string t;
for (int i=0; i <s.size(); ++i)
{
if (isalpha(s[i]))
{
s[i] = tolower(s[i]);
t += s[i];
}
}
s = t;
}
3๋ฒ
// hangman.cpp -- string ๋ฉ์๋๋ค
#include <iostream>
#include<fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include<vector>
using std::string;
using std::vector;
vector<string> wordlist;
int main()
{
using std::cout;
using std::cin;
using std::tolower;
using std::endl;
std::ifstream inFile;
inFile.open("wordlist.txt");
while (inFile.good())
{
std::string temp;
inFile >> temp;
wordlist.push_back(temp);
}
inFile.close();
const int NUM = wordlist.size();
std::srand(std::time(0));
char play;
cout << "์์ด ๋จ์ด ๊ฒ์์ ํ์๊ฒ ์ต๋๊น? <y/n> ";
cin >> play;
play = tolower(play);
while (play == 'y')
{
string target = wordlist[std::rand() % NUM];
int length = target.length();
string attempt(length, '-');
string badchars;
int guesses = 6;
cout << "์์๊ป๋ผ ๋จ์ด๋ฅผ ์ถ์ธกํด ๋ณด์ญ์์ค.\n"
<< length << "๊ฐ์ ๋ฌธ์๋ก ์ด๋ฃจ์ด์ ธ ์์ต๋๋ค.\n"
<< "ํ ๋ฒ์ ํ ๋ฌธ์์ฉ ์ถ์ธกํ์ญ์์ค.\n"
<< "ํ๋ฆด ์ ์๋ ๊ธฐํ: " << guesses << "๋ฒ\n";
cout << "์ถ์ธกํ๋ ๋จ์ด: " << attempt << endl;
while (guesses > 0 && attempt != target)
{
char letter;
cout << "๋ฌธ์๋ฅผ ์ถ์ธกํ์ญ์์ค: ";
cin >> letter;
if (badchars.find(letter) != string::npos
|| attempt.find(letter) != string::npos)
{
cout << "์ด๋ฏธ ์ถ์ธกํ ๋ฌธ์์
๋๋ค. ๋ค์ ํ์ญ์์ค.\n";
continue;
}
int loc = target.find(letter);
if (loc == string::npos)
{
cout << "๋ก! ํ๋ ธ์ต๋๋ค.\n";
--guesses;
badchars += letter; // ํ๋ฆฐ ์ถ์ธก๋ค์ ๋ฌธ์์ด์ ์ถ๊ฐํ๋ค
}
else
{
cout << "๋ฉ๋๋! ๋ง์์ต๋๋ค.\n";
attempt[loc] = letter;
// ๊ฐ์ ๋ฌธ์๊ฐ ๋ ์๋์ง ๊ฒ์ฌํ๋ค
loc = target.find(letter, loc + 1);
while (loc != string::npos)
{
attempt[loc] = letter;
loc = target.find(letter, loc + 1);
}
}
cout << "์ถ์ธกํ๋ ๋จ์ด: " << attempt << endl;
if (attempt != target)
{
if (badchars.length() > 0)
cout << "ํ๋ฆฌ๊ฒ ์ถ์ธกํ ๋ฌธ์๋ค: " << badchars << endl;
cout << "ํ๋ฆด ์ ์๋ ๊ธฐํ: " << guesses << "๋ฒ\n";
}
}
if (guesses > 0)
cout << "๊ทธ๋ ์ต๋๋ค. ๊ทธ๊ฒ์ด ์์๊ป๋ผ ๋จ์ด์
๋๋ค.\n";
else
cout << "์ํ๊น์ต๋๋ค. ์์๊ป๋ผ ๋จ์ด๋ " << target << "์
๋๋ค.\n";
cout << "๊ฒ์์ ๋ค์ ํ์๊ฒ ์ต๋๊น? <y/n> ";
cin >> play;
play = tolower(play);
}
cout << "ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.\n";
return 0;
}
4๋ฒ
#include<iostream>
#include<algorithm>
int reduce(long ar[], int n);
int main()
{
long a[5] = { 8,2,4,5,2 };
int n = reduce(a, 5);
std::cout << n;
return 0;
}
int reduce(long ar[], int n)
{
std::sort(ar, ar + n);
long* pastend = std::unique(ar, ar + n);
return pastend - ar;
}
5๋ฒ
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
template<class T>
int reduce(T ar[], int n);
template<class T>
void Show(const T ar[], int n);
int main()
{
long a[5] = { 8,2,4,5,2 };
string b[4] = { "abc","fdd","eer","abc" };
int n=reduce(a, 5);
Show(a, n);
n = reduce(b, 4);
Show(b, n);
return 0;
}
template<class T>
int reduce(T ar[], int n)
{
sort(ar, ar + n);
T* pastend=unique(ar, ar + n);
return pastend - ar;
}
template<class T>
void Show(const T ar[], int n)
{
for (int i = 0; i < n; i++)
cout << ar[i] << " ";
cout<<endl;
}
6๋ฒ
// queue.h -- ํ๋ฅผ ์ํ ์ธํฐํ์ด์ค
#ifndef QUEUE_H_
#define QUEUE_H_
// ์ด ํ๋ Customer ํญ๋ชฉ๋ค์ ํฌํจํ๊ฒ ๋๋ค
class Customer
{
private:
long arrive; // ๊ณ ๊ฐ์ด ํ์ ๋์ฐฉํ ์๊ฐ
int processtime; // ๊ณ ๊ฐ์ ๊ฑฐ๋๋ฅผ ์ฒ๋ฆฌํ๋ ์๊ฐ
public:
Customer() { arrive = processtime = 0; }
void set(long when);
long when() const { return arrive; }
int ptime() const { return processtime; }
};
typedef Customer Item;
#endif
// queue.cpp -- Queue์ Customer ๋ฉ์๋๋ค
#include "queue.h"
#include <cstdlib> // (๋๋ stdlib.h) rand()๋ฅผ ์ํด
// customer ๋ฉ์๋
// when์ ๊ณ ๊ฐ์ด ๋์ฐฉํ ์๊ฐ์ด๋ค
// ๋์ฐฉ ์๊ฐ์ when์ผ๋ก ์ค์ ๋๋ค
// ์ฒ๋ฆฌ ์๊ฐ์ 1, 2, 3 ์ค์์ ๋ฌด์์๋ก ํ ๊ฐ์ด ์ค์ ๋๋ค
void Customer::set(long when)
{
processtime = std::rand() % 3 + 1;
arrive = when;
}
// bank.cpp -- Queue ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ๋ค
// queue.cpp์ ํจ๊ป ์ปดํ์ผํ๋ค
#include <iostream>
#include <cstdlib> // rand()์ srand()๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด
#include <ctime> // time()์ ์ฌ์ฉํ๊ธฐ ์ํด
#include "queue.h"
#include<queue>
const int MIN_PER_HR = 60;
bool newcustomer(double x); // ์ ๊ณ ๊ฐ์ด ๋์ฐฉํ๋๊ฐ?
int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::queue;
using std::ios_base;
// ํ์ํ ์ฌ๋ฌ ๊ฐ์ง๋ฅผ ์ค๋นํ๋ค
std::srand(std::time(0)); // rand()์ ๋ฌด์์ ์ด๊ธฐํ
cout << "์ฌ๋ก ์ฐ๊ตฌ: ํ์ ์ํ์ ATM\n";
cout << "ํ์ ์ต๋ ๊ธธ์ด๋ฅผ ์
๋ ฅํ์ญ์์ค: ";
int qs;
cin >> qs;
queue<Item> line; // line ํ์๋ ์ต๋ qs๋ช
๊น์ง ๋๊ธฐํ ์ ์๋ค
cout << "์๋ฎฌ๋ ์ด์
์๊ฐ ์๋ฅผ ์
๋ ฅํ์ญ์์ค: ";
int hours; // ์๋ฎฌ๋ ์ด์
์๊ฐ ์
cin >> hours;
// ์๋ฎฌ๋ ์ด์
์ 1๋ถ์ 1์ฃผ๊ธฐ๋ฅผ ์คํํ๋ค
long cyclelimit = MIN_PER_HR * hours; // ์๋ฎฌ๋ ์ด์
์ฃผ๊ธฐ ์
cout << "์๊ฐ๋น ํ๊ท ๊ณ ๊ฐ ์๋ฅผ ์
๋ ฅํ์ญ์์ค: ";
double perhour; // ์๊ฐ๋น ํ๊ท ๊ณ ๊ฐ ์
cin >> perhour;
double min_per_cust; // ํ๊ท ๊ณ ๊ฐ ๋์ฐฉ ๊ฐ๊ฒฉ(๋ถ ๋จ์)
min_per_cust = MIN_PER_HR / perhour;
Item temp; // ์ ๊ณ ๊ฐ ๋ฐ์ดํฐ
long turnaways = 0; // ํ๊ฐ ๊ฐ๋ ์ฐจ์ ๋ฐ๊ธธ์ ๋๋ฆฐ ๊ณ ๊ฐ ์
long customers = 0; // ํ์ ์ค์ ์ ๊ณ ๊ฐ ์
long served = 0; // ์๋ฎฌ๋ ์ด์
์์ ๊ฑฐ๋๋ฅผ ์ฒ๋ฆฌํ ๊ณ ๊ฐ ์
long sum_line = 0; // ํ์ ๋์ ๊ธธ์ด
int wait_time = 0; // ATM์ด ๋น ๋๊น์ง ๋๊ธฐํ๋ ์๊ฐ
long line_wait = 0; // ๊ณ ๊ฐ๋ค์ด ์ค์ ์์ ๋๊ธฐํ ๋์ ์๊ฐ
// ์๋ฎฌ๋ ์ด์
์ ์คํํ๋ค
for (int cycle = 0; cycle < cyclelimit; cycle++)
{
if (newcustomer(min_per_cust)) // ์ ๊ณ ๊ฐ์ด ๋์ฐฉํ๋ค
{
if (qs==line.size())
turnaways++;
else
{
customers++;
temp.set(cycle); // cycle์ด ๋์ฐฉ ์๊ฐ์ด ๋๋ค
line.push(temp); // ํ์ ์ ๊ณ ๊ฐ์ ์ถ๊ฐํ๋ค
}
}
if (wait_time <= 0 && line.size()!=0)
{
temp = line.front();
line.pop(); // ๋ค์ ๊ณ ๊ฐ์ ๋ฐ๋๋ค
wait_time = temp.ptime(); // wait_time์ ์ค์ ํ๋ค
line_wait += cycle - temp.when();
served++;
}
if (wait_time > 0)
wait_time--;
sum_line += line.size();
}
// ์๋ฎฌ๋ ์ด์
๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค
if (customers > 0)
{
cout << " ํ์ ์ค์ ์ ๊ณ ๊ฐ ์: " << customers << endl;
cout << "๊ฑฐ๋๋ฅผ ์ฒ๋ฆฌํ ๊ณ ๊ฐ ์: " << served << endl;
cout << " ๋ฐ๊ธธ์ ๋๋ฆฐ ๊ณ ๊ฐ ์: " << turnaways << endl;
cout << " ํ๊ท ํ์ ๊ธธ์ด: ";
cout.precision(2);
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.setf(ios_base::showpoint);
cout << (double)sum_line / cyclelimit << endl;
cout << " ํ๊ท ๋๊ธฐ ์๊ฐ: "
<< (double)line_wait / served << "๋ถ\n";
}
else
cout << "๊ณ ๊ฐ์ด ํ ๋ช
๋ ์์ต๋๋ค!\n";
cout << "์๋ฃ!\n";
return 0;
}
// x๋ ๊ณ ๊ฐ ๊ฐ์ ํ๊ท ์๊ฐ ๊ฐ๊ฒฉ์ด๋ค(๋ถ ๋จ์)
// ์ด ์๊ฐ ๋ด์ ๊ณ ๊ฐ์ด ๋์ฐฉํ๋ฉด ๋ฆฌํด๊ฐ์ true์ด๋ค
bool newcustomer(double x)
{
return (std::rand() * x / RAND_MAX < 1);
}
7๋ฒ
#include<iostream>
#include<vector>
#include<algorithm>
#include<time.h>
using namespace std;
vector<int> Lotto(int total_n, int sel_n);
int main()
{
srand(time(NULL));
vector<int> winners = Lotto(51, 6);
for (int i = 0; i < 6; i++)
cout << winners[i] << ' ';
return 0;
}
vector<int> Lotto(int total_n, int sel_n)
{
vector<int> temp(total_n);
for (int i = 0; i < total_n; i++)
temp[i] = i + 1;
random_shuffle(temp.begin(), temp.end());
temp.resize(sel_n);
return temp;
}
8๋ฒ
#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<iterator>
#include<algorithm>
using namespace std;
void input(set<string>& s);
int main()
{
set<string> Mat,Pat;
cout << "Mat์ ์น๊ตฌ ๋ชฉ๋ก ์
๋ ฅ (๋๋ด๋ ค๋ฉด quit ์
๋ ฅ) : ";
input(Mat);
cout << "Pat์ ์น๊ตฌ ๋ชฉ๋ก ์
๋ ฅ (๋๋ด๋ ค๋ฉด quit ์
๋ ฅ): ";
input(Pat);
ostream_iterator<string, char> out(cout, " ");
cout << "Mat๊ฐ ์
๋ ฅํ ์น๊ตฌ๋ชฉ๋ก\n";
copy(Mat.begin(), Mat.end(), out);
cout << endl;
cout << "Pat๊ฐ ์
๋ ฅํ ์น๊ตฌ๋ชฉ๋ก\n";
copy(Pat.begin(), Pat.end(), out);
cout << endl;
set<string> MatPat;
set_union(Mat.begin(), Mat.end(), Pat.begin(), Pat.end(),
insert_iterator<set<string>>(MatPat, MatPat.begin()));
cout << "Mat์ Pat์ ์น๊ตฌ ๋ชฉ๋ก\n";
copy(MatPat.begin(), MatPat.end(), out);
cout << endl;
return 0;
}
void input(set<string>& s)
{
string temp;
getline(cin, temp);
while (temp != "quit")
{
s.insert(temp);
getline(cin, temp);
}
}
9๋ฒ
#include<iostream>
#include<ctime>
#include<vector>
#include<algorithm>
#include<list>
using namespace std;
const int SIZE = 100000;
int main()
{
vector<int> vi0(SIZE);
int i;
for (i = 0; i < SIZE; i++)
vi0[i] = std::rand() % 100;
vector<int> vi;
list<int> li;
vi.insert(vi.begin(), vi0.begin(), vi0.end());
li.insert(li.begin(), vi0.begin(), vi0.end());
clock_t start = clock();
sort(vi.begin(), vi.end());
clock_t end = clock();
cout << "vi ์ ๋ ฌํ๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ : " << (double)(end - start) / CLOCKS_PER_SEC << endl;
start = clock();
li.sort();
end = clock();
cout << "li ์ ๋ ฌํ๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ : " << (double)(end - start) / CLOCKS_PER_SEC << endl;
li.insert(li.begin(), vi0.begin(), vi0.end());
//li๋ฅผ vi์ ๋ณต์ฌ
start = clock();
vi.insert(vi.begin(), li.begin(), li.end());
sort(vi.begin(), vi.end());
li.insert(li.begin(), vi.begin(), vi.end());
end = clock();
cout << "li๋ฅผ vi์ ๋ณต์ฌํ๊ณ vi๋ฅผ ์ ๋ ฌํ๊ณ ์ ๋ ฌ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ li๋ก ๋ณต์ฌํ๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ : " << (double)(end - start) / CLOCKS_PER_SEC << endl;
return 0;
}
10๋ฒ
// vect3.cpp -- STL ํจ์๋ค์ ์ฌ์ฉํ๋ค
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include<memory>
struct Review {
std::string title;
int rating;
int price;
};
bool operator<(const Review& r1, const Review& r2);
bool worseThan(const Review& r1, const Review& r2);
bool FillReview(Review& rr);
void ShowReview(const Review& rr);
bool worseThan2(const Review& r1, const Review& r2);
int main()
{
using namespace std;
shared_ptr<vector<Review>> books (new vector<Review>);
Review temp;
while (FillReview(temp))
books->push_back(temp);
shared_ptr<vector<Review>> rem(new vector<Review>);
*rem = *books;
char choice;
cout << "๋ค์์ ์ ํ ์ฌํญ ์ค ํ๋๋ฅผ ์
๋ ฅํ์ญ์์ค.\n"
<< "a. ์๋ ์์\tb. ์ํ๋ฒณ ์์\tc. ์ฆ๊ฐ์จ ์์\n"
<< "d. ๊ฐ์์จ ์์\te. ๊ฐ๊ฒฉ ์ฆ๊ฐ ์์\tf. ๊ฐ๊ฒฉ ๊ฐ์ ์์\ng. ์์
๋๋ด๊ธฐ\n";
cin >> choice;
while (choice !='q')
{
if (choice == 'g')
break;
switch (choice)
{
case 'a':
cout << "์๋ ์์\n๋ฑ๊ธ\t์ ๋ชฉ\t๊ฐ๊ฒฉ\n";
for_each(rem->begin(), rem->end(), ShowReview);
break;
case 'b':
sort(books->begin(), books->end());
cout << "์ํ๋ฒณ ์์\n๋ฑ๊ธ\t์ ๋ชฉ\t๊ฐ๊ฒฉ\n";
for_each(books->begin(), books->end(), ShowReview);
break;
case 'c':
sort(books->begin(), books->end(), worseThan);
cout << "์ฆ๊ฐ์จ ์์\n๋ฑ๊ธ\t์ ๋ชฉ\t๊ฐ๊ฒฉ\n";
for_each(books->begin(), books->end(), ShowReview);
break;
case 'd':
sort(books->begin(), books->end(), worseThan);
cout << "๊ฐ์์จ ์์\n๋ฑ๊ธ\t์ ๋ชฉ\t๊ฐ๊ฒฉ\n";
for_each(books->rbegin(), books->rend(), ShowReview);
break;
case 'e':
sort(books->begin(), books->end(), worseThan2);
cout << "๊ฐ๊ฒฉ ์ฆ๊ฐ์จ ์์\n๋ฑ๊ธ\t์ ๋ชฉ\t๊ฐ๊ฒฉ\n";
for_each(books->begin(), books->end(), ShowReview);
break;
case 'f':
sort(books->begin(), books->end(), worseThan2);
cout << "๊ฐ๊ฒฉ ๊ฐ์์จ ์์\n๋ฑ๊ธ\t์ ๋ชฉ\t๊ฐ๊ฒฉ\n";
for_each(books->rbegin(), books->rend(), ShowReview);
break;
}
cout << "๋ค์์ ์ ํ ์ฌํญ ์ค ํ๋๋ฅผ ์
๋ ฅํ์ญ์์ค.\n"
<< "a. ์๋ ์์\tb. ์ํ๋ฒณ ์์\tc. ์ฆ๊ฐ์จ ์์\n"
<< "d. ๊ฐ์์จ ์์\te. ๊ฐ๊ฒฉ ์ฆ๊ฐ ์์\tf. ๊ฐ๊ฒฉ ๊ฐ์ ์์\ng. ์์
๋๋ด๊ธฐ\n";
cin >> choice;
}
return 0;
}
bool operator<(const Review& r1, const Review& r2)
{
if (r1.title < r2.title)
return true;
else if (r1.title == r2.title && r1.rating < r2.rating)
return true;
else
return false;
}
bool worseThan(const Review& r1, const Review& r2)
{
if (r1.rating < r2.rating)
return true;
else
return false;
}
bool worseThan2(const Review& r1, const Review& r2)
{
if (r1.price < r2.price)
return true;
else
return false;
}
bool FillReview(Review& rr)
{
std::cout << "์ฑ
์ ๋ชฉ์ ์
๋ ฅํ์ญ์์ค(๋๋ด๋ ค๋ฉด quit): ";
std::getline(std::cin, rr.title);
if (rr.title == "quit")
return false;
std::cout << "์ฑ
๋ฑ๊ธ(0-10)์ ์
๋ ฅํ์ญ์์ค: ";
std::cin >> rr.rating;
if (!std::cin)
return false;
std::cout << "์ฑ
๊ฐ๊ฒฉ์ ์
๋ ฅํ์ญ์์ค: ";
std::cin >> rr.price;
if (!std::cin)
return false;
// ๋๋จธ์ง ์
๋ ฅ ์ค์ ์ญ์ ํ๋ค
while (std::cin.get() != '\n')
continue;
return true;
}
void ShowReview(const Review& rr)
{
std::cout << rr.rating << "\t" << rr.title <<"\t"<<rr.price <<std::endl;
}
728x90