-
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool cp(const string & s1)
{
string s2(s1);
reverse(s2.begin(),s2.end());
int cp = s1.compare(s2);
if(cp ==0)
return true;
else
return false;
}
int main()
{
cout << "Please enter a string<quit to quit>";
string s1;
cin >> s1;
while(s1 != "quit")
{
cout << cp(s1) << endl;
cout << "Please enter a string<quit to quit>";
cin >> s1;
}
cout << "BYE\n" ;
return 0;
}
-
#include<iostream>
#include<string>
#include<cctype>
#include<algorithm>
using namespace std;
string change(const string & s)
{
string s2;
for(char x :s)
if(isalpha(x))
s2+=(char)tolower(x);
return s2;
}
bool cp(const string s)
{
string s2(s);
reverse(s2.begin(),s2.end());
if(!s.compare(s2))
return true;
return false;
}
int main()
{
cout << "Please enter a sentemce:\n";
string str,str2;
getline(cin,str);
str2 = change(str);
//cout <<str2;
cout << cp(str2);
return 0;
}
-
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<cctype>
#include<vector>
#include<cstdlib>
using std::string;
int main()
{
using std::cout;
using std::cin;
using std::tolower;
using std::endl;
using std::ifstream;
// using namespace std;
std::srand(std::time(0));
char play;
cout << "Will you play a word game?<y/n>";
cin >> play;
play = tolower(play);
ifstream f;
f.open("16.txt");
if(f.is_open() == false)
{
std::cerr << "Can't open file.Bye.\n";
exit(EXIT_FAILURE);
}
string item;
std::vector<string> ve;
while(f>>item)
{
ve.push_back(item);
}
int NUM = ve.size();
while(play == 'y')
{
string target = ve[std::rand()%NUM];
int length = target.size();
string attempt(length,'-');
string badchars;
int guesses = 6;
cout << "Guess my secret word. It has " << length << " letters, and you guess\n"
<< "one letter at a time. You get " << guesses <<" wrong guesses.\n";
cout << "Your word: " << attempt << endl;
while(guesses != 0 && attempt != target)
{
char letter;
cout << "Guess a letter: ";
cin >> letter;
if(badchars.find(letter) != string::npos
|| attempt.find(letter) != string::npos)
{
cout << "You already guessed that. Try again.\n";
continue;
}
int loc = target.find(letter);
if(loc == string::npos)
{
cout << "Oh, bad guess!\n";
--guesses;
badchars += letter;
}
else
{
cout << "Good guess!\n";
attempt[loc] = letter;
loc = target.find(letter,loc+1);
while(loc != string::npos)
{
attempt[loc] = letter;
loc = target.find(letter,loc+1);
}
}
cout << "Your word: " << attempt << endl;
if(attempt != target)
{
if(badchars.length() > 0)
cout << "Bad choices: " << badchars << endl;
cout << guesses << " bad guesses left\n";
}
}//while(guesses != 0 && attempt != target)
if(guesses > 0)
cout << "That's right!\n";
else
cout << "sorry, the word is " << target << ".\n";
cout << "will you play another? <y/n>";
cin >> play;
play = tolower(play);
}//while(play == 'y')
cout << "bye\n";
return 0;
}
-
#include<iostream>
#include<cstring>
#include<list>
int reduce2(long ar[],int n)
{
std::list<long> li(ar,ar+n);
li.sort();
li.unique();
return li.size();
}
int main()
{
long ar[]={4,8,3,1,6,5,9,9,9,3,2,4,7,0,99};
std::cout << reduce2(ar,sizeof(ar)/sizeof(long));
//std::cout << sizeof(ar)/sizeof(int);
return 0;
}
-
#include<iostream>
#include<cstring>
#include<list>
template<class T>
int reduce2(T ar[],int n)
{
std::list<T> li(ar,ar+n);
li.sort();
li.unique();
for(T x:li)
std::cout << x <<" ";
std::cout << std::endl;
return li.size();
}
int main()
{
long ar[]={4,8,3,1,6,5,9,9,9,3,2,4,7,0,99};
std::cout << reduce2(ar,sizeof(ar)/sizeof(long));
std::cout << std::endl;
std::string str[]={"one","big","cat","and","the","cat","is","a","magical","cat"};
std::cout << reduce2(str,10);
return 0;
}
-
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<queue>
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;};
};
void Customer::set(long when)
{
processtime = std::rand()%3+1;
arrive = when;
}
typedef Customer Item;
bool newcustomer(double x)
{
return (std::rand()*x/RAND_MAX < 1 );
}
const int MIN_PER_HR = 60;
int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::ios_base;
using std::queue;
std::srand(std::time(0));
cout << "Case Study: Bank of Heather Automatic Teller\n";
cout << "Enter maximum size of queue: ";
int qs;
cin >> qs;
queue<Item> line;
cout << "Enter the number of simulation hours: ";
int hours;
cin >> hours;
long cyclelimit = MIN_PER_HR * hours;
cout << "Enter the average number of customers per hour: ";
double perhour;
cin >> perhour;
double 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;
long line_wait = 0;
for(int cycle = 0 ; cycle < cyclelimit ; cycle++)
{
if(newcustomer(min_per_cust))
{
if(line.size()>=qs)
turnaways++;
else
{
customers++;
temp.set(cycle);
line.push(temp);
}
}
if(wait_time<=0 && !line.empty())
{
temp = line.back();
wait_time = temp.ptime();
line.pop();
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.precision(2);
cout.setf(ios_base::fixed,ios_base::floatfield);
cout <<(double)sum_line/cyclelimit << endl;
cout << (double)line_wait/served <<" minutes\n";
}
else
cout << "No customers";
cout << "Done\n";
return 0;
}
-
//vector有一个random_shuffle
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> Lotto(const int tol,const int sel)
{
vector<int> ar;
vector<int> award;
for(int i = 0 ; i < tol ;i++)
ar.push_back(i+1);
for(int i = 0 ; i < sel ; i++)
{
random_shuffle(ar.begin(),ar.end());
award.push_back(ar[0]);
ar.erase(ar.begin());
}
return award;
}
int main()
{
vector<int> winners;
winners = Lotto(51,6);
for(int x: winners)
cout << x << " ";
return 0;
}
-
#include<iostream>
#include<string>
#include<list>
int main()
{
using std::list;
using std::cout;
using std::cin;
using std::string;
list<string> mat;
list<string> pat;
string item;
cout << "Mat, please enter names of your friends:\n";
getline(cin,item);
while(item != "" )
{
mat.push_back(item);
getline(cin,item);
}
cout << "Pat, please enter names of your friends:\n";
getline(cin,item);
while(item!="")
{
pat.push_back(item);
getline(cin,item);
}
mat.sort();
pat.sort();
mat.merge(pat);
mat.unique();
cout << "list:\n";
for(string x : mat)
cout << x << std::endl;
return 0;
}
-
#include<iostream>
#include<vector>
#include<ctime>
#include<stdlib.h>
#include<list>
#include<algorithm>
const long NUM = 100000;
int main()
{
using std::vector;
using std::list;
vector<int> vi0;
srand(time(0));
for(int i = 0 ; i < NUM ; i++ )
vi0.push_back(rand());
vector<int> vi(vi0.begin(),vi0.end());
list<int> li(vi0.begin(),vi0.end());
clock_t start = clock();
sort(vi.begin(),vi.end());
clock_t clkend = clock();
std::cout << (double)(clkend - start)/CLOCKS_PER_SEC << std::endl;
start = clock();
li.sort();
clkend = clock();
std::cout << (double)(clkend - start)/CLOCKS_PER_SEC << std::endl;
start = clock();
li.assign(vi0.begin(),vi0.end());
vi.assign(li.begin(),li.end());
sort(vi.begin(),vi.end());
li.assign(vi.begin(),vi.end());
clkend = clock();
std::cout << (double)(clkend - start)/CLOCKS_PER_SEC << std::endl;
return 0;
}
网友评论