2016 题目
- 用键盘输入一行文本,如that is a question,请编写一个test函数,将输入文本的单词和对应的单词长度输入数组中,并像一下方式输出
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin,s);
string word;
while (!s.empty()) {
if (s.find(" ") == s.npos) {
word = s.substr(0);
cout << setw(20) << left << word << setw(8) << left << word.length() << endl;
break;
}
word = s.substr(0,s.find(" "));
cout << setw(20) << left << word << setw(8) << left << word.length() << endl;
s.erase(0,s.find(" ") + 1);
}
return 0;
}
```c
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main(){
string s;
getline(cin,s);
string word;
size_t lastPos = 0;
cout << setw(6) << "word" << setw(8) << "length" << endl;
do {
word = s.substr(lastPos,s.find(" ",lastPos) - lastPos); //substr(pos,num);
cout << setw(6) << word << setw(8) << word.length() << endl;
lastPos = s.find(" ",lastPos);
} while (lastPos++ != s.npos);
return 0;
}
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char s[100];
cin.getline(s,100);
//gets(s);
char *word = strtok(s," ");
while (word) {
cout << setw(20) << left << word << setw(3) << left << strlen(word) <<endl;
word = strtok(NULL, " ");
}
return 0;
}
2017 题目
- 使用字符数组存储一段字符,判断是否是回文字符串(需要判断并忽略空格)
#include <iostream>
#include <cstring>
using namespace std;
bool huiwen(char []);
int main()
{
char s[100];
cin.getline(s,100);
if(huiwen(s))
cout << "true" << endl;
else
cout << "no" << endl;
return 0;
}
bool huiwen(char s[])
{
int len = strlen(s);
int left = 0;
int right = len - 1;
while(left < right )
{
if(s[left] == ' ')
left++;
if(s[right] == ' ')
right--;
if(s[left]!=s[right])
return false;
left++;
right--;
}
return true;
}
递归:
bool testPalindrome( char a[], int size )
{
if(a[0]==' ')
return selectionSort(&a[1],size - 1);
if(a[size - 1] == ' ')
return selectionSort(a,size - 1);
if(a[0] != a[size-1])
return false;
return true;
}
官方:
bool testPalindrome( string palindrome, int left, int right )
{
// test array to see if a palindrome
if ( left == right || left > right )
return true;
else if(palindrome[ left ] == ' ')
return testPalindrome( palindrome, left + 1, right);
else if(palindrome[ right ] == ' ')
testPalindrome( palindrome, left, right - 1 );
else if ( palindrome[ left ] != palindrome[ right ] )
return false;
else
return testPalindrome( palindrome, left + 1, right - 1 );
}
- 格式转换,从一个文件中读取日期07/21/2016,转换为以下格式July 21,2016并输出到屏幕上
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
static const string months[13] =
{
"","January","February","March","April","May","June","July","August","September"
,"October","November","December"
};
ifstream in("input.txt",ios::in);
char s[100];
in >> s;
int month,year,day;
char ch;
istringstream input(s);
input >> month >> ch >> day >> ch >> year;
cout << months[month] << " " << day << "," << year << endl;
return 0;
}
- 从文件读入一个字符串,对其进行奇偶排序,使字母都按顺序排列。
奇偶排序:每个奇数的字符开始,依次比较,若a[i-1]>a[i]或者a[i]>a[i+1]则交换;再从偶数的字符开始,如此循环往复,直至交换完毕。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//文件读入字符串,奇偶排序
int main()
{
ifstream ifs("input.txt",ios::in);
if(!ifs)
{
cerr << "File is cannot opened!!!" << endl;
exit(1);
}
string str;
ifs >> str;
cout << str << endl;
bool flag1,flag2;
do{
flag1 =true;
flag2 = true;
for(int i = 1; i < str.length() - 1; i += 2)
{
if(tolower(str[i]) > tolower(str[i + 1]))
{
swap(str[i],str[i + 1]);
flag1 = false;
}
}
for(int j = 0; j < str.length();j += 2)
{
if(tolower(str[j]) > tolower(str[j+1]))
{
swap(str[j],str[j+1]);
flag2 = false;
}
}
}while(flag1 == false || flag2 == false);
//当奇偶排序都不在交换说明已经有序。
cout << str << endl;
return 0;
}
2018 题目
- 寻找1-1000中所有满足以下条件的数:所有因子(包括1但不包括本身)之和等于这个数本身。并将所有这样的数输出,格式如下:6=1+2+3【10分】(完数)
#include <iostream>
using namespace std;
bool perfectNumber(int);
void printNumber(int);
int main()
{
for(int i = 2; i <= 1000; i++)
if(perfectNumber(i))
printNumber(i);
return 0;
}
bool perfectNumber(int num)
{
int sum = 1;
int i = 2;
while(i <= num/2)
{
if(num%i == 0)
sum+=i;
i++;
}
if(sum == num)
return true;
else
return false;
}
void printNumber(int num)
{
cout << num << " = 1";
int i = 2;
while(i <= num/2)
{
if(num%i == 0)
cout << " + " << i ;
i++;
}
cout << endl;
}
- 在10-100000之间随机产生10个数,放到int数组中,再把这个数组中的每个元素都转化成字符串存到字符指针数组中,fun2实现把一个整数转化成一个字符串【15分】
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <cstring>
using namespace std;
void fun2(int [],char [],int);
string fun1(int );
int main()
{
int a[10];
char str[10][10];
srand(time(0));
for(int i = 0; i < 10; i++){
a[i] = (rand()%(1000-10+1)) + 10;
cout << a[i] << " ";
strcpy(&str[i][0],fun1(a[i]).c_str()); //func2也行
}
cout << endl;
for(int i = 0; i < 10; i++)
cout << &str[i][0] << " ";
return 0;
}
void fun2(int a[],char str[], int len)
{
string s= "";
for(int i = 0; i < 10; i++)
{
s= s + to_string(a[i]);
}
strcpy(str,s.c_str());
}
string fun1(int num)
{
return to_string(num);
}
- 写一个职工类,其中私有数据成员包括职工姓名name,职工号id,入职年月rznf,年度评分score(每年每个员工会得到一个分,是4-9之中的整数值,当得到第三个评分后,会判断是否加薪或辞退,然后清零这三个评分),员工月薪wages员工总数Total。成员函数有构造函数,析构函数,判断是否加薪(连续3年评分都大于等于9,则加薪10%),判断是否辞退(连续3年评分小于等于6,辞退)。再写一个友元函数,用来计算该公司每月应付职工薪酬,和年应付职工薪酬。【20分】
Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
class Employee
{
public:
Employee(string = "",int = 000,int = 201001,double = 3000.0);
virtual ~Employee();
void setScore(int);
bool judgeRaise();
bool judgeFire();
friend void cal(Employee &);
private:
string name;
int id;
int rznf;
int score[3];
double wages;
static double total;
static double sum;
};
#endif // EMPLOYEE_H
Employee.cpp
#include "Employee.h"
#include <iostream>
#include <string>
using namespace std;
double Employee::total = 0;
double Employee::sum = 0;
Employee::Employee(string tname,int tid,int trznf,double twages)
:name(tname),id(tid),rznf(trznf),wages(twages)
{
for(int i = 0; i < 3; i++)
score[i] = 0;
Employee::total++;
sum += wages;
}
void Employee::setScore(int s)
{
if(s < 4 && s > 9)
{
cerr << "set score out of range!!! set to 4";
s = 4;
}
for(int i = 0; i < 3; i++)
{
if(score[i]==0)
{
score[i] = s;
break;
}
}
if(score[2]) //第三次评分判断离职或者加薪
{
if(judgeRaise())
{
for(int i = 0; i < 3; i++)
score[i] = 0;
sum -= wages;
wages = wages * 1.1;
sum += wages;
cout << name << " : wages raise!!! " << endl;
}
else if(judgeFire())
{
sum = sum - wages;
total --;
cout << name << " : dismiss!" << endl;
}
else
{
cout << name << " : keep hire!" << endl;
}
}
}
bool Employee::judgeRaise()
{
for(int i = 0; i < 3; i ++)
if(score[i] < 9) //有一次小于9的不加工资
return false;
return true;
}
bool Employee::judgeFire()
{
for(int i = 0; i < 3; i ++)
if(score[i] > 6) //有一次超过6评分的,不解雇
return false;
return true;
}
void cal(Employee &e)
{
cout << "employee numbers: " << Employee::total << endl;
cout << "Per month : " << Employee::sum << endl;
cout << "Per year : " << Employee::sum *12 << endl;
}
const int* Employee::getScore()const
{
return score;
}
Employee::~Employee()
{
}
main.cpp
#include <iostream>
#include "Employee.h"
using namespace std;
int main()
{
Employee e1("Van",001,201002,5000);
cal(e1);
cout << endl;
Employee e2("Jone",002,201001,8000);
cal(e1);
cout << endl;
Employee e3("Snow",003,201309.9000);
cal(e1);
cout << endl;
e1.setScore(9);
e1.setScore(9);
e1.setScore(9);
cout << endl;
cal(e2);
cout << endl;
e2.setScore(6);
e2.setScore(7);
e2.setScore(4);
cout << endl;
cal(e2);
cout << endl;
e3.setScore(6);
e3.setScore(6);
e3.setScore(6);
cout << endl;
cal(e3);
cout << endl;
const int *p = e3.getScore();
for(int i = 0; i < 3;i++)
cout << p[i] << " ";
return 0;
}