├── 014:MyString.cpp ├── 018:别叫,这个大整数已经很简化了.cpp ├── 4.6.cpp ├── 4.7.cpp ├── 8.2.cpp ├── 8.3.cpp ├── 8.4.cpp ├── MOOC ├── 001:简单的swap.cpp ├── 002:难一点的swap.cpp ├── 003:好怪异的返回值.cpp ├── 004:神秘的数组初始化.cpp ├── 005:编程填空:学生信息处理程序.cpp ├── 006:奇怪的类复制.cpp ├── 007:返回什么才好呢.cpp ├── 008:超简单的复数类.cpp ├── 009:哪来的输出.cpp ├── 010:返回什么才好呢.cpp ├── 011:Big & Base 封闭类问题.cpp ├── 012:这个指针哪来的.cpp ├── 013:魔兽世界之一:备战.cpp ├── 014:MyString.cpp ├── 015:看上去好坑的运算符重载.cpp ├── 016:惊呆!Point竟然能这样输入输出.cpp ├── 017:第四周程序填空题3.cpp ├── 018:别叫,这个大整数已经很简化了!.cpp ├── 019:编程填空:统计动物数量.cpp ├── 020:全面的MyString.cpp ├── 021:继承自string的MyString.cpp ├── 022:魔兽世界之二:装备.cpp ├── 023:看上去像多态.cpp ├── 024:Fun和Do.cpp ├── 025:这是什么鬼delete.cpp ├── 026:怎么又是Fun和Do.cpp ├── 027:简单的SumArray.cpp ├── 028:简单的foreach.cpp ├── 029:简单的Filter.cpp ├── 030:你真的搞清楚为啥 while(cin >> n) 能成立了吗?.cpp ├── 031:山寨版istream_iterator.cpp ├── 032:这个模板并不难.cpp ├── 033:排序,又见排序!.cpp ├── 034:goodcopy.cpp ├── 035:按距离排序.cpp ├── 036:很难蒙混过关的CArray3d三维数组模板类.cpp ├── 037:函数对象的过滤器.cpp ├── 038:白给的list排序.cpp ├── 039:我自己的 ostream_iterator.cpp ├── 040:List.cpp ├── 041:Set.cpp ├── 042:热血格斗场.cpp ├── 043:冷血格斗场.cpp ├── 044:编程填空:数据库内的学生信息 .cpp └── 045:魔兽世界三(开战).cpp ├── README.md └── n皇后.cpp /014:MyString.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************ 2 | 补足MyString类,使程序输出指定结果 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | class MyString { 8 | char * p; 9 | public: 10 | MyString(const char * s) { 11 | if( s) { 12 | p = new char[strlen(s) + 1]; 13 | strcpy(p,s); 14 | } 15 | else 16 | p = NULL; 17 | 18 | } 19 | ~MyString() { if(p) delete [] p; } 20 | // 在此处补充你的代码 21 | }; 22 | int main() 23 | { 24 | char w1[200],w2[100]; 25 | while( cin >> w1 >> w2) { 26 | MyString s1(w1),s2 = s1; 27 | MyString s3(NULL); 28 | s3.Copy(w1); 29 | cout << s1 << "," << s2 << "," << s3 << endl; 30 | 31 | s2 = w2; 32 | s3 = s2; 33 | s1 = s3; 34 | cout << s1 << "," << s2 << "," << s3 << endl; 35 | 36 | } 37 | } 38 | 39 | 输入 40 | 多组数据,每组一行,是两个不带空格的字符串 41 | 输出 42 | 对每组数据,先输出一行,打印输入中的第一个字符串三次 43 | 然后再输出一行,打印输入中的第二个字符串三次 44 | 45 | 样例输入 46 | abc def 47 | 123 456 48 | 49 | 样例输出 50 | abc,abc,abc 51 | def,def,def 52 | 123,123,123 53 | 456,456,456 54 | ************************************************************************/ 55 | #include 56 | #include 57 | #include 58 | using namespace std; 59 | class MyString { 60 | char * p; 61 | public: 62 | MyString(const char * s) { 63 | if( s) { 64 | p = new char[strlen(s) + 1]; 65 | strcpy(p,s); 66 | } 67 | else 68 | p = NULL; 69 | 70 | } 71 | ~MyString() { if(p) delete [] p; } 72 | void Copy(const char *s) 73 | { 74 | if(p) delete p; 75 | p = new char[strlen(s) + 1]; 76 | strcpy(p,s); 77 | } 78 | MyString & operator=(const MyString & s) 79 | { 80 | //if(strlen(p)> w1 >> w2) { 109 | MyString s1(w1),s2 = s1; 110 | MyString s3(NULL); 111 | s3.Copy(w1); 112 | cout << s1 << "," << s2 << "," << s3 << endl; 113 | 114 | s2 = w2; 115 | s3 = s2; 116 | s1 = s3; 117 | cout << s1 << "," << s2 << "," << s3 << endl; 118 | 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /018:别叫,这个大整数已经很简化了.cpp: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | 018:别叫,这个大整数已经很简化了! 3 | 4 | 输入 5 | 多组数据,每组数据是两个非负整数s和 n。s最多可能200位, n用int能表示 6 | 输出 7 | 对每组数据,输出6行,内容分别是: 8 | 9 | 样例输入 10 | 99999999999999999999999999888888888888888812345678901234567789 12 11 | 6 6 12 | 13 | 样例输出 14 | 99999999999999999999999999888888888888888812345678901234567801 15 | 99999999999999999999999999888888888888888812345678901234567801 16 | 99999999999999999999999999888888888888888812345678901234567801 17 | 25 18 | 25 19 | 26 20 | 12 21 | 12 22 | 12 23 | 13 24 | 13 25 | 14 26 | ***********************************************************************/ 27 | /************************************dev c++****************************************/ 28 | #include 29 | #include 30 | #include 31 | #include 32 | using namespace std; 33 | const int MAX = 110; 34 | class CHugeInt{ 35 | private: 36 | char buf[220];//固定一个220位大小的数组,便于进行对齐 37 | public: 38 | void reverse(char *p)//将存储数据的数组进行逆序操作,符合竖式计算从低位向高位进行的原理 39 | { 40 | int len = strlen(p); 41 | int i = 0, j = len - 1; 42 | while (i < j) 43 | { 44 | swap(p[i], p[j]); 45 | ++i; 46 | --j; 47 | } 48 | } 49 | CHugeInt(char *p) 50 | { 51 | memset(buf, 0, sizeof(buf));//将buf初始化 52 | strcpy(buf, p); 53 | reverse(buf); 54 | } 55 | CHugeInt(int n) 56 | { 57 | memset(buf, 0, sizeof(buf)); 58 | sprintf(buf, "%d", n); 59 | reverse(buf); 60 | } 61 | CHugeInt operator+(int n) 62 | { 63 | return *this + CHugeInt(n);//可以直接利用后面写的重载运算符 64 | } 65 | CHugeInt operator+(const CHugeInt & n) 66 | { 67 | CHugeInt tmp(0); 68 | int carry = 0;//进位 69 | for (int i = 0; i < 210; i++) 70 | { 71 | char c1 = buf[i]; 72 | char c2 = n.buf[i]; 73 | if (c1 == 0 && c2 == 0 && carry == 0)break; 74 | if (c1 == 0)c1 = '0'; 75 | if (c2 == 0)c2 = '0'; 76 | int k = c1 - '0' + c2 - '0' + carry; 77 | if (k >= 10)//相加大于10则进位 78 | { 79 | carry = 1;//进位置1 80 | tmp.buf[i] = k - 10 + '0'; 81 | } 82 | else 83 | { 84 | carry = 0; 85 | tmp.buf[i] = k + '0'; 86 | } 87 | } 88 | return tmp; 89 | } 90 | friend CHugeInt operator+(int n, CHugeInt & h) 91 | { 92 | return h + n; 93 | } 94 | friend ostream & operator<<(ostream & os, const CHugeInt & h) 95 | { 96 | int len = strlen(h.buf); 97 | for (int i = len - 1; i >= 0; i--) 98 | { 99 | cout << h.buf[i]; 100 | } 101 | return os; 102 | } 103 | CHugeInt & operator++() 104 | { 105 | *this = *this + 1; 106 | return *this; 107 | } 108 | CHugeInt operator++(int) 109 | { 110 | CHugeInt tmp(*this); 111 | *this = *this + 1; 112 | return tmp; 113 | } 114 | CHugeInt & operator+=(int n) 115 | { 116 | *this = *this + n; 117 | return *this; 118 | } 119 | }; 120 | int main() 121 | { 122 | char s[210]; 123 | int n; 124 | 125 | while (cin >> s >> n) 126 | { 127 | CHugeInt a(s); 128 | CHugeInt b(n); 129 | 130 | cout << a + b << endl; 131 | cout << n + a << endl; 132 | cout << a + n << endl; 133 | b += n; 134 | cout << ++b << endl; 135 | cout << b++ << endl; 136 | cout << b << endl; 137 | } 138 | return 0; 139 | } 140 | /*********************************visual studio 2013**********************************/ 141 | #include 142 | #include 143 | #include 144 | #include 145 | using namespace std; 146 | const int MAX = 110; 147 | class CHugeInt{ 148 | private: 149 | char buf[220];//固定一个220位大小的数组,便于进行对齐 150 | public: 151 | void reverse(char *p)//将存储数据的数组进行逆序操作,符合竖式计算从低位向高位进行的原理 152 | { 153 | int len = strlen(p); 154 | int i = 0, j = len - 1; 155 | while (i < j) 156 | { 157 | swap(p[i], p[j]); 158 | ++i; 159 | --j; 160 | } 161 | } 162 | CHugeInt(char *p) 163 | { 164 | memset(buf, 0, sizeof(buf));//将buf初始化 165 | //strcpy_s(buf, sizeof(buf), p); //strcpy(buf, p); 166 | /*不能使用strcpy_s(buf, sizeof(buf), p)来拷贝,因为经过memset将buf所有元素设为0后, 167 | 使用strcpy_s(buf, sizeof(buf), p)来复制p的内容,调试发现应该保持为0的元素都变成了-2, 168 | 导致后面+运算错误。改为使用strcpy_s(buf, strlen(p)+1, p),后正确。*/ 169 | strcpy_s(buf, strlen(p) + 1, p);//只拷贝p长度内的字符 170 | reverse(buf); 171 | } 172 | CHugeInt(int n) 173 | { 174 | memset(buf, 0, sizeof(buf)); 175 | //sprintf_s(buf, sizeof(buf), "%d", n); //sprintf(buf, "%d", n); 176 | /*使用sprintf_s(buf, sizeof(buf), "%d", n)和使用strcpy_s(buf, sizeof(buf), p)有同样的问题。*/ 177 | #if 0 178 | char t[11]; 179 | sprintf_s(t, sizeof(t), "%d", n);//执行该语句后,strlen(t)正好是n的位数 180 | strcpy_s(buf, strlen(t) + 1, t);//只拷贝n位数内的字符 181 | #else 182 | int intlen = 0, t = n; 183 | while (t) 184 | { 185 | t /= 10; 186 | intlen++; //n是几位数 187 | } 188 | if (intlen>0) sprintf_s(buf, intlen+1, "%d", n); //intlen=0,即n=0,保持buf初始化即可,不需要执行sprintf_s。 189 | #endif 190 | reverse(buf); 191 | } 192 | CHugeInt operator+(int n) 193 | { 194 | return *this + CHugeInt(n);//可以直接利用后面写的重载运算符 195 | } 196 | CHugeInt operator+(const CHugeInt & n) 197 | { 198 | CHugeInt tmp(0); 199 | int carry = 0;//进位 200 | int i; 201 | for (i = 0; i < 210; i++) 202 | { 203 | char c1 = buf[i]; 204 | char c2 = n.buf[i]; 205 | if (c1 == 0 && c2 == 0 && carry == 0)break; 206 | if (c1 == 0)c1 = '0'; 207 | if (c2 == 0)c2 = '0'; 208 | int k = c1 - '0' + c2 - '0' + carry; 209 | if (k >= 10)//相加大于10则进位 210 | { 211 | carry = 1;//进位置1 212 | tmp.buf[i] = k - 10 + '0'; 213 | } 214 | else 215 | { 216 | carry = 0; 217 | tmp.buf[i] = k + '0'; 218 | } 219 | } 220 | return tmp; 221 | } 222 | friend CHugeInt operator+(int n, CHugeInt & h) 223 | { 224 | return h + n; 225 | } 226 | friend ostream & operator<<(ostream & os, const CHugeInt & h) 227 | { 228 | int len = strlen(h.buf); 229 | for (int i = len - 1; i >= 0; i--) 230 | { 231 | cout << h.buf[i]; 232 | } 233 | return os; 234 | } 235 | CHugeInt & operator++() 236 | { 237 | *this = *this + 1; 238 | return *this; 239 | } 240 | CHugeInt operator++(int) 241 | { 242 | CHugeInt tmp(*this); 243 | *this = *this + 1; 244 | return tmp; 245 | } 246 | CHugeInt & operator+=(int n) 247 | { 248 | *this = *this + n; 249 | return *this; 250 | } 251 | }; 252 | int main() 253 | { 254 | char s[210]; 255 | int n; 256 | 257 | while (cin >> s >> n) 258 | { 259 | CHugeInt a(s); 260 | CHugeInt b(n); 261 | 262 | cout << a + b << endl; 263 | cout << n + a << endl; 264 | cout << a + n << endl; 265 | b += n; 266 | cout << ++b << endl; 267 | cout << b++ << endl; 268 | cout << b << endl; 269 | } 270 | return 0; 271 | } 272 | -------------------------------------------------------------------------------- /4.6.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | 第四章 运算符重载 3 | 习题 4 | 6.编写一个二维数组类Array2,使得程序的输出结果是 5 | 0,1,2,3, 6 | 4,5,6,7, 7 | 8,9,10,11, 8 | next 9 | 0,1,2,3, 10 | 4,5,6,7, 11 | 8,9,10,11, 12 | 13 | #include 14 | using namespace std; 15 | int main() 16 | { 17 | Array2 a(3, 4); 18 | int i, j; 19 | for (i = 0; i < 3; ++i) 20 | for (j = 0; j < 4; j++) 21 | a[i][j] = i * 4 + j; //等价于a.operator[](i)[j] = i * 4 + j; 22 | for (i = 0; i < 3; ++i) { 23 | for (j = 0; j < 4; j++) { 24 | cout << a(i, j) << ","; 25 | } 26 | cout << endl; 27 | } 28 | cout << "next" << endl; 29 | Array2 b; 30 | b = a; 31 | for (i = 0; i < 3; ++i) { 32 | for (j = 0; j < 4; j++) { 33 | cout << b[i][j] << ","; 34 | } 35 | cout << endl; 36 | } 37 | return 0; 38 | } 39 | ***********************************************************/ 40 | 41 | #include 42 | using namespace std; 43 | #if 1 //one funtion 44 | class Array2{ 45 | int row, col; 46 | int **ptr; 47 | public: 48 | Array2(int s1 = 0, int s2 = 0) :row(s1), col(s2) 49 | { 50 | if (s1 == 0 && s2 == 0) ptr = NULL; 51 | //else if (s1 == 0 && s2 > 0) ptr = new int[s2]; 52 | //else if (s1 > 0 && s2 == 0) ptr = new int[s1]; 53 | else 54 | { 55 | ptr = new int*[s1]; 56 | for (int i = 0; i < s1; i++) 57 | ptr[i] = new int[s2]; 58 | } 59 | } 60 | ~Array2() 61 | { 62 | if (ptr != NULL) delete[] ptr; 63 | } 64 | Array2 & operator=(const Array2 & a) 65 | { 66 | if (ptr == a.ptr)return *this; 67 | if (a.ptr == NULL) 68 | { 69 | if (ptr) delete[] ptr; 70 | row = 0; 71 | col = 0; 72 | ptr = NULL; 73 | return *this; 74 | } 75 | if (row 25 | #include 26 | #include 27 | using namespace std; 28 | class MyString{ 29 | char * str; 30 | public: 31 | MyString() 32 | { 33 | //str=NULL; 不能这样,否则,会导致cout<str) + strlen(s.str)+1]; 76 | strcpy(sTmp.str, this->str); 77 | strcat(sTmp.str, s.str); 78 | return sTmp; 79 | } 80 | MyString operator+(const char * s) 81 | { 82 | #if 0 83 | MyString sTmp(*this); 84 | if (sTmp.str) delete[] sTmp.str; 85 | sTmp.str = new char[strlen(this->str) + strlen(s)+1]; 86 | strcpy(sTmp.str, this->str); 87 | strcat(sTmp.str, s); 88 | return sTmp; 89 | #else //简化 90 | return *this+ MyString(s); 91 | #endif 92 | } 93 | MyString & operator+=(const char * s) 94 | { 95 | #if 0 96 | char * tmp; 97 | tmp = new char[strlen(str) + 1]; 98 | strcpy(tmp, str); 99 | if (str) delete[] str; 100 | str = new char[strlen(tmp) + strlen(s)+1]; 101 | strcpy(str, tmp); 102 | strcat(str, s); 103 | #else //简化 104 | *this = *this + MyString(s); 105 | #endif 106 | return *this; 107 | } 108 | friend MyString operator+(const char * s, const MyString & st); 109 | friend ostream & operator<<(ostream & os, const MyString & s); 110 | //返回引用,可以作为左值,修改其值,例如 s1[2] = 'A'; 111 | char & operator[](int i) 112 | { 113 | return str[i]; 114 | } 115 | char * operator()(int st, int sp) 116 | { 117 | char * ss; 118 | ss = str; 119 | ss[st+sp] = '\0'; 120 | return &ss[st]; 121 | } 122 | friend bool operator<(const MyString & s1, const MyString & s2); 123 | friend bool operator==(const MyString & s1, const MyString & s2); 124 | friend bool operator>(const MyString & s1, const MyString & s2); 125 | }; 126 | MyString operator+(const char * s, const MyString & st) 127 | { 128 | #if 0 129 | MyString ss; 130 | ss.str = new char[strlen(s) + strlen(st.str)+1]; 131 | strcpy(ss.str, s); 132 | strcat(ss.str, st.str); 133 | return ss; 134 | #else 135 | return st+MyString(s); 136 | #endif 137 | } 138 | ostream & operator<<(ostream & os, const MyString & s) 139 | { 140 | os << s.str; 141 | return os; 142 | } 143 | bool operator<(const MyString & s1, const MyString & s2) 144 | { 145 | int t; 146 | t = strcmp(s1.str, s2.str); 147 | if (t < 0) return true; 148 | else return false; 149 | } 150 | bool operator==(const MyString & s1, const MyString & s2) 151 | { 152 | int t; 153 | t = strcmp(s1.str, s2.str); 154 | if (t == 0) return true; 155 | else return false; 156 | } 157 | bool operator>(const MyString & s1, const MyString & s2) 158 | { 159 | int t; 160 | t = strcmp(s1.str, s2.str); 161 | if (t > 0) return true; 162 | else return false; 163 | } 164 | int CompareString(const void *e1, const void *e2) 165 | { 166 | MyString *s1 = (MyString *)e1; 167 | MyString *s2 = (MyString *)e2; 168 | if (*s1 < *s2) return -1; 169 | else if (*s1 == *s2) return 0; 170 | else if (*s1 > *s2) return 1; 171 | } 172 | int main() 173 | { 174 | MyString s1("abcd-"), s2, s3("efgh-"), s4(s1); 175 | MyString SArray[4] = { "big", "me", "about", "take" }; 176 | cout << "1." << s1 << s2 << s3 << s4 << endl; 177 | s4 = s3; 178 | s3 = s1 + s3; 179 | cout << "2." << s1 << endl; 180 | cout << "3." << s2 << endl; 181 | cout << "4." << s3 << endl; 182 | cout << "5." << s4 << endl; 183 | cout << "6." << s1[2] << endl; 184 | s2 = s1; 185 | s1 = "ijkl-"; 186 | s1[2] = 'A'; 187 | cout << "7." << s2 << endl; 188 | cout << "8." << s1 << endl; 189 | s1 += "mnop"; 190 | cout << "9." << s1 << endl; 191 | s4 = "qrst-" + s2; 192 | cout << "10." << s4 << endl; 193 | s1 = s2 + s4 + "uvw" + "xyz"; 194 | cout << "11." << s1 << endl; 195 | qsort(SArray, 4, sizeof(MyString), CompareString); 196 | for (int i = 0; i < 4; ++i) 197 | cout << SArray[i] << endl; 198 | //输出s1从下标为0开始长度为4的字串 199 | cout << s1(0, 4) << endl; 200 | //输出s1从下标为5开始长度为10的字串 201 | cout << s1(5, 10) << endl; 202 | return 0; 203 | } 204 | /*********************************visual studio 2013**************************************************/ 205 | #include 206 | #include 207 | #include 208 | using namespace std; 209 | class MyString{ 210 | char * str; 211 | public: 212 | MyString() 213 | { 214 | //str = NULL; 215 | str = new char[1]; 216 | *str = '\0'; 217 | } 218 | MyString(const char * s) 219 | { 220 | str = new char[strlen(s) + 1]; 221 | strcpy_s(str, strlen(s)+1, s);// strcpy(str, s); 222 | } 223 | MyString(const MyString & s) 224 | { 225 | str = new char[strlen(s.str)+1]; 226 | strcpy_s(str, strlen(s.str)+1, s.str);//strcpy(str, s.str); 227 | } 228 | ~MyString() 229 | { 230 | if (str) delete[] str; 231 | } 232 | MyString & operator=(const MyString & s) 233 | { 234 | if (str == s.str) return *this; 235 | if (str) delete[] str; 236 | str = new char[strlen(s.str)+1]; 237 | strcpy_s(str, strlen(s.str)+1, s.str);//strcpy(str, s.str); 238 | return *this; 239 | } 240 | MyString & operator=(const char * s) 241 | { 242 | #if 0 243 | if (str) delete[] str; 244 | str = new char[strlen(s)+1]; 245 | strcpy_s(str, strlen(s)+1, s);//strcpy(str, s); 246 | return *this; 247 | #else 248 | return *this = MyString(s); 249 | #endif 250 | } 251 | MyString operator+(const MyString & s) 252 | { 253 | MyString sTmp(*this); 254 | if (sTmp.str) delete[] sTmp.str; 255 | sTmp.str = new char[strlen(this->str) + strlen(s.str) + 1]; 256 | strcpy_s(sTmp.str, strlen(this->str) + 1, this->str);//strcpy(sTmp.str, this->str); 257 | strcat_s(sTmp.str, strlen(this->str) + strlen(s.str) + 1, s.str); //strcat(sTmp.str, s.str); 258 | return sTmp; 259 | } 260 | MyString operator+(const char * s) 261 | { 262 | #if 0 263 | MyString sTmp(*this); 264 | if (sTmp.str) delete[] sTmp.str; 265 | sTmp.str = new char[strlen(this->str) + strlen(s) + 1]; 266 | strcpy_s(sTmp.str, strlen(this->str) + 1, this->str);//strcpy(sTmp.str, this->str); 267 | strcat_s(sTmp.str, strlen(this->str) + strlen(s) + 1, s); //strcat(sTmp.str, s); 268 | return sTmp; 269 | #else 270 | return *this + MyString(s); 271 | #endif 272 | } 273 | MyString & operator+=(const char * s) 274 | { 275 | #if 0 276 | char * tmp; 277 | tmp = new char[strlen(str) + 1]; 278 | strcpy_s(tmp, strlen(str) + 1, str);//strcpy(tmp, str); 279 | if (str) delete[] str; 280 | str = new char[strlen(tmp) + strlen(s)+1]; 281 | strcpy_s(str, strlen(tmp)+1, tmp);//strcpy(str, tmp); 282 | strcat_s(str, strlen(str) + strlen(s)+1, s); //strcat(str, s); //strncat(str, s, strlen(s)); 283 | return *this; 284 | #else 285 | return *this = *this + MyString(s); 286 | #endif 287 | } 288 | friend MyString operator+(const char * s, const MyString & st); 289 | friend ostream & operator<<(ostream & os, const MyString & s); 290 | char & operator[](int i) 291 | { 292 | return str[i]; 293 | } 294 | char * operator()(int st, int sp) 295 | { 296 | char * ss; 297 | ss = str; 298 | ss[st+sp] = '\0'; 299 | return &ss[st]; 300 | } 301 | friend bool operator<(const MyString & s1, const MyString & s2); 302 | friend bool operator==(const MyString & s1, const MyString & s2); 303 | friend bool operator>(const MyString & s1, const MyString & s2); 304 | }; 305 | MyString operator+(const char * s, const MyString & st) 306 | { 307 | #if 0 308 | MyString ss; 309 | ss.str = new char[strlen(s) + strlen(st.str)+1]; 310 | strcpy_s(ss.str, strlen(s)+1, s);//strcpy(ss.str, s); 311 | strcat_s(ss.str, strlen(ss.str) + strlen(st.str)+1, st.str); //strcat(ss.str, st.str); //strncat(ss.str, st.str, strlen(st.str)); 312 | return ss; 313 | #else 314 | return st + MyString(s); 315 | #endif 316 | } 317 | ostream & operator<<(ostream & os, const MyString & s) 318 | { 319 | os << s.str; 320 | return os; 321 | } 322 | bool operator<(const MyString & s1, const MyString & s2) 323 | { 324 | int t; 325 | t = strcmp(s1.str, s2.str); 326 | if (t < 0) return true; 327 | else return false; 328 | } 329 | bool operator==(const MyString & s1, const MyString & s2) 330 | { 331 | int t; 332 | t = strcmp(s1.str, s2.str); 333 | if (t == 0) return true; 334 | else return false; 335 | } 336 | bool operator>(const MyString & s1, const MyString & s2) 337 | { 338 | int t; 339 | t = strcmp(s1.str, s2.str); 340 | if (t > 0) return true; 341 | else return false; 342 | } 343 | int CompareString(const void *e1, const void *e2) 344 | { 345 | MyString *s1 = (MyString *)e1; 346 | MyString *s2 = (MyString *)e2; 347 | if (*s1 < *s2) return -1; 348 | else if (*s1 == *s2) return 0; 349 | else if (*s1 > *s2) return 1; 350 | } 351 | int main() 352 | { 353 | MyString s1("abcd-"), s2, s3("efgh-"), s4(s1); 354 | MyString SArray[4] = { "big", "me", "about", "take" }; 355 | cout << "1." << s1 << s2 << s3 << s4 << endl; 356 | s4 = s3; 357 | s3 = s1 + s3; 358 | cout << "2." << s1 << endl; 359 | cout << "3." << s2 << endl; 360 | cout << "4." << s3 << endl; 361 | cout << "5." << s4 << endl; 362 | cout << "6." << s1[2] << endl; 363 | s2 = s1; 364 | s1 = "ijkl-"; 365 | s1[2] = 'A'; 366 | cout << "7." << s2 << endl; 367 | cout << "8." << s1 << endl; 368 | s1 += "mnop"; 369 | cout << "9." << s1 << endl; 370 | s4 = "qrst-" + s2; 371 | cout << "10." << s4 << endl; 372 | s1 = s2 + s4 + "uvw" + "xyz"; 373 | cout << "11." << s1 << endl; 374 | qsort(SArray, 4, sizeof(MyString), CompareString); 375 | for (int i = 0; i < 4; ++i) 376 | cout << SArray[i] << endl; 377 | //输出s1从下标为0开始长度为4的字串 378 | cout << s1(0, 4) << endl; 379 | //输出s1从下标为5开始长度为10的字串 380 | cout << s1(5, 10) << endl; 381 | return 0; 382 | } 383 | /******************************************使用string类,复合关系(封闭类):DEV C++/visual studio***********************************************/ 384 | #include 385 | #include 386 | #include 387 | #include 388 | using namespace std; 389 | class MyString{ 390 | string str; //封闭类 391 | public: 392 | MyString(){ } 393 | MyString(const char * s) 394 | { 395 | str = s; 396 | } 397 | MyString(const MyString & s) 398 | { 399 | str = s.str; 400 | } 401 | MyString & operator=(const MyString & s) 402 | { 403 | str.assign(s.str, 0, s.str.length() + 1); 404 | return *this; 405 | } 406 | MyString & operator=(const char * s) 407 | { 408 | str.assign(s); 409 | return *this; 410 | } 411 | MyString operator+(const MyString & s) 412 | { 413 | MyString sTmp(*this); 414 | sTmp.str += s.str; 415 | return sTmp; 416 | } 417 | MyString operator+(const char * s) 418 | { 419 | MyString sTmp(*this); 420 | sTmp.str += s; 421 | return sTmp; 422 | } 423 | MyString & operator+=(const char * s) 424 | { 425 | str += s; 426 | return *this; 427 | } 428 | friend MyString operator+(const char * s, const MyString & st); 429 | friend ostream & operator<<(ostream & os, const MyString & s); 430 | char & operator[](int i) 431 | { 432 | return str[i]; 433 | } 434 | string operator()(int st, int sp) 435 | { 436 | return str.substr(st, sp); 437 | } 438 | bool operator<(const MyString & s) 439 | { 440 | if (str.compare(s.str)<0) return true; 441 | else return false; 442 | } 443 | bool operator==(const MyString & s) 444 | { 445 | if (str.compare(s.str)==0) return true; 446 | else return false; 447 | } 448 | bool operator>(const MyString & s) 449 | { 450 | if (str.compare(s.str)>0) return true; 451 | else return false; 452 | } 453 | }; 454 | MyString operator+(const char * s, const MyString & st) 455 | { 456 | MyString ss; 457 | ss.str.assign(s); 458 | ss.str += st.str; 459 | return ss; 460 | } 461 | ostream & operator<<(ostream & os, const MyString & s) 462 | { 463 | os << s.str; 464 | return os; 465 | } 466 | 467 | int CompareString(const void *e1, const void *e2) 468 | { 469 | MyString *s1 = (MyString *)e1; 470 | MyString *s2 = (MyString *)e2; 471 | if (*s1 < *s2) return -1; 472 | else if (*s1 == *s2) return 0; 473 | else if (*s1 > *s2) return 1; 474 | } 475 | int main() 476 | { 477 | MyString s1("abcd-"), s2, s3("efgh-"), s4(s1); 478 | MyString SArray[4] = { "big", "me", "about", "take" }; 479 | cout << "1." << s1 << s2 << s3 << s4 << endl; 480 | s4 = s3; 481 | s3 = s1 + s3; 482 | cout << "2." << s1 << endl; 483 | cout << "3." << s2 << endl; 484 | cout << "4." << s3 << endl; 485 | cout << "5." << s4 << endl; 486 | cout << "6." << s1[2] << endl; 487 | s2 = s1; 488 | s1 = "ijkl-"; 489 | s1[2] = 'A'; 490 | cout << "7." << s2 << endl; 491 | cout << "8." << s1 << endl; 492 | s1 += "mnop"; 493 | cout << "9." << s1 << endl; 494 | s4 = "qrst-" + s2; 495 | cout << "10." << s4 << endl; 496 | s1 = s2 + s4 + "uvw" + "xyz"; 497 | cout << "11." << s1 << endl; 498 | qsort(SArray, 4, sizeof(MyString), CompareString); 499 | for (int i = 0; i < 4; ++i) 500 | cout << SArray[i] << endl; 501 | //输出s1从下标为0开始长度为4的字串 502 | cout << s1(0, 4) << endl; 503 | //输出s1从下标为5开始长度为10的字串 504 | cout << s1(5, 10) << endl; 505 | return 0; 506 | } 507 | /******************************************继承string类:DEV C++/visual studio******/ 508 | #include 509 | #include 510 | #include 511 | #include 512 | using namespace std; 513 | class MyString : public string { 514 | public: 515 | MyString() :string() {}; 516 | MyString(const char * s) :string(s){}; 517 | MyString(const string & s) : string(s){}; 518 | MyString operator() (int s, int l) 519 | { 520 | return this->substr(s, l); 521 | } 522 | }; 523 | int CompareString(const void *e1, const void *e2) 524 | { 525 | MyString *s1 = (MyString *)e1; 526 | MyString *s2 = (MyString *)e2; 527 | if (*s1 < *s2) return -1; 528 | else if (*s1 == *s2) return 0; 529 | else if (*s1 > *s2) return 1; 530 | } 531 | int main() 532 | { 533 | MyString s1("abcd-"), s2, s3("efgh-"), s4(s1); 534 | MyString SArray[4] = { "big", "me", "about", "take" }; 535 | cout << "1." << s1 << s2 << s3 << s4 << endl; 536 | s4 = s3; 537 | s3 = s1 + s3; 538 | cout << "2." << s1 << endl; 539 | cout << "3." << s2 << endl; 540 | cout << "4." << s3 << endl; 541 | cout << "5." << s4 << endl; 542 | cout << "6." << s1[2] << endl; 543 | s2 = s1; 544 | s1 = "ijkl-"; 545 | s1[2] = 'A'; 546 | cout << "7." << s2 << endl; 547 | cout << "8." << s1 << endl; 548 | s1 += "mnop"; 549 | cout << "9." << s1 << endl; 550 | s4 = "qrst-" + s2; 551 | cout << "10." << s4 << endl; 552 | s1 = s2 + s4 + "uvw" + "xyz"; 553 | cout << "11." << s1 << endl; 554 | qsort(SArray, 4, sizeof(MyString), CompareString); 555 | for (int i = 0; i < 4; ++i) 556 | cout << SArray[i] << endl; 557 | //输出s1从下标为0开始长度为4的字串 558 | cout << s1(0, 4) << endl; 559 | //输出s1从下标为5开始长度为10的字串 560 | cout << s1(5, 10) << endl; 561 | return 0; 562 | } 563 | /******************************重载string相关的函数:DEV C++*************************************/ 564 | /* 565 | visual studio 报错误: 566 | error C2556: “void strcpy(char *,const char *)”: 重载函数与“char *strcpy(char *,const char *)”只是在返回类型上不同 567 | error C2556: “void strcat(char *,const char *)”: 重载函数与“char *strcat(char *,const char *)”只是在返回类型上不同 568 | error C2556: “int strlen(const char *)”: 重载函数与“size_t strlen(const char *)”只是在返回类型上不同 569 | */ 570 | #include 571 | #include 572 | using namespace std; 573 | int strlen(const char * s) 574 | { int i = 0; 575 | for(; s[i]; ++i); 576 | return i; 577 | } 578 | void strcpy(char * d,const char * s) 579 | { 580 | int i = 0; 581 | for( i = 0; s[i]; ++i) 582 | d[i] = s[i]; 583 | d[i] = 0; 584 | 585 | } 586 | int strcmp(const char * s1,const char * s2) 587 | { 588 | for(int i = 0; s1[i] && s2[i] ; ++i) { 589 | if( s1[i] < s2[i] ) 590 | return -1; 591 | else if( s1[i] > s2[i]) 592 | return 1; 593 | } 594 | return 0; 595 | } 596 | void strcat(char * d,const char * s) 597 | { 598 | int len = strlen(d); 599 | strcpy(d+len,s); 600 | } 601 | class MyString{ 602 | char * str; 603 | public: 604 | MyString() 605 | { 606 | //str = NULL; 607 | str = new char[1]; 608 | *str = '\0'; 609 | } 610 | MyString(const char * s) 611 | { 612 | str = new char[strlen(s) + 1]; 613 | strcpy(str, s); 614 | } 615 | MyString(const MyString & s) 616 | { 617 | str = new char[strlen(s.str) + 1]; 618 | strcpy(str, s.str); 619 | } 620 | ~MyString() 621 | { 622 | if (str) delete[] str; 623 | } 624 | MyString & operator=(const MyString & s) 625 | { 626 | if (str == s.str) return *this; 627 | if (str) delete[] str; 628 | str = new char[strlen(s.str) + 1]; 629 | strcpy(str, s.str); 630 | return *this; 631 | } 632 | MyString & operator=(const char * s) 633 | { 634 | if (str) delete[] str; 635 | str = new char[strlen(s) + 1]; 636 | strcpy(str, s); 637 | return *this; 638 | } 639 | MyString operator+(const MyString & s) 640 | { 641 | MyString sTmp(*this); 642 | if (sTmp.str) delete[] sTmp.str; 643 | sTmp.str = new char[strlen(this->str) + strlen(s.str) + 1]; 644 | strcpy(sTmp.str, this->str); 645 | strcat(sTmp.str, s.str); 646 | return sTmp; 647 | } 648 | MyString operator+(const char * s) 649 | { 650 | MyString sTmp(*this); 651 | if (sTmp.str) delete[] sTmp.str; 652 | sTmp.str = new char[strlen(this->str) + strlen(s) + 1]; 653 | strcpy(sTmp.str, this->str); 654 | strcat(sTmp.str, s); 655 | return sTmp; 656 | } 657 | MyString & operator+=(const char * s) 658 | { 659 | char * tmp; 660 | tmp = new char[strlen(str) + 1]; 661 | strcpy(tmp, str); 662 | if (str) delete[] str; 663 | str = new char[strlen(tmp) + strlen(s) + 1]; 664 | strcpy(str, tmp); 665 | strcat(str, s); //strncat(str, s, strlen(s)); 666 | return *this; 667 | } 668 | friend MyString operator+(const char * s, const MyString & st) 669 | { 670 | MyString ss; 671 | ss.str = new char[strlen(s) + strlen(st.str) + 1]; 672 | strcpy(ss.str, s); 673 | strcat(ss.str, st.str); //strncat(ss.str, st.str, strlen(st.str)); 674 | return ss; 675 | } 676 | friend ostream & operator<<(ostream & os, const MyString & s) 677 | { 678 | os << s.str; 679 | return os; 680 | } 681 | char & operator[](int i) 682 | { 683 | return str[i]; 684 | } 685 | char * operator()(int st, int sp) 686 | { 687 | char * ss; 688 | ss = str; 689 | ss[st + sp] = '\0'; 690 | return &ss[st]; 691 | } 692 | friend bool operator<(const MyString & s1, const MyString & s2) 693 | { 694 | int t; 695 | t = strcmp(s1.str, s2.str); 696 | if (t < 0) return true; 697 | else return false; 698 | } 699 | friend bool operator==(const MyString & s1, const MyString & s2) 700 | { 701 | int t; 702 | t = strcmp(s1.str, s2.str); 703 | if (t == 0) return true; 704 | else return false; 705 | } 706 | friend bool operator>(const MyString & s1, const MyString & s2) 707 | { 708 | int t; 709 | t = strcmp(s1.str, s2.str); 710 | if (t > 0) return true; 711 | else return false; 712 | } 713 | }; 714 | 715 | int CompareString( const void * e1, const void * e2) 716 | { 717 | MyString * s1 = (MyString * ) e1; 718 | MyString * s2 = (MyString * ) e2; 719 | if( * s1 < *s2 ) 720 | return -1; 721 | else if( *s1 == *s2) 722 | return 0; 723 | else if( *s1 > *s2 ) 724 | return 1; 725 | } 726 | int main() 727 | { 728 | MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); 729 | MyString SArray[4] = {"big","me","about","take"}; 730 | cout << "1. " << s1 << s2 << s3<< s4<< endl; 731 | s4 = s3; 732 | s3 = s1 + s3; 733 | cout << "2. " << s1 << endl; 734 | cout << "3. " << s2 << endl; 735 | cout << "4. " << s3 << endl; 736 | cout << "5. " << s4 << endl; 737 | cout << "6. " << s1[2] << endl; 738 | s2 = s1; 739 | s1 = "ijkl-"; 740 | s1[2] = 'A' ; 741 | cout << "7. " << s2 << endl; 742 | cout << "8. " << s1 << endl; 743 | s1 += "mnop"; 744 | cout << "9. " << s1 << endl; 745 | s4 = "qrst-" + s2; 746 | cout << "10. " << s4 << endl; 747 | s1 = s2 + s4 + " uvw " + "xyz"; 748 | cout << "11. " << s1 << endl; 749 | qsort(SArray, 4, sizeof(MyString), CompareString); 750 | for (int i = 0; i < 4; i++) 751 | cout << SArray[i] << endl; 752 | //s1的从下标0开始长度为4的子串 753 | cout << s1(0, 4) << endl; 754 | //s1的从下标5开始长度为10的子串 755 | cout << s1(5, 10) << endl; 756 | return 0; 757 | } 758 | -------------------------------------------------------------------------------- /8.2.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************************* 2 | 编程将一个c++程序文件中的注释以及函数体内部的语句全部去掉后输出到一个 3 | 新的文件。假设“/**/”形式的注释没有嵌套。若程序名为proccpp,则以命令行方式运 4 | 行之: 5 | Procpp test1.cpp result2.cpp 6 | 能够将testl. cpp处理后的结果生成为result2. cpp文件。 7 | 输入样例: 8 | #include 9 | #include< iomanip > 10 | using namepsace std; 11 | /* test sample 12 | */ 13 | void func(int n) 14 | { 15 | if (n) 16 | { 17 | cout << ”in func” << endl; 18 | } 19 | } 20 | int main() //this is main 21 | { 22 | double f; 23 | cin >> f; 24 | retun 0; 25 | } 26 | 输出样例: 27 | #include < iostream > 28 | #include 29 | using namespace std; 30 | void func() 31 | { 32 | } 33 | int main() 34 | { 35 | } 36 | ****************************************************************************************************/ 37 | #include 38 | #include 39 | #include 40 | using namespace std; 41 | int main(int argc, char* argv[]) 42 | { 43 | if (argc != 3) 44 | { 45 | cout << "File name missing!" << endl; 46 | return 0; 47 | } 48 | ifstream inFile(argv[1], ios::binary | ios::in); 49 | if (!inFile) 50 | { 51 | cout << "Source file open error." << endl; 52 | return 0; 53 | } 54 | ofstream outFile(argv[2], ios::binary | ios::out); 55 | if (!outFile) 56 | { 57 | inFile.close(); 58 | cout << "New file open error." << endl; 59 | return 0; 60 | } 61 | char c,c_last=0; 62 | int flag=0; 63 | while (inFile.get(c)) 64 | { 65 | if (c == '/' && inFile.peek() == '*')// /* */格式的注释 66 | { 67 | while (inFile.get(c))//一直读到注释结尾'/'字符,丢弃其间读到的所有注释内容 68 | { 69 | if (c == '/')break; 70 | } 71 | if (c == '/') 72 | { 73 | while (inFile.get(c))//一直读到换行'\n',将'/'和其间读到的回车'\r'也丢弃 74 | { 75 | if (c == '\n')break; 76 | } 77 | } 78 | if (c == '\n')continue;//将换行'\n'也丢弃 79 | } 80 | if (c == '/' && inFile.peek() == '/')// //格式的注释 81 | { 82 | while (inFile.get(c))//一直读到回车'\r',将上行的注释丢弃,但是换行不丢弃 83 | { 84 | if (c == '\r')break; 85 | } 86 | } 87 | //遇到{ },只留最外层的{ },最外层{}内的所有内容丢弃 88 | if (c_last == '{') flag++; 89 | if (c == '}')flag--; 90 | if (flag == 0) 91 | { 92 | outFile.put(c); 93 | if (c == '{')//{后面的换行也被丢弃了,重新加上换行 94 | { 95 | outFile.put('\r'); 96 | outFile.put('\n'); 97 | } 98 | } 99 | c_last = c; 100 | } 101 | outFile.close(); 102 | inFile.close(); 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /8.3.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | 编写一个程序doublefile.exe.运行方式如下: 3 | doubleflie 文件名 4 | 则能将指定文件内容加倍,文件未必是文本文件。例如有some.txt文件内容如下: 5 | ThLs is a file. 6 | Please double lt. 7 | 最后一行后面没有换行。则经过处理后some.txt文件内容如下: 8 | This is a file. 9 | Please double it.This is a file. 10 | Please double id. 11 | **********************************************************/ 12 | #include 13 | #include 14 | using namespace std; 15 | int main(int argc, char* argv[]) 16 | { 17 | if (argc != 2) 18 | { 19 | cout << "File name missing!" << endl; 20 | return 0; 21 | } 22 | fstream ioFile(argv[1], ios::binary | ios::in | ios::out); 23 | if (!ioFile) 24 | { 25 | cout << "file open error" << endl; 26 | return 0; 27 | } 28 | ioFile.seekp(0, ios::end); 29 | int wrpointer=ioFile.tellp(); 30 | ioFile.seekg(0, ios::beg); 31 | char c; 32 | for (int i = 0; i < wrpointer; i++) 33 | { 34 | //当前指针位置i,将要读取i位置的字符,并将该字符写入wrpointer+i的位置 35 | ioFile.get(c);//读取字符后,指针移到下一个位置i+1 36 | ioFile.seekp(wrpointer-1, ios::cur);//当前的指针位置i+1和要写入的位置wrpointer+i,间隔wrpointer-1;指针跳到该位置wrpointer+i 37 | ioFile.put(c);//在位置wrpointer+i写入字符,指针指向wrpointer+i+1 38 | ioFile.seekg(0-wrpointer, ios::cur);//指针要跳回位置i+1,准备读取该位置字符;改位置和当前指针位置wrpointer+i+1,间隔0-wrpointer 39 | } 40 | ioFile.close(); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /8.4.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | 编写一个学生记录处理程序。学生记录用以下类表示 3 | claas { 4 | char name[10]; 5 | int ID; 6 | double gpa; 7 | }; 8 | 学生记录文件名为students. dat,要求是二进制文件,文件中的每个记录和上述类的对象相对应。students. dat最初不存在。 9 | 程序可以多次运行,如果第一敬运行程序后students.dat生成了,则以后再运行时,程序应基干已经存在的students. dat进行操作。 10 | 程序运行时能接受以下几种命令: 11 | 1) Add 姓名 学号 成绩 12 | 例如:Add Tom 1234567 78.5 13 | 添加学生信息.姓名和学号都不会包含空格。姓名由最多9个字母组成,学号是整数。可能重名,但学号不会重复。 14 | 如果发现相同学号的学生已经存在,则不添加学生信息,而是输出:“Aiready entered”。 15 | 2) Search 姓名 16 | 根据姓名查找学生信息,并输出。如果有重名的,把重名的学生信息全部输出。辅出格式为: 17 | 姓名 学号 成绩 18 | 每个学生信息输出为一行。查不到则输出“Not Found”。 19 | 3) Search 学号 20 | 根据学号查找学生信息,并输出。输出格式为: 21 | 姓名 学号 成绩 22 | 每个学生信息输出为一行。查不到则输出“Not Found”。 23 | 4) Modify 学号 成绩 24 | 根据学号修改学生的成绩并输出“Done”。如果找不到该学号的学生,则输出“NotFound”。 25 | 假定学生记录非常多,所以不能采取用一个大数组把全部学生记录都读取到内存的做法。 26 | ***************************************************************************************************/ 27 | #include 28 | #include 29 | #include 30 | using namespace std; 31 | class CStudent{ 32 | public: 33 | char name[10]; 34 | int ID; 35 | double gpa; 36 | }; 37 | int main() 38 | { 39 | string command; 40 | CStudent st,pt; 41 | fstream ioFile("students.dat", ios::binary | ios::in | ios::out);//ios::app //app会在每次写操作之前都把写指针置于文件末尾 42 | if (!ioFile) //文件不存在,新建文件 43 | { 44 | ioFile.open("students.dat", ios::binary | ios::in | ios::out | ios::trunc); 45 | } 46 | if (!ioFile) //新建文件不成功 47 | { 48 | cout << "open file error" << endl; 49 | return 0; 50 | } 51 | while (cin >> command) 52 | { 53 | if (command == "Add") 54 | { 55 | cin >> st.name >> st.ID >> st.gpa; 56 | int flag_repeat = 0; //是否重复添加的标志 57 | ioFile.seekg(0, ios::end); 58 | int total = ioFile.tellg() / sizeof(CStudent) - 1; //文件中有几条记录(从0数) 59 | for (int i = 0; i <= total; i++) 60 | { 61 | ioFile.seekg(i*sizeof(CStudent), ios::beg); 62 | ioFile.read((char*)&pt, sizeof(pt)); 63 | if (pt.ID == st.ID) 64 | { 65 | flag_repeat = 1; 66 | break; 67 | } 68 | } 69 | ioFile.seekp(0, ios::end); 70 | if (flag_repeat == 0) ioFile.write((char*)&st, sizeof(st)); 71 | else if (flag_repeat == 1) cout << "Already entered" << endl; 72 | } 73 | else if (command == "Search") 74 | { 75 | cin.ignore(1, ' '); //丢弃Search后面的空格 76 | int c = cin.peek(); 77 | int flag_found = 0; //是否找到的标志 78 | if (c >= 'A'&&c <= 'Z') //按姓名查找 79 | { 80 | cin >> st.name; 81 | ioFile.seekg(0, ios::end); 82 | int total = ioFile.tellg() / sizeof(CStudent) - 1; 83 | int i; 84 | for (i = 0; i <= total; i++) 85 | { 86 | ioFile.seekg(i*sizeof(CStudent), ios::beg); 87 | ioFile.read((char*)&pt, sizeof(pt)); 88 | if (strcmp(st.name, pt.name)==0)//(st.name == pt.name) //name是char型,不是字符串对象,不能用==比较 89 | { 90 | cout << pt.name << " " << pt.ID << " " << pt.gpa << endl; 91 | flag_found = 1; 92 | } 93 | } 94 | } 95 | else //按学号查找 96 | { 97 | cin >> st.ID; 98 | ioFile.seekg(0, ios::end); 99 | int total = ioFile.tellg() / sizeof(CStudent) - 1; 100 | int i; 101 | for (i = 0; i <= total; i++) 102 | { 103 | ioFile.seekg(i*sizeof(CStudent), ios::beg); 104 | ioFile.read((char*)&pt, sizeof(pt)); 105 | if (st.ID == pt.ID) 106 | { 107 | cout << pt.name << " " << pt.ID << " " << pt.gpa << endl; 108 | flag_found = 1; 109 | break; 110 | } 111 | } 112 | } 113 | if (flag_found == 0) cout << "Not Found" << endl; 114 | } 115 | else if (command == "Modify") 116 | { 117 | cin >> st.ID >> st.gpa; 118 | ioFile.seekg(0, ios::end); 119 | int total = ioFile.tellg() / sizeof(CStudent) - 1; 120 | int i, flag_found = 0; 121 | for (i = 0; i <= total; i++) 122 | { 123 | ioFile.seekg(i*sizeof(CStudent), ios::beg); 124 | ioFile.read((char*)&pt, sizeof(pt)); 125 | if (st.ID == pt.ID) 126 | { 127 | pt.gpa = st.gpa; 128 | ioFile.seekp(i*sizeof(CStudent), ios::beg); 129 | ioFile.write((char*)&pt, sizeof(pt)); 130 | flag_found = 1; 131 | cout << "Done" << endl; 132 | break; 133 | } 134 | } 135 | if (flag_found == 0) cout << "Not Found" << endl; 136 | } 137 | } 138 | ioFile.close(); 139 | return 0; 140 | } 141 | -------------------------------------------------------------------------------- /MOOC/001:简单的swap.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | 001:简单的swap 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 填空,使得程序输出结果是: 6 | 5,3 7 | ********************************************************************************/ 8 | 9 | #include 10 | using namespace std; 11 | class A 12 | { 13 | public: 14 | int x; 15 | int getX() { return x; } 16 | }; 17 | void swap( 18 | A & a, A & b // 在此处补充你的代码 19 | ) 20 | { 21 | int tmp = a.x; 22 | a.x = b.x; 23 | b.x = tmp; 24 | } 25 | int main() 26 | { 27 | A a,b; 28 | a.x = 3; 29 | b.x = 5; 30 | swap(a,b); 31 | cout << a.getX() << "," << b.getX(); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /MOOC/002:难一点的swap.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | 002:难一点的swap 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 填空,使得程序输出结果是: 6 | 5,3 7 | *************************************************************************/ 8 | 9 | #include 10 | using namespace std; 11 | 12 | void swap( 13 | int * & a, int * & b // 在此处补充你的代码 14 | ) 15 | { 16 | int * tmp = a; 17 | a = b; 18 | b = tmp; 19 | } 20 | int main() 21 | { 22 | int a = 3,b = 5; 23 | int * pa = & a; 24 | int * pb = & b; 25 | swap(pa,pb); 26 | cout << *pa << "," << * pb; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /MOOC/003:好怪异的返回值.cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | 003:好怪异的返回值 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 填空,使得程序输出指定结果 6 | 10 7 | ***************************************************************************/ 8 | 9 | #include 10 | using namespace std; 11 | int & // 在此处补充你的代码 12 | getElement(int * a, int i) 13 | { 14 | return a[i]; 15 | } 16 | int main() 17 | { 18 | int a[] = {1,2,3}; 19 | getElement(a,1) = 10; 20 | cout << a[1] ; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /MOOC/004:神秘的数组初始化.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************** 2 | 004:神秘的数组初始化 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 填空,使得程序输出指定结果 6 | 123,456 7 | ******************************************************************/ 8 | 9 | #include 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | int * a[] = { 15 | NULL, NULL, new int, new int[6] // 在此处补充你的代码 16 | }; 17 | 18 | *a[2] = 123; 19 | a[3][5] = 456; 20 | if(! a[0] ) { 21 | cout << * a[2] << "," << a[3][5]; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /MOOC/005:编程填空:学生信息处理程序.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************** 2 | 005:编程填空:学生信息处理程序 3 | 总时间限制: 1000ms 内存限制: 1024kB 4 | 描述 5 | 实现一个学生信息处理程序,计算一个学生的四年平均成绩。 6 | 要求实现一个代表学生的类,并且类中所有成员变量都是【私有的】。 7 | 补充下列程序中的 Student 类以实现上述功能。 8 | 9 | 输入 10 | 输入数据为一行,包括: 11 | 姓名,年龄,学号,第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,第四学年平均成绩。 12 | 其中姓名为由字母和空格组成的字符串(输入保证姓名不超过20个字符,并且空格不会出现在字符串两端),年龄、学号和学年平均成绩均为非负整数。信息之间用逗号隔开。 13 | 14 | 输出 15 | 输出一行数据,包括: 16 | 姓名,年龄,学号,四年平均成绩。 17 | 信息之间用逗号隔开。 18 | 19 | 样例输入 20 | Tom Hanks,18,7817,80,80,90,70 21 | 22 | 样例输出 23 | Tom Hanks,18,7817,80 24 | 25 | 提示 26 | 必须用类实现,其中所有成员变量都是私有的。 27 | 输出结果中,四年平均成绩不一定为整数。 28 | ************************************************************************/ 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | using namespace std; 37 | 38 | class Student { 39 | // 在此处补充你的代码 40 | private: 41 | char name[21]; 42 | int age; 43 | int id; 44 | int avg[4]; 45 | float average; 46 | public: 47 | void input() 48 | { 49 | int i; 50 | cin.getline(name, 20, ','); 51 | cin >> age; 52 | if (cin.get() == ',') 53 | cin >> id; 54 | for (i = 0; i < 4; i++) 55 | { 56 | if (cin.get() == ',') 57 | cin >> avg[i]; 58 | } 59 | } 60 | void calculate() 61 | { 62 | int i; 63 | average = 0; 64 | for (i = 0; i < 4; i++) 65 | { 66 | average += avg[i]; 67 | } 68 | average /= 4; 69 | } 70 | void output() 71 | { 72 | cout << name <<"," << age <<"," << id <<"," << average; 73 | } 74 | // 在此处补充你的代码 75 | }; 76 | 77 | int main() { 78 | Student student; // 定义类的对象 79 | student.input(); // 输入数据 80 | student.calculate(); // 计算平均成绩 81 | student.output(); // 输出数据 82 | } 83 | -------------------------------------------------------------------------------- /MOOC/006:奇怪的类复制.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | 006:奇怪的类复制 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空,使其输出9 22 5 6 | *******************************************************************************/ 7 | 8 | #include 9 | using namespace std; 10 | class Sample { 11 | public: 12 | int v; 13 | // 在此处补充你的代码 14 | Sample(){} 15 | Sample(int x) :v(x){} 16 | Sample(const Sample & x) 17 | { 18 | v = x.v + 2; 19 | } 20 | // 在此处补充你的代码 21 | }; 22 | void PrintAndDouble(Sample o) 23 | { 24 | cout << o.v; 25 | cout << endl; 26 | } 27 | int main() 28 | { 29 | Sample a(5); 30 | Sample b = a; 31 | PrintAndDouble(b); 32 | Sample c = 20; 33 | PrintAndDouble(c); 34 | Sample d; 35 | d = a; 36 | cout << d.v; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /MOOC/007:返回什么才好呢.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | 007:返回什么才好呢 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空,使其按要求输出 6 | 7 | 输入 8 | 多组数据,每组一行,是整数 m 和 n 9 | 10 | 输出 11 | 先输出一行: 12 | 123 13 | 然后,对每组数据,输出两行,第一行是m,第二行是n 14 | 15 | 样例输入 16 | 2 3 17 | 4 5 18 | 19 | 样例输出 20 | 123 21 | 2 22 | 3 23 | 4 24 | 5 25 | ********************************************************************************/ 26 | 27 | #include 28 | using namespace std; 29 | class A { 30 | public: 31 | int val; 32 | 33 | A(int 34 | // 在此处补充你的代码 35 | x = 123) :val(x){} 36 | A & GetObj() 37 | { 38 | return *this; 39 | } 40 | // 在此处补充你的代码 41 | }; 42 | int main() 43 | { 44 | int m,n; 45 | A a; 46 | cout << a.val << endl; 47 | while(cin >> m >> n) { 48 | a.GetObj() = m; 49 | cout << a.val << endl; 50 | a.GetObj() = A(n); 51 | cout << a.val<< endl; 52 | } 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /MOOC/008:超简单的复数类.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | 008:超简单的复数类 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 下面程序的输出是: 6 | 3+4i 7 | 5+6i 8 | 请补足Complex类的成员函数。不能加成员变量。 9 | **************************************************************************/ 10 | 11 | 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | class Complex { 17 | private: 18 | double r,i; 19 | public: 20 | void Print() { 21 | cout << r << "+" << i << "i" << endl; 22 | } 23 | // 在此处补充你的代码 24 | Complex() {}; 25 | #if 1 26 | //输入为x+xi,x为多位数 27 | Complex(char * str) 28 | { 29 | char st[10]; 30 | char * sr; 31 | char * si; 32 | char * n = NULL; 33 | strcpy(st, str); 34 | sr = strtok_r(st, "+", &n); 35 | si = strtok_r(NULL, "i", &n); 36 | r = atof(sr); 37 | i = atof(si); 38 | } 39 | #else 40 | //要输入为x+xi,x为个位数 41 | Complex(char* x) 42 | { 43 | r=x[0]-'0'; 44 | i=x[2]-'0'; 45 | } 46 | #endif 47 | // 在此处补充你的代码 48 | }; 49 | int main() { 50 | Complex a; 51 | a = "3+4i"; a.Print(); 52 | a = "5+6i"; a.Print(); 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /MOOC/009:哪来的输出.cpp: -------------------------------------------------------------------------------- 1 | /***************************************************************************************** 2 | 009:哪来的输出 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空,输出指定结果 6 | 2 7 | 1 8 | *****************************************************************************************/ 9 | 10 | #include 11 | using namespace std; 12 | class A { 13 | public: 14 | int i; 15 | A(int x) { i = x; } 16 | // 在此处补充你的代码 17 | ~A() 18 | { 19 | cout< 27 | using namespace std; 28 | class A { 29 | public: 30 | int val; 31 | 32 | A(int 33 | // 在此处补充你的代码 34 | x=123):val(x){} 35 | A & GetObj() 36 | { 37 | return *this; 38 | } 39 | // 在此处补充你的代码 40 | }; 41 | int main() 42 | { 43 | int m,n; 44 | A a; 45 | cout << a.val << endl; 46 | while(cin >> m >> n) { 47 | a.GetObj() = m; 48 | cout << a.val << endl; 49 | a.GetObj() = A(n); 50 | cout << a.val<< endl; 51 | } 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /MOOC/011:Big & Base 封闭类问题.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | 011:Big & Base 封闭类问题 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空,输出指定结果 6 | 7 | 输入 8 | 多组数据,每组一行,是一个整数 9 | 10 | 输出 11 | 对每组数据,输出两行,每行把输入的整数打印两遍 12 | 13 | 样例输入 14 | 3 15 | 4 16 | 17 | 样例输出 18 | 3,3 19 | 3,3 20 | 4,4 21 | 4,4 22 | *******************************************************************************************/ 23 | 24 | #include 25 | #include 26 | using namespace std; 27 | class Base { 28 | public: 29 | int k; 30 | Base(int n):k(n) { } 31 | }; 32 | class Big 33 | { 34 | public: 35 | int v; 36 | Base b; 37 | // 在此处补充你的代码 38 | Big(int x) :v(x), b(x){} 39 | // 在此处补充你的代码 40 | }; 41 | int main() 42 | { 43 | int n; 44 | while(cin >>n) { 45 | Big a1(n); 46 | Big a2 = a1; 47 | cout << a1.v << "," << a1.b.k << endl; 48 | cout << a2.v << "," << a2.b.k << endl; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /MOOC/012:这个指针哪来的.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************************************* 2 | 012:这个指针哪来的 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 填空,按要求输出 6 | 7 | 10 8 | *******************************************************************************************************/ 9 | 10 | #include 11 | using namespace std; 12 | 13 | struct A 14 | { 15 | int v; 16 | A(int vv):v(vv) { } 17 | // 在此处补充你的代码 18 | const A * getPointer() const 19 | { 20 | const A * p = this; 21 | return p; 22 | } 23 | // 在此处补充你的代码 24 | }; 25 | 26 | int main() 27 | { 28 | const A a(10); 29 | const A * p = a.getPointer(); 30 | cout << p->v << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /MOOC/013:魔兽世界之一:备战.cpp: -------------------------------------------------------------------------------- 1 | /***************************************************************************************** 2 | 013:魔兽世界之一:备战 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。 6 | 红司令部,City 1,City 2,……,City n,蓝司令部 7 | 8 | 两军的司令部都会制造武士。武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值、攻击力这三种属性。 9 | 10 | 双方的武士编号都是从1开始计算。红方制造出来的第n个武士,编号就是n。同样,蓝方制造出来的第n个武士,编号也是n。 11 | 12 | 武士在刚降生的时候有一个生命值。 13 | 14 | 在每个整点,双方的司令部中各有一个武士降生。 15 | 16 | 红方司令部按照iceman、lion、wolf、ninja、dragon的顺序循环制造武士。 17 | 18 | 蓝方司令部按照lion、dragon、ninja、iceman、wolf的顺序循环制造武士。 19 | 20 | 制造武士需要生命元。 21 | 22 | 制造一个初始生命值为m的武士,司令部中的生命元就要减少m个。 23 | 24 | 如果司令部中的生命元不足以制造某个按顺序应该制造的武士,那么司令部就试图制造下一个。如果所有武士都不能制造了,则司令部停止制造武士。 25 | 26 | 给定一个时间,和双方司令部的初始生命元数目,要求你将从0点0分开始到双方司令部停止制造武士为止的所有事件按顺序输出。 27 | 一共有两种事件,其对应的输出样例如下: 28 | 29 | 1) 武士降生 30 | 输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter 31 | 表示在4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。 32 | 33 | 2) 司令部停止制造武士 34 | 输出样例: 010 red headquarter stops making warriors 35 | 表示在10点整,红方司令部停止制造武士 36 | 37 | 输出事件时: 38 | 首先按时间顺序输出; 39 | 同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。 40 | 41 | 42 | 输入 43 | 第一行是一个整数,代表测试数据组数。 44 | 每组测试数据共两行。 45 | 第一行:一个整数M。其含义为, 每个司令部一开始都有M个生命元( 1 <= M <= 10000)。 46 | 第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于10000。 47 | 48 | 输出 49 | 对每组测试数据,要求输出从0时0分开始,到双方司令部都停止制造武士为止的所有事件。 50 | 对每组测试数据,首先输出"Case:n" n是测试数据的编号,从1开始 。 51 | 接下来按恰当的顺序和格式输出所有事件。每个事件都以事件发生的时间开头,时间以小时为单位,有三位。 52 | 53 | 样例输入 54 | 1 55 | 20 56 | 3 4 5 6 7 57 | 58 | 样例输出 59 | Case:1 60 | 000 red iceman 1 born with strength 5,1 iceman in red headquarter 61 | 000 blue lion 1 born with strength 6,1 lion in blue headquarter 62 | 001 red lion 2 born with strength 6,1 lion in red headquarter 63 | 001 blue dragon 2 born with strength 3,1 dragon in blue headquarter 64 | 002 red wolf 3 born with strength 7,1 wolf in red headquarter 65 | 002 blue ninja 3 born with strength 4,1 ninja in blue headquarter 66 | 003 red headquarter stops making warriors 67 | 003 blue iceman 4 born with strength 5,1 iceman in blue headquarter 68 | 004 blue headquarter stops making warriors 69 | *****************************************************************************************/ 70 | 71 | #include 72 | #include 73 | #include 74 | using namespace std; 75 | 76 | class Headquarter{ 77 | private: 78 | string headquarterName;//司令部的名称 79 | int elements;//司令部里的生命元 80 | int counts;//制造的战士总数 81 | string warriorNames[5];//每种战士的名称 82 | int warriorStrength[5];//每种战士的生命值 83 | int warriorCounts[5];//每种战士的数量 84 | int time;//制造战士的次数 85 | void productWarrior();//某次制造的战士 86 | public: 87 | Headquarter(string headquarter, int element, string name[], int strength[]);//构造函数,用来初始化 88 | int dispatchWarrior();//制造士兵 89 | }; 90 | 91 | Headquarter::Headquarter(string headquarter, int element, string name[], int strength[]) 92 | { 93 | int i, order[5]; 94 | counts = 0; 95 | time = 0; 96 | headquarterName = headquarter; 97 | elements = element; 98 | if (headquarterName == "red")//红方司令部 99 | {//制造战士的顺序 100 | order[0] = 2; 101 | order[1] = 3; 102 | order[2] = 4; 103 | order[3] = 1; 104 | order[4] = 0; 105 | } 106 | else//蓝方司令部 107 | {//制造战士的顺序 108 | order[0] = 3; 109 | order[1] = 0; 110 | order[2] = 1; 111 | order[3] = 2; 112 | order[4] = 4; 113 | } 114 | for (i = 0; i < 5; i++) 115 | { 116 | warriorCounts[i] = 0; 117 | warriorNames[i] = name[order[i]]; 118 | warriorStrength[i] = strength[order[i]]; 119 | } 120 | } 121 | 122 | void Headquarter::productWarrior() 123 | { 124 | warriorCounts[time % 5]++; 125 | elements -= warriorStrength[time % 5]; 126 | 127 | cout << setfill('0') << setw(3) << counts << " " << headquarterName << " " << warriorNames[time % 5] << " "; 128 | counts++; 129 | cout << counts << " born with strength " << warriorStrength[time % 5] << "," 130 | << warriorCounts[time % 5] << " " << warriorNames[time % 5] << " in " 131 | << headquarterName << " headquarter" << endl; 132 | 133 | time++; 134 | //counts++; 135 | } 136 | 137 | int Headquarter::dispatchWarrior() 138 | { 139 | int i, flag_dispatchWarrior = 1, record_time; 140 | if (elements >= warriorStrength[time % 5])//能够按顺序制造战士 141 | { 142 | productWarrior(); 143 | } 144 | else//无法按顺序制造战士 145 | { 146 | record_time = time; 147 | for (i = (record_time % 5) + 1; i < 5; i++)//判断司令部剩余的生命元是否足够制造战士 148 | { 149 | time++; 150 | if (elements >= warriorStrength[i]) break; 151 | } 152 | if (i == 5) 153 | { 154 | for (i = 0; i < (record_time % 5); i++)//判断司令部剩余的生命元是否足够制造战士 155 | { 156 | time++; 157 | if (elements >= warriorStrength[i]) break; 158 | } 159 | } 160 | if (i == (record_time % 5)) flag_dispatchWarrior = 0; 161 | 162 | if (flag_dispatchWarrior == 0) 163 | { 164 | //cout << counts <<"-"<< time << endl; 165 | cout << setfill('0') << setw(3) << counts << " " << headquarterName << " headquarter stops making warriors" << endl; 166 | } 167 | else//继续制造战士 168 | { 169 | //cout << ">>"; 170 | productWarrior(); 171 | } 172 | } 173 | return flag_dispatchWarrior; 174 | } 175 | 176 | int main() 177 | { 178 | string name[5] = { "dragon", "ninja", "iceman", "lion", "wolf" }; 179 | int cases, element, strength[5], flag_red, flag_blue; 180 | int case_cnt = 0; 181 | cin >> cases; 182 | while (cases) 183 | { 184 | cases--; 185 | case_cnt++; 186 | flag_red = 1; 187 | flag_blue = 1; 188 | 189 | cin >> element >> strength[0] >> strength[1] >> strength[2] >> strength[3] >> strength[4]; 190 | Headquarter red("red", element, name, strength); 191 | Headquarter blue("blue", element, name, strength); 192 | cout << "Case:" << case_cnt << endl; 193 | while (flag_red == 1 || flag_blue == 1) 194 | { 195 | if (flag_red == 1) flag_red = red.dispatchWarrior(); 196 | if (flag_blue == 1) flag_blue = blue.dispatchWarrior(); 197 | } 198 | //cout << "********__________________**********" << endl; 199 | } 200 | return 0; 201 | } 202 | 203 | 204 | -------------------------------------------------------------------------------- /MOOC/014:MyString.cpp: -------------------------------------------------------------------------------- 1 | /*********************************************************************************************** 2 | 014:MyString 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 补足MyString类,使程序输出指定结果 6 | 7 | 输入 8 | 多组数据,每组一行,是两个不带空格的字符串 9 | 10 | 输出 11 | 对每组数据,先输出一行,打印输入中的第一个字符串三次 12 | 然后再输出一行,打印输入中的第二个字符串三次 13 | 14 | 样例输入 15 | abc def 16 | 123 456 17 | 18 | 样例输出 19 | abc,abc,abc 20 | def,def,def 21 | 123,123,123 22 | 456,456,456 23 | ***********************************************************************************************/ 24 | 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | class MyString { 30 | char * p; 31 | public: 32 | MyString(const char * s) { 33 | if( s) { 34 | p = new char[strlen(s) + 1]; 35 | strcpy(p,s); 36 | } 37 | else 38 | p = NULL; 39 | 40 | } 41 | ~MyString() { if(p) delete [] p; } 42 | // 在此处补充你的代码 43 | void Copy(const char *s) 44 | { 45 | if(p) delete p; 46 | p = new char[strlen(s) + 1]; 47 | strcpy(p,s); 48 | } 49 | MyString & operator=(const MyString & s) 50 | { 51 | //if(strlen(p)> w1 >> w2) { 80 | MyString s1(w1),s2 = s1; 81 | MyString s3(NULL); 82 | s3.Copy(w1); 83 | cout << s1 << "," << s2 << "," << s3 << endl; 84 | 85 | s2 = w2; 86 | s3 = s2; 87 | s1 = s3; 88 | cout << s1 << "," << s2 << "," << s3 << endl; 89 | 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /MOOC/015:看上去好坑的运算符重载.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | 015:看上去好坑的运算符重载 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空 6 | 7 | 输入 8 | 多组数据,每组一行,整数n 9 | 10 | 输出 11 | 对每组数据,输出一行,包括两个整数, n-5和n - 8 12 | 13 | 样例输入 14 | 20 15 | 30 16 | 17 | 样例输出 18 | 15,12 19 | 25,22 20 | *************************************************************************/ 21 | 22 | #include 23 | using namespace std; 24 | class MyInt 25 | { 26 | int nVal; 27 | public: 28 | MyInt( int n) { nVal = n ;} 29 | // 在此处补充你的代码 30 | MyInt & operator-(int a) 31 | { 32 | nVal-=a; 33 | return *this; 34 | } 35 | operator int() 36 | { 37 | return nVal; 38 | } 39 | // 在此处补充你的代码 40 | }; 41 | int Inc(int n) { 42 | return n + 1; 43 | } 44 | int main () { 45 | int n; 46 | while(cin >>n) { 47 | MyInt objInt(n); 48 | objInt-2-1-3; 49 | cout << Inc(objInt); 50 | cout <<","; 51 | objInt-2-1; 52 | cout << Inc(objInt) << endl; 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /MOOC/016:惊呆!Point竟然能这样输入输出.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | 016:惊呆!Point竟然能这样输入输出 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空 6 | 7 | 输入 8 | 多组数据,每组两个整数 9 | 10 | 输出 11 | 对每组数据,输出一行,就是输入的两个整数 12 | 13 | 样例输入 14 | 2 3 15 | 4 5 16 | 17 | 样例输出 18 | 2,3 19 | 4,5 20 | **************************************************************************/ 21 | 22 | #include 23 | using namespace std; 24 | class Point { 25 | private: 26 | int x; 27 | int y; 28 | public: 29 | Point() { }; 30 | // 在此处补充你的代码 31 | friend istream & operator>>(istream & is, Point & pp) 32 | { 33 | is>>pp.x>>pp.y; 34 | return is; 35 | } 36 | friend ostream & operator<<(ostream & os, const Point & pp) 37 | { 38 | os<> p) { 47 | cout << p << endl; 48 | } 49 | return 0; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /MOOC/017:第四周程序填空题3.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | 017:第四周程序填空题3 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 写一个二维数组类 Array2,使得下面程序的输出结果是: 6 | 0,1,2,3, 7 | 4,5,6,7, 8 | 8,9,10,11, 9 | next 10 | 0,1,2,3, 11 | 4,5,6,7, 12 | 8,9,10,11, 13 | **************************************************************************/ 14 | 15 | #include 16 | #include 17 | using namespace std; 18 | 19 | class Array2 { 20 | // 在此处补充你的代码 21 | #if 1 22 | private: 23 | int row;//数组行数 24 | int column;//数组列数 25 | int* ptr;//指向二维数组的指针 26 | public: 27 | Array2(int paraRow=0, int paraColumn=0) :row(paraRow), column(paraColumn) 28 | { 29 | if (paraRow == 0 && paraColumn == 0) ptr = NULL; 30 | else ptr = new int[row * column]; 31 | } 32 | Array2& operator= (const Array2 &a) 33 | { 34 | if(ptr==a.ptr)return *this; 35 | if (a.ptr == NULL) 36 | { 37 | if (ptr) delete[] ptr; 38 | row = 0; 39 | column = 0; 40 | ptr = NULL; 41 | return *this; 42 | } 43 | if ((row*column) < (a.row*a.column)) 44 | { 45 | if (ptr) delete[] ptr; 46 | ptr = new int[a.row * a.column]; 47 | } 48 | memcpy(ptr, a.ptr, sizeof(int)*a.row*a.column); 49 | row = a.row; 50 | column = a.column; 51 | return *this; 52 | } 53 | ~Array2() 54 | { 55 | if (ptr) delete[] ptr; 56 | } 57 | int* operator [] (int i) 58 | { 59 | return ptr + i*column; 60 | } 61 | int& operator() (int i, int j) 62 | { 63 | return ptr[i*column + j]; 64 | } 65 | #else 66 | int row, col; 67 | int **ptr; 68 | public: 69 | Array2(int s1 = 0, int s2 = 0) :row(s1), col(s2) 70 | { 71 | if (s1 == 0 && s2 == 0) ptr = NULL; 72 | else 73 | { 74 | ptr = new int*[s1]; 75 | for (int i = 0; i < s1; i++) 76 | ptr[i] = new int[s2]; 77 | } 78 | } 79 | ~Array2() 80 | { 81 | if (ptr != NULL) delete[] ptr; 82 | } 83 | Array2 & operator=(Array2 & a) 84 | { 85 | if(ptr==a.ptr)return *this; 86 | if (a.ptr == NULL) 87 | { 88 | if (ptr) delete[] ptr; 89 | row = 0; 90 | col = 0; 91 | ptr = NULL; 92 | return *this; 93 | } 94 | if (row 33 | #include 34 | #include 35 | #include 36 | using namespace std; 37 | const int MAX = 110; 38 | class CHugeInt { 39 | // 在此处补充你的代码 40 | private: 41 | char buf[220];//固定一个220位大小的数组,便于进行对齐 42 | public: 43 | void reverse(char *p)//将存储数据的数组进行逆序操作,符合竖式计算从低位向高位进行的原理 44 | { 45 | int len = strlen(p); 46 | int i = 0, j = len - 1; 47 | while (i < j) 48 | { 49 | swap(p[i], p[j]); 50 | ++i; 51 | --j; 52 | } 53 | } 54 | CHugeInt(char *p) 55 | { 56 | memset(buf, 0, sizeof(buf));//将buf初始化 57 | strcpy(buf, p); 58 | reverse(buf); 59 | } 60 | CHugeInt(int n) 61 | { 62 | memset(buf, 0, sizeof(buf)); 63 | sprintf(buf, "%d", n); 64 | reverse(buf); 65 | } 66 | CHugeInt operator+(int n) 67 | { 68 | return *this + CHugeInt(n);//可以直接利用后面写的重载运算符 69 | } 70 | CHugeInt operator+(const CHugeInt & n) 71 | { 72 | CHugeInt tmp(0); 73 | int carry = 0;//进位 74 | for (int i = 0; i < 210; i++) 75 | { 76 | char c1 = buf[i]; 77 | char c2 = n.buf[i]; 78 | if (c1 == 0 && c2 == 0 && carry == 0)break; 79 | if (c1 == 0)c1 = '0'; 80 | if (c2 == 0)c2 = '0'; 81 | int k = c1 - '0' + c2 - '0' + carry; 82 | if (k >= 10)//相加大于10则进位 83 | { 84 | carry = 1;//进位置1 85 | tmp.buf[i] = k - 10 + '0'; 86 | } 87 | else 88 | { 89 | carry = 0; 90 | tmp.buf[i] = k + '0'; 91 | } 92 | } 93 | return tmp; 94 | } 95 | friend CHugeInt operator+(int n, CHugeInt & h) 96 | { 97 | return h + n; 98 | } 99 | friend ostream & operator<<(ostream & os, const CHugeInt & h) 100 | { 101 | int len = strlen(h.buf); 102 | for (int i = len - 1; i >= 0; i--) 103 | { 104 | cout << h.buf[i]; 105 | } 106 | return os; 107 | } 108 | CHugeInt & operator++() 109 | { 110 | *this = *this + 1; 111 | return *this; 112 | } 113 | CHugeInt operator++(int) 114 | { 115 | CHugeInt tmp(*this); 116 | *this = *this + 1; 117 | return tmp; 118 | } 119 | CHugeInt & operator+=(int n) 120 | { 121 | *this = *this + n; 122 | return *this; 123 | } 124 | // 在此处补充你的代码 125 | }; 126 | int main() 127 | { 128 | char s[210]; 129 | int n; 130 | 131 | while (cin >> s >> n) { 132 | CHugeInt a(s); 133 | CHugeInt b(n); 134 | 135 | cout << a + b << endl; 136 | cout << n + a << endl; 137 | cout << a + n << endl; 138 | b += n; 139 | cout << ++ b << endl; 140 | cout << b++ << endl; 141 | cout << b << endl; 142 | } 143 | return 0; 144 | } 145 | 146 | -------------------------------------------------------------------------------- /MOOC/019:编程填空:统计动物数量.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | 019:编程填空:统计动物数量 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 代码填空,使得程序能够自动统计当前各种动物的数量 6 | 0 animals in the zoo, 0 of them are dogs, 0 of them are cats 7 | 3 animals in the zoo, 2 of them are dogs, 1 of them are cats 8 | 6 animals in the zoo, 3 of them are dogs, 3 of them are cats 9 | 3 animals in the zoo, 2 of them are dogs, 1 of them are cats 10 | **************************************************************************/ 11 | 12 | #include 13 | using namespace std; 14 | // 在此处补充你的代码 15 | class Animal{ 16 | public: 17 | static int number; 18 | Animal() 19 | { 20 | number++; 21 | } 22 | virtual ~Animal() 23 | { 24 | number--; 25 | } 26 | }; 27 | class Dog : public Animal{ 28 | public: 29 | static int number; 30 | Dog() 31 | { 32 | number++; 33 | } 34 | ~Dog() 35 | { 36 | number--; 37 | } 38 | }; 39 | class Cat : public Animal{ 40 | public: 41 | static int number; 42 | Cat() 43 | { 44 | number++; 45 | } 46 | virtual ~Cat() 47 | { 48 | number--; 49 | } 50 | }; 51 | int Animal::number = 0,Dog::number = 0,Cat::number = 0;//静态成员变量必须在类定义的外面作一下声明或初始化,否则,程序编译不会报错,但链接时会报“标识符找不到”的错误。 52 | // 在此处补充你的代码 53 | 54 | void print() { 55 | cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl; 56 | } 57 | 58 | int main() { 59 | print(); 60 | Dog d1, d2; 61 | Cat c1; 62 | print(); 63 | Dog* d3 = new Dog(); 64 | Animal* c2 = new Cat; 65 | Cat* c3 = new Cat; 66 | print(); 67 | delete c3; 68 | delete c2; 69 | delete d3; 70 | print(); 71 | } 72 | -------------------------------------------------------------------------------- /MOOC/020:全面的MyString.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************** 2 | 020:全面的MyString 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空,输出指定结果 6 | 1. abcd-efgh-abcd- 7 | 2. abcd- 8 | 3. 9 | 4. abcd-efgh- 10 | 5. efgh- 11 | 6. c 12 | 7. abcd- 13 | 8. ijAl- 14 | 9. ijAl-mnop 15 | 10. qrst-abcd- 16 | 11. abcd-qrst-abcd- uvw xyz 17 | about 18 | big 19 | me 20 | take 21 | abcd 22 | qrst-abcd- 23 | **************************************************************************************/ 24 | 25 | #include 26 | #include 27 | using namespace std; 28 | int strlen(const char * s) 29 | { int i = 0; 30 | for(; s[i]; ++i); 31 | return i; 32 | } 33 | void strcpy(char * d,const char * s) 34 | { 35 | int i = 0; 36 | for( i = 0; s[i]; ++i) 37 | d[i] = s[i]; 38 | d[i] = 0; 39 | 40 | } 41 | int strcmp(const char * s1,const char * s2) 42 | { 43 | for(int i = 0; s1[i] && s2[i] ; ++i) { 44 | if( s1[i] < s2[i] ) 45 | return -1; 46 | else if( s1[i] > s2[i]) 47 | return 1; 48 | } 49 | return 0; 50 | } 51 | void strcat(char * d,const char * s) 52 | { 53 | int len = strlen(d); 54 | strcpy(d+len,s); 55 | } 56 | class MyString 57 | { 58 | // 在此处补充你的代码 59 | char * str; 60 | public: 61 | MyString() 62 | { 63 | //str = NULL; 64 | str = new char[1]; 65 | *str = '\0'; 66 | } 67 | MyString(const char * s) 68 | { 69 | str = new char[strlen(s) + 1]; 70 | strcpy(str, s); 71 | } 72 | MyString(const MyString & s) 73 | { 74 | str = new char[strlen(s.str) + 1]; 75 | strcpy(str, s.str); 76 | } 77 | ~MyString() 78 | { 79 | if (str) delete[] str; 80 | } 81 | MyString & operator=(const MyString & s) 82 | { 83 | if (str == s.str) return *this; 84 | if (str) delete[] str; 85 | str = new char[strlen(s.str) + 1]; 86 | strcpy(str, s.str); 87 | return *this; 88 | } 89 | MyString & operator=(const char * s) 90 | { 91 | if (str) delete[] str; 92 | str = new char[strlen(s) + 1]; 93 | strcpy(str, s); 94 | return *this; 95 | } 96 | MyString operator+(const MyString & s) 97 | { 98 | MyString sTmp(*this); 99 | if (sTmp.str) delete[] sTmp.str; 100 | sTmp.str = new char[strlen(this->str) + strlen(s.str) + 1]; 101 | strcpy(sTmp.str, this->str); 102 | strcat(sTmp.str, s.str); 103 | return sTmp; 104 | } 105 | MyString operator+(const char * s) 106 | { 107 | MyString sTmp(*this); 108 | if (sTmp.str) delete[] sTmp.str; 109 | sTmp.str = new char[strlen(this->str) + strlen(s) + 1]; 110 | strcpy(sTmp.str, this->str); 111 | strcat(sTmp.str, s); 112 | return sTmp; 113 | } 114 | MyString & operator+=(const char * s) 115 | { 116 | char * tmp; 117 | tmp = new char[strlen(str) + 1]; 118 | strcpy(tmp, str); 119 | if (str) delete[] str; 120 | str = new char[strlen(tmp) + strlen(s) + 1]; 121 | strcpy(str, tmp); 122 | strcat(str, s); //strncat(str, s, strlen(s)); 123 | return *this; 124 | } 125 | friend MyString operator+(const char * s, const MyString & st) 126 | { 127 | MyString ss; 128 | ss.str = new char[strlen(s) + strlen(st.str) + 1]; 129 | strcpy(ss.str, s); 130 | strcat(ss.str, st.str); //strncat(ss.str, st.str, strlen(st.str)); 131 | return ss; 132 | } 133 | friend ostream & operator<<(ostream & os, const MyString & s) 134 | { 135 | os << s.str; 136 | return os; 137 | } 138 | char & operator[](int i) 139 | { 140 | return str[i]; 141 | } 142 | char * operator()(int st, int sp) 143 | { 144 | char * ss; 145 | ss = str; 146 | ss[st + sp] = '\0'; 147 | return &ss[st]; 148 | } 149 | friend bool operator<(const MyString & s1, const MyString & s2) 150 | { 151 | int t; 152 | t = strcmp(s1.str, s2.str); 153 | if (t < 0) return true; 154 | else return false; 155 | } 156 | friend bool operator==(const MyString & s1, const MyString & s2) 157 | { 158 | int t; 159 | t = strcmp(s1.str, s2.str); 160 | if (t == 0) return true; 161 | else return false; 162 | } 163 | friend bool operator>(const MyString & s1, const MyString & s2) 164 | { 165 | int t; 166 | t = strcmp(s1.str, s2.str); 167 | if (t > 0) return true; 168 | else return false; 169 | } 170 | // 在此处补充你的代码 171 | }; 172 | 173 | 174 | int CompareString( const void * e1, const void * e2) 175 | { 176 | MyString * s1 = (MyString * ) e1; 177 | MyString * s2 = (MyString * ) e2; 178 | if( * s1 < *s2 ) 179 | return -1; 180 | else if( *s1 == *s2) 181 | return 0; 182 | else if( *s1 > *s2 ) 183 | return 1; 184 | } 185 | int main() 186 | { 187 | MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); 188 | MyString SArray[4] = {"big","me","about","take"}; 189 | cout << "1. " << s1 << s2 << s3<< s4<< endl; 190 | s4 = s3; 191 | s3 = s1 + s3; 192 | cout << "2. " << s1 << endl; 193 | cout << "3. " << s2 << endl; 194 | cout << "4. " << s3 << endl; 195 | cout << "5. " << s4 << endl; 196 | cout << "6. " << s1[2] << endl; 197 | s2 = s1; 198 | s1 = "ijkl-"; 199 | s1[2] = 'A' ; 200 | cout << "7. " << s2 << endl; 201 | cout << "8. " << s1 << endl; 202 | s1 += "mnop"; 203 | cout << "9. " << s1 << endl; 204 | s4 = "qrst-" + s2; 205 | cout << "10. " << s4 << endl; 206 | s1 = s2 + s4 + " uvw " + "xyz"; 207 | cout << "11. " << s1 << endl; 208 | qsort(SArray,4,sizeof(MyString),CompareString); 209 | for( int i = 0;i < 4;i ++ ) 210 | cout << SArray[i] << endl; 211 | //s1的从下标0开始长度为4的子串 212 | cout << s1(0,4) << endl; 213 | //s1的从下标5开始长度为10的子串 214 | cout << s1(5,10) << endl; 215 | return 0; 216 | } 217 | 218 | 219 | -------------------------------------------------------------------------------- /MOOC/021:继承自string的MyString.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************************* 2 | 021:继承自string的MyString 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空,输出指定结果 6 | 1. abcd-efgh-abcd- 7 | 2. abcd- 8 | 3. 9 | 4. abcd-efgh- 10 | 5. efgh- 11 | 6. c 12 | 7. abcd- 13 | 8. ijAl- 14 | 9. ijAl-mnop 15 | 10. qrst-abcd- 16 | 11. abcd-qrst-abcd- uvw xyz 17 | about 18 | big 19 | me 20 | take 21 | abcd 22 | qrst-abcd- 23 | 24 | 提示 25 | 提示 1:如果将程序中所有 "MyString" 用 "string" 替换,那么除了最后两条红色的语句编译无法通过外,其他语句都没有问题, 26 | 而且输出和前面给的结果吻合。也就是说,MyString 类对 string 类的功能扩充只体现在最后两条语句上面。 27 | 28 | 提示 2: string 类有一个成员函数 string substr(int start,int length); 能够求从 start 位置开始,长度为 length 的子串 29 | 30 | 提示 3: C++中,派生类的对象可以赋值给基类对象,因为,一个派生类对象,也可看作是一个基类对象(大学生是学生)。 31 | 反过来则不行(学生未必是大学生) 同样,调用需要基类对象作参数的函数时,以派生类对象作为实参,也是没有问题的 32 | *********************************************************************************/ 33 | 34 | #include 35 | #include 36 | #include 37 | #include 38 | using namespace std; 39 | class MyString:public string 40 | { 41 | // 在此处补充你的代码 42 | public: 43 | MyString() :string(){} 44 | MyString(const char * s) :string(s){} 45 | MyString(const string & s) :string(s){} 46 | MyString operator()(int st, int ln) 47 | { 48 | return this->substr(st, ln); 49 | } 50 | // 在此处补充你的代码 51 | }; 52 | 53 | 54 | int main() 55 | { 56 | MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); 57 | MyString SArray[4] = {"big","me","about","take"}; 58 | cout << "1. " << s1 << s2 << s3<< s4<< endl; 59 | s4 = s3; 60 | s3 = s1 + s3; 61 | cout << "2. " << s1 << endl; 62 | cout << "3. " << s2 << endl; 63 | cout << "4. " << s3 << endl; 64 | cout << "5. " << s4 << endl; 65 | cout << "6. " << s1[2] << endl; 66 | s2 = s1; 67 | s1 = "ijkl-"; 68 | s1[2] = 'A' ; 69 | cout << "7. " << s2 << endl; 70 | cout << "8. " << s1 << endl; 71 | s1 += "mnop"; 72 | cout << "9. " << s1 << endl; 73 | s4 = "qrst-" + s2; 74 | cout << "10. " << s4 << endl; 75 | s1 = s2 + s4 + " uvw " + "xyz"; 76 | cout << "11. " << s1 << endl; 77 | sort(SArray,SArray+4); 78 | for( int i = 0;i < 4;i ++ ) 79 | cout << SArray[i] << endl; 80 | //s1的从下标0开始长度为4的子串 81 | cout << s1(0,4) << endl; 82 | //s1的从下标5开始长度为10的子串 83 | cout << s1(5,10) << endl; 84 | return 0; 85 | } 86 | 87 | 88 | -------------------------------------------------------------------------------- /MOOC/022:魔兽世界之二:装备.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | 022:魔兽世界之二:装备 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。 6 | 7 | 红司令部,City 1,City 2,……,City n,蓝司令部 8 | 9 | 两军的司令部都会制造武士。武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值这两种属性。 10 | 有的武士可以拥有武器。武器有三种,sword, bomb,和arrow,编号分别为0,1,2。 11 | 双方的武士编号都是从1开始计算。红方制造出来的第 n 个武士,编号就是n。同样,蓝方制造出来的第 n 个武士,编号也是n。 12 | 13 | 不同的武士有不同的特点。 14 | dragon 可以拥有一件武器。编号为n的dragon降生时即获得编号为 n%3 的武器。dragon还有“士气”这个属性,是个浮点数,其值为它降生后其司令部剩余生命元的数量除以造dragon所需的生命元数量。 15 | ninja可以拥有两件武器。编号为n的ninja降生时即获得编号为 n%3 和 (n+1)%3的武器。 16 | iceman有一件武器。编号为n的iceman降生时即获得编号为 n%3 的武器。 17 | lion 有“忠诚度”这个属性,其值等于它降生后其司令部剩余生命元的数目。 18 | wolf没特点。 19 | 请注意,在以后的题目里,武士的士气,生命值,忠诚度在其生存期间都可能发生变化,都有作用,武士手中的武器随着使用攻击力也会发生变化。 20 | 21 | 武士在刚降生的时候有一个生命值。 22 | 23 | 在每个整点,双方的司令部中各有一个武士降生。 24 | 25 | 红方司令部按照 iceman、lion、wolf、ninja、dragon 的顺序循环制造武士。 26 | 27 | 蓝方司令部按照 lion、dragon、ninja、iceman、wolf 的顺序循环制造武士。 28 | 29 | 制造武士需要生命元。 30 | 31 | 制造一个初始生命值为 m 的武士,司令部中的生命元就要减少 m 个。 32 | 33 | 如果司令部中的生命元不足以制造某个按顺序应该制造的武士,那么司令部就试图制造下一个。如果所有武士都不能制造了,则司令部停止制造武士。 34 | 给定一个时间,和双方司令部的初始生命元数目,要求你将从0点0分开始到双方司令部停止制造武士为止的所有事件按顺序输出。 35 | 一共有两种事件,其对应的输出样例如下: 36 | 37 | 1) 武士降生 38 | 输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter 39 | 表示在 4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。 40 | 如果造出的是dragon,那么还要输出一行,例: 41 | It has a arrow,and it's morale is 23.34 42 | 表示该dragon降生时得到了arrow,其士气是23.34(为简单起见,本题中arrow前面的冠词用a,不用an,士气精确到小数点后面2位,四舍五入) 43 | 如果造出的是ninja,那么还要输出一行,例: 44 | It has a bomb and a arrow 45 | 表示该ninja降生时得到了bomb和arrow。 46 | 如果造出的是iceman,那么还要输出一行,例: 47 | It has a sword 48 | 表示该iceman降生时得到了sword。 49 | 如果造出的是lion,那么还要输出一行,例: 50 | It's loyalty is 24 51 | 表示该lion降生时的忠诚度是24。 52 | 2) 司令部停止制造武士 53 | 输出样例: 010 red headquarter stops making warriors 54 | 表示在 10点整,红方司令部停止制造武士 55 | 56 | 输出事件时: 57 | 首先按时间顺序输出; 58 | 同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。 59 | 60 | 输入 61 | 第一行是一个整数,代表测试数据组数。 62 | 每组测试数据共两行。 63 | 第一行,一个整数M。其含义为: 每个司令部一开始都有M个生命元( 1 <= M <= 10000) 64 | 第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于10000 65 | 66 | 输出 67 | 对每组测试数据,要求输出从0时0分开始,到双方司令部都停止制造武士为止的所有事件。 68 | 对每组测试数据,首先输出“Case:n" n是测试数据的编号,从1开始 69 | 接下来按恰当的顺序和格式输出所有事件。每个事件都以事件发生的时间开头,时间以小时为单位,有三位。 70 | 71 | 样例输入 72 | 1 73 | 20 74 | 3 4 5 6 7 75 | 76 | 样例输出 77 | Case:1 78 | 000 red iceman 1 born with strength 5,1 iceman in red headquarter 79 | It has a bomb 80 | 000 blue lion 1 born with strength 6,1 lion in blue headquarter 81 | It's loyalty is 14 82 | 001 red lion 2 born with strength 6,1 lion in red headquarter 83 | It's loyalty is 9 84 | 001 blue dragon 2 born with strength 3,1 dragon in blue headquarter 85 | It has a arrow,and it's morale is 3.67 86 | 002 red wolf 3 born with strength 7,1 wolf in red headquarter 87 | 002 blue ninja 3 born with strength 4,1 ninja in blue headquarter 88 | It has a sword and a bomb 89 | 003 red headquarter stops making warriors 90 | 003 blue iceman 4 born with strength 5,1 iceman in blue headquarter 91 | It has a bomb 92 | 004 blue headquarter stops making warriors 93 | *************************************************************************************/ 94 | 95 | #include 96 | #include 97 | #include 98 | using namespace std; 99 | string Weapon[3]={"sword","bomb","arrow"}; 100 | class Headquarter{ 101 | private: 102 | string headquarterName;//司令部的名称 103 | int elements;//司令部里的生命元 104 | int counts;//制造的战士总数 105 | string warriorNames[5];//每种战士的名称 106 | int warriorStrength[5];//每种战士的生命值 107 | int warriorCounts[5];//每种战士的数量 108 | int time;//制造战士的次数 109 | void productWarrior();//某次制造的战士 110 | public: 111 | Headquarter(string headquarter, int element, string name[], int strength[]);//构造函数,用来初始化 112 | int dispatchWarrior();//制造士兵 113 | }; 114 | 115 | Headquarter::Headquarter(string headquarter, int element, string name[], int strength[]) 116 | { 117 | int i, order[5]; 118 | counts = 0; 119 | time = 0; 120 | headquarterName = headquarter; 121 | elements = element; 122 | if (headquarterName == "red")//红方司令部 123 | {//制造战士的顺序 124 | order[0] = 2; 125 | order[1] = 3; 126 | order[2] = 4; 127 | order[3] = 1; 128 | order[4] = 0; 129 | } 130 | else//蓝方司令部 131 | {//制造战士的顺序 132 | order[0] = 3; 133 | order[1] = 0; 134 | order[2] = 1; 135 | order[3] = 2; 136 | order[4] = 4; 137 | } 138 | for (i = 0; i < 5; i++) 139 | { 140 | warriorCounts[i] = 0; 141 | warriorNames[i] = name[order[i]]; 142 | warriorStrength[i] = strength[order[i]]; 143 | } 144 | } 145 | 146 | void Headquarter::productWarrior() 147 | { 148 | warriorCounts[time % 5]++; 149 | elements -= warriorStrength[time % 5]; 150 | 151 | cout << setfill('0') << setw(3) << counts << " " << headquarterName << " " << warriorNames[time % 5] << " " 152 | << (counts + 1) << " born with strength " << warriorStrength[time % 5] << "," 153 | << warriorCounts[time % 5] << " " << warriorNames[time % 5] << " in " 154 | << headquarterName << " headquarter" << endl; 155 | counts++; 156 | if (warriorNames[time % 5]=="dragon") 157 | { 158 | cout << "It has a " << Weapon[counts % 3] << ",and it's morale is " << fixed<< setprecision(2) << float(elements) / warriorStrength[time % 5] << endl; 159 | } 160 | if (warriorNames[time % 5] == "ninja") 161 | { 162 | cout << "It has a " << Weapon[counts % 3] << " and a " << Weapon[(counts+1) % 3] << endl; 163 | } 164 | if (warriorNames[time % 5] == "iceman") 165 | { 166 | cout << "It has a " << Weapon[counts % 3] << endl; 167 | } 168 | if (warriorNames[time % 5] == "lion") 169 | { 170 | cout << "It's loyalty is " << elements << endl; 171 | } 172 | time++; 173 | //counts++; 174 | } 175 | 176 | int Headquarter::dispatchWarrior() 177 | { 178 | int i, flag_dispatchWarrior = 1, record_time; 179 | if (elements >= warriorStrength[time % 5])//能够按顺序制造战士 180 | { 181 | productWarrior(); 182 | } 183 | else//无法按顺序制造战士 184 | { 185 | record_time = time; 186 | for (i = (record_time % 5) + 1; i < 5; i++)//判断司令部剩余的生命元是否足够制造战士 187 | { 188 | time++; 189 | if (elements >= warriorStrength[i]) break; 190 | } 191 | if (i == 5) 192 | { 193 | for (i = 0; i < (record_time % 5); i++)//判断司令部剩余的生命元是否足够制造战士 194 | { 195 | time++; 196 | if (elements >= warriorStrength[i]) break; 197 | } 198 | } 199 | if (i == (record_time % 5)) flag_dispatchWarrior = 0; 200 | 201 | if (flag_dispatchWarrior == 0) 202 | { 203 | //cout << counts <<"-"<< time << endl; 204 | cout << setfill('0') << setw(3) << counts << " " << headquarterName << " headquarter stops making warriors" << endl; 205 | } 206 | else//继续制造战士 207 | { 208 | //cout << ">>"; 209 | productWarrior(); 210 | } 211 | } 212 | return flag_dispatchWarrior; 213 | } 214 | 215 | int main() 216 | { 217 | string name[5] = { "dragon", "ninja", "iceman", "lion", "wolf" }; 218 | int cases, element, strength[5], flag_red, flag_blue; 219 | int case_cnt = 0; 220 | cin >> cases; 221 | while (cases) 222 | { 223 | cases--; 224 | case_cnt++; 225 | flag_red = 1; 226 | flag_blue = 1; 227 | 228 | cin >> element >> strength[0] >> strength[1] >> strength[2] >> strength[3] >> strength[4]; 229 | Headquarter red("red", element, name, strength); 230 | Headquarter blue("blue", element, name, strength); 231 | cout << "Case:" << case_cnt << endl; 232 | while (flag_red == 1 || flag_blue == 1) 233 | { 234 | if (flag_red == 1) flag_red = red.dispatchWarrior(); 235 | if (flag_blue == 1) flag_blue = blue.dispatchWarrior(); 236 | } 237 | //cout << "********__________________**********" << endl; 238 | } 239 | return 0; 240 | } 241 | 242 | 243 | -------------------------------------------------------------------------------- /MOOC/023:看上去像多态.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************************** 2 | 023:看上去像多态 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空产生指定输出 6 | D::Fun 7 | B::Fun 8 | D::Fun 9 | nBVal=2 10 | nBVal=24 11 | nDVal=8 12 | B::Fun 13 | nBVal=12 14 | ******************************************************************************************/ 15 | 16 | #include 17 | using namespace std; 18 | class B { 19 | private: 20 | int nBVal; 21 | public: 22 | void Print() 23 | { cout << "nBVal="<< nBVal << endl; } 24 | void Fun() 25 | {cout << "B::Fun" << endl; } 26 | B ( int n ) { nBVal = n;} 27 | }; 28 | // 在此处补充你的代码 29 | class D : public B { 30 | private: 31 | int nDVal; 32 | public: 33 | D(int n): nDVal(n), B(3 * n) {} 34 | void Fun() 35 | { 36 | cout << "D::Fun" << endl; 37 | } 38 | void Print() 39 | { 40 | B::Print(); 41 | cout << "nDVal=" << nDVal << endl; 42 | } 43 | }; 44 | // 在此处补充你的代码 45 | int main() { 46 | B * pb; D * pd; 47 | D d(4); d.Fun(); 48 | pb = new B(2); pd = new D(8); 49 | pb -> Fun(); pd->Fun(); 50 | pb->Print (); pd->Print (); 51 | pb = & d; pb->Fun(); 52 | pb->Print(); 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /MOOC/024:Fun和Do.cpp: -------------------------------------------------------------------------------- 1 | /*********************************************************************总时间限制: 1000ms 内存限制: 65536kB 2 | 024:Fun和Do 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空输出指定结果 6 | 7 | **********************************************************************/ 8 | 9 | #include 10 | using namespace std; 11 | class A { 12 | private: 13 | int nVal; 14 | public: 15 | void Fun() 16 | { cout << "A::Fun" << endl; }; 17 | void Do() 18 | { cout << "A::Do" << endl; } 19 | }; 20 | class B:public A { 21 | public: 22 | virtual void Do() 23 | { cout << "B::Do" << endl;} 24 | }; 25 | class C:public B { 26 | public: 27 | void Do( ) 28 | { cout <<"C::Do"< 12 | using namespace std; 13 | class A 14 | { 15 | public: 16 | A() { } 17 | // 在此处补充你的代码 18 | virtual ~A() 19 | { 20 | cout<<"destructor A"< 14 | using namespace std; 15 | class A { 16 | private: 17 | int nVal; 18 | public: 19 | void Fun() 20 | { cout << "A::Fun" << endl; }; 21 | virtual void Do() 22 | { cout << "A::Do" << endl; } 23 | }; 24 | class B:public A { 25 | public: 26 | virtual void Do() 27 | { cout << "B::Do" << endl;} 28 | }; 29 | class C:public B { 30 | public: 31 | void Do( ) 32 | { cout <<"C::Do"<Fun(); p->Do(); 40 | } 41 | int main() { 42 | Call( new A()); 43 | Call( new C()); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /MOOC/027:简单的SumArray.cpp: -------------------------------------------------------------------------------- 1 | /*********************************************************************************** 2 | 027:简单的SumArray 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 填写模板 PrintArray,使得程序输出结果是: TomJackMaryJohn 10 不得编写SumArray函数 6 | 7 | TomJackMaryJohn 8 | 10 9 | ***********************************************************************************/ 10 | 11 | #include 12 | #include 13 | using namespace std; 14 | template 15 | T SumArray( 16 | // 在此处补充你的代码 17 | T* a,T* b) 18 | { 19 | T p; 20 | p = *a; 21 | #if 0 22 | int t = b - a; 23 | for (int i = 1; i < t; i++) 24 | { 25 | p += *(++a); 26 | } 27 | #else 28 | while (b - a > 1) 29 | { 30 | p += *(++a); 31 | } 32 | return p; 33 | #endif 34 | return p; 35 | // 在此处补充你的代码 36 | } 37 | int main() { 38 | string array[4] = { "Tom","Jack","Mary","John"}; 39 | cout << SumArray(array,array+4) << endl; 40 | int a[4] = { 1, 2, 3, 4}; //提示:1+2+3+4 = 10 41 | cout << SumArray(a,a+4) << endl; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /MOOC/028:简单的foreach.cpp: -------------------------------------------------------------------------------- 1 | /***************************************************************************************** 2 | 028:简单的foreach 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 编写MyForeach模板,使程序按要求输出 不得编写 MyForeach函数 6 | 7 | 输入 8 | 多组数据 9 | 每组数据第一行是两个整数 m 和 n ,都不超过 50 10 | 第二行是m个不带空格的字符串 11 | 第三行是 n个整数 12 | 13 | 输出 14 | 对每组数据 15 | 第一行输出所有输入字符串连在一起的结果 16 | 第二行输出输入中的每个整数加1的结果 17 | 18 | 样例输入 19 | 3 4 20 | Tom Mike Jack 21 | 1 2 3 4 22 | 1 2 23 | Peking 24 | 100 200 25 | 26 | 样例输出 27 | TomMikeJack 28 | 2,3,4,5, 29 | Peking 30 | 101,201, 31 | ******************************************************************************************/ 32 | #include 33 | #include 34 | using namespace std; 35 | // 在此处补充你的代码 36 | template 37 | void MyForeach(T* t1, T* t2, void(*fun)(T1 t)) 38 | { 39 | while (t2-t1>0) 40 | { 41 | (*fun)(*t1); 42 | t1++; 43 | } 44 | } 45 | // 在此处补充你的代码 46 | void Print(string s) 47 | { 48 | cout << s; 49 | } 50 | void Inc(int & n) 51 | { 52 | ++n; 53 | } 54 | string array[100]; 55 | int a[100]; 56 | int main() { 57 | int m, n; 58 | while (cin >> m >> n) { 59 | for (int i = 0; i < m; ++i) 60 | cin >> array[i]; 61 | for (int j = 0; j < n; ++j) 62 | cin >> a[j]; 63 | MyForeach(array, array + m, Print); 64 | cout << endl; 65 | MyForeach(a, a + n, Inc); 66 | for (int i = 0; i < n; ++i) 67 | cout << a[i] << ","; 68 | cout << endl; 69 | } 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /MOOC/029:简单的Filter.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************************************* 2 | 029:简单的Filter 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 编写Filter模板,使得程序产生指定输出 不得编写 Filter函数 6 | 7 | 输出 8 | MikeJackLucy 9 | 3,4,5, 10 | *********************************************************************************************/ 11 | 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | // 在此处补充你的代码 17 | template 18 | T1* Filter(T1* t1, T1* t2, T1* t3, bool(*fun)(T2 t)) 19 | { 20 | while (t2 - t1 > 0) 21 | { 22 | if ((*fun)(*t1) == true) 23 | { 24 | *t3 = *t1; 25 | t3++; 26 | } 27 | t1++; 28 | } 29 | return t3; 30 | } 31 | // 在此处补充你的代码 32 | 33 | bool LargerThan2(int n) 34 | { 35 | return n > 2; 36 | } 37 | bool LongerThan3(string s) 38 | { 39 | return s.length() > 3; 40 | } 41 | 42 | string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"}; 43 | string as2[5]; 44 | int a1[5] = { 1,2,3,4,5}; 45 | int a2[5]; 46 | int main() { 47 | string * p = Filter(as1,as1+5,as2,LongerThan3); 48 | for(int i = 0;i < p - as2; ++i) 49 | cout << as2[i]; 50 | cout << endl; 51 | int * p2 = Filter(a1,a1+5,a2,LargerThan2); 52 | for(int i = 0;i < p2-a2; ++i) 53 | cout << a2[i] << ","; 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /MOOC/030:你真的搞清楚为啥 while(cin >> n) 能成立了吗?.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | 030:你真的搞清楚为啥 while(cin >> n) 能成立了吗? 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 读入两个整数,输出两个整数 ,直到碰到-1 6 | 7 | 输入 8 | 多组数据,每组一行,是两个整数 9 | 10 | 输出 11 | 对每组数据,原样输出 12 | 当碰到输入中出现-1 时,程序结束 13 | 输入中保证会有 -1 14 | 15 | 样例输入 16 | 12 44 17 | 344 555 18 | -1 19 | 2 3 20 | 21 | 样例输出 22 | 12 44 23 | 344 555 24 | *************************************************************************************/ 25 | #include 26 | using namespace std; 27 | class MyCin 28 | { 29 | // 在此处补充你的代码 30 | #if 0 31 | public: 32 | istream& operator>>(int& n) 33 | { 34 | cin >> n; 35 | if (n == -1) exit(0); 36 | return cin; 37 | } 38 | #else 39 | private: 40 | bool status; 41 | public: 42 | MyCin() :status(true){} 43 | MyCin & operator >> (int & n) 44 | { 45 | #if 0 46 | cin >> n; 47 | if (n == -1) exit(0); 48 | #else 49 | #if 0 50 | if (!status) 51 | return *this; 52 | cin >> n; 53 | if (n == -1) status = false; 54 | #else 55 | cin >> n; 56 | if (n == -1) status = false; 57 | return *this; 58 | #endif 59 | #endif 60 | } 61 | operator bool() //while循环的判断条件 62 | { 63 | return status; 64 | } 65 | #endif 66 | // 在此处补充你的代码 67 | }; 68 | int main() 69 | { 70 | MyCin m; 71 | int n1,n2; 72 | while( m >> n1 >> n2) 73 | cout << n1 << " " << n2 << endl; 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /MOOC/031:山寨版istream_iterator.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | 031:山寨版istream_iterator 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 模仿C++标准模板库istream_iterator用法,实现CMyistream_iterator使得程序按要求输出 6 | 7 | 输入 8 | 第一行是整数t,表示有t组数据 9 | 每组数据一行,三个整数加两个字符串。字符串是不含空格的 10 | 11 | 输出 12 | 对每组数据,输出二行 13 | 在第一行输出第一个数 14 | 第二行原样输出输入的内容 15 | 16 | 样例输入 17 | 2 18 | 79 90 20 hello me 19 | 12 34 19 take up 20 | 21 | 样例输出 22 | 79 23 | 79 90 20 hello me 24 | 12 25 | 12 34 19 take up 26 | 27 | 提示 28 | C++标准模板库 istream_iterator模版使用说明: 29 | 30 | 其构造函数执行过程中就会要求输入,然后每次执行++,则读取输入流中的下一个项目,执行 * 则返回上次从输入流中读取的项目。例如,下面程序运行时,就会等待用户输入数据,输入数据后程序才会结束: 31 | #include 32 | #include 33 | using namespace std; 34 | int main() { 35 | istream_iteratorinputInt(cin); 36 | return 0; 37 | } 38 | 39 | 下面程序运行时,如果输入 12 34 程序输出结果是: 12,12 40 | #include 41 | #include 42 | using namespace std; 43 | int main() { 44 | istream_iteratorinputInt(cin); 45 | cout << *inputInt << "," << *inputInt << endl; 46 | return 0; 47 | } 48 | 49 | 下面程序运行时,如果输入 12 34 56程序输出结果是: 12,56 50 | #include 51 | #include 52 | using namespace std; 53 | int main() { 54 | istream_iteratorinputInt(cin); 55 | cout << *inputInt << ","; 56 | inputInt++; 57 | inputInt++; 58 | cout << *inputInt << endl; 59 | return 0; 60 | } 61 | 62 | 下面的程序,输入什么,按回车后,就输出什么;按ctrl+z结束 63 | #include 64 | #include 65 | using namespace std; 66 | int main() { 67 | istream_iteratorinputInt(cin),eof; 68 | while(inputInt!=eof) 69 | { 70 | cout << *inputInt << ","; 71 | inputInt++; 72 | } 73 | cout< 79 | #include 80 | using namespace std; 81 | template 82 | class CMyistream_iterator 83 | { 84 | // 在此处补充你的代码 85 | istream& in; 86 | T value; 87 | public: 88 | CMyistream_iterator(istream& i) :in(i){ cin >> value; } 89 | T operator*(){ return value; } 90 | CMyistream_iterator& operator++(int) //后缀++ 91 | { 92 | CMyistream_iteratortmp = *this; 93 | cin >> value; 94 | return tmp; 95 | } 96 | CMyistream_iterator& operator++() //前缀++ 97 | { 98 | cin >> value; 99 | return *this; 100 | } 101 | // 在此处补充你的代码 102 | }; 103 | int main() 104 | { 105 | int t; 106 | cin >> t; 107 | while (t--) { 108 | CMyistream_iteratorinputInt(cin); 109 | int n1, n2, n3; 110 | n1 = *inputInt; //读入 n1 111 | int tmp = *inputInt; 112 | cout << tmp << endl; 113 | inputInt++; 114 | n2 = *inputInt; //读入 n2 115 | inputInt++; 116 | n3 = *inputInt; //读入 n3 117 | cout << n1 << " " << n2 << " " << n3 << " "; 118 | CMyistream_iterator inputStr(cin); 119 | string s1, s2; 120 | s1 = *inputStr; 121 | inputStr++; 122 | s2 = *inputStr; 123 | cout << s1 << " " << s2 << endl; 124 | } 125 | return 0; 126 | } 127 | -------------------------------------------------------------------------------- /MOOC/032:这个模板并不难.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | 032:这个模板并不难 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空,输出指定结果 6 | 7 | 输入 8 | 多组数据。每组第一行是一个不含空格的字符串 9 | 第二行是整数n 10 | 第三行是n个整数 11 | 12 | 输出 13 | 对每组数据,先依次输出输入字符串的每个字母,并且在每个字母后面加逗号 14 | 然后依次再输出输入的n个整数 ,在每个整数后面加逗号 15 | 16 | 样例输入 17 | Tom 18 | 3 19 | 3 4 5 20 | Jack 21 | 4 22 | 1 2 3 4 23 | 24 | 样例输出 25 | T,o,m, 26 | 3,4,5, 27 | J,a,c,k, 28 | 1,2,3,4, 29 | **************************************************************************/ 30 | #define _CRT_SECURE_NO_WARNINGS 31 | #include 32 | #include 33 | #include 34 | using namespace std; 35 | template 36 | class myclass { 37 | // 在此处补充你的代码 38 | T* p; 39 | int size; 40 | public: 41 | myclass(T* ptr, int len) 42 | { 43 | if (p != NULL)delete[] p; 44 | p = new T[len]; 45 | size = len; 46 | memcpy(p, ptr, size*sizeof(T)); 47 | } 48 | // 在此处补充你的代码 49 | ~myclass() { delete[] p; } 50 | void Show() 51 | { 52 | for (int i = 0; i < size; i++) 53 | { 54 | cout << p[i] << ","; 55 | } 56 | cout << endl; 57 | } 58 | }; 59 | int a[100]; 60 | int main() 61 | { 62 | char line[100]; 63 | while (cin >> line) 64 | { 65 | myclass obj(line, strlen(line)); 66 | obj.Show(); 67 | int n; 68 | cin >> n; 69 | for (int i = 0; i < n; ++i) 70 | cin >> a[i]; 71 | myclass obj2(a, n); 72 | obj2.Show(); 73 | } 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /MOOC/033:排序,又见排序!.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************* 2 | 033:排序,又见排序! 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 自己编写一个能对任何类型的数组进行排序的mysort函数模版。只能写一个mysort模板,不能写mysort函数! 6 | 7 | 输入 8 | 无 9 | 10 | 输出 11 | 4,8,10,11,123, 12 | 123,11,10,8,4, 13 | 1.4,1.2,1.8,3.1,3.2,2.1, 14 | 15 | ***************************************************************************************/ 16 | #include 17 | using namespace std; 18 | bool Greater1(int n1, int n2) 19 | { 20 | return n1 < n2; 21 | } 22 | bool Greater2(int n1, int n2) 23 | { 24 | return n1 > n2; 25 | } 26 | bool Greater3(double d1, double d2) 27 | { 28 | return d1 < d2; 29 | } 30 | // 在此处补充你的代码 31 | template 32 | void mysort(T1* first, T1* last, T2* op) 33 | { 34 | T1 tmp; 35 | for (int i = 0; i < last - first; ++i) 36 | { 37 | for (int j = i + 1; j < last - first; ++j) 38 | { 39 | if (op(*(first + i), *(first + j)) == false) 40 | { 41 | tmp = *(first + i); 42 | *(first + i) = *(first + j); 43 | *(first + j) = tmp; 44 | } 45 | } 46 | } 47 | } 48 | // 在此处补充你的代码 49 | #define NUM 5 50 | int main() 51 | { 52 | int an[NUM] = { 8,123,11,10,4 }; 53 | mysort(an, an + NUM, Greater1); //从小到大排序 54 | for (int i = 0; i < NUM; i++) 55 | cout << an[i] << ","; 56 | mysort(an, an + NUM, Greater2); //从大到小排序 57 | cout << endl; 58 | for (int i = 0; i < NUM; i++) 59 | cout << an[i] << ","; 60 | cout << endl; 61 | double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1 }; 62 | mysort(d + 1, d + 5, Greater3); //将数组从下标1到下标4从小到大排序 63 | for (int i = 0; i < 6; i++) 64 | cout << d[i] << ","; 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /MOOC/034:goodcopy.cpp: -------------------------------------------------------------------------------- 1 | /***************************************************************************************** 2 | 034:goodcopy 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 编写GoodCopy类模板,使得程序按指定方式输出 6 | 7 | 输入 8 | 第一行是整数 t,表示数据组数 9 | 每组数据: 10 | 第一行是整数 n , n < 50 11 | 第二行是 n 个整数 12 | 第三行是 n 个字符串 13 | 14 | 输出 15 | 将输入的整数原序输出两次,用","分隔 16 | 然后将输入的字符串原序输出两次,也用 ","分隔 17 | 18 | 样例输入 19 | 2 20 | 4 21 | 1 2 3 4 22 | Tom Jack Marry Peking 23 | 1 24 | 0 25 | Ted 26 | 27 | 样例输出 28 | 1,2,3,4, 29 | 1,2,3,4, 30 | Tom,Jack,Marry,Peking, 31 | Tom,Jack,Marry,Peking, 32 | 0, 33 | 0, 34 | Ted, 35 | Ted, 36 | ******************************************************************************************/ 37 | #include 38 | using namespace std; 39 | 40 | template 41 | struct GoodCopy { 42 | // 在此处补充你的代码 43 | public: 44 | void operator()(T* first, T* last, T* dest) 45 | { 46 | int i; 47 | T* tmp = new T[(last - first)*sizeof(T)]; 48 | for (i=0;i 67 | void Print(T s, T e) { 68 | for (; s != e; ++s) 69 | cout << *s << ","; 70 | cout << endl; 71 | } 72 | 73 | int main() 74 | { 75 | int t; 76 | cin >> t; 77 | while (t--) { 78 | int m; 79 | cin >> m; 80 | for (int i = 0; i < m; ++i) 81 | cin >> a[i]; 82 | GoodCopy()(a, a + m, b); 83 | Print(b, b + m); 84 | GoodCopy()(a, a + m, a + m / 2); 85 | Print(a + m / 2, a + m / 2 + m); 86 | 87 | for (int i = 0; i < m; ++i) 88 | cin >> c[i]; 89 | GoodCopy()(c, c + m, d); 90 | Print(c, c + m); 91 | GoodCopy()(c, c + m, c + m / 2); 92 | Print(c + m / 2, c + m / 2 + m); 93 | } 94 | return 0; 95 | } 96 | 97 | 98 | -------------------------------------------------------------------------------- /MOOC/035:按距离排序.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************************************* 2 | 035:按距离排序 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空,输出指定结果 6 | 7 | 输入 8 | 多组数据,每组一行,是一个整数n和一个字符串s 9 | 10 | 输出 11 | 定义两个整数的距离为两个整数差的绝对值 12 | 定义两个字符串的距离为两个字符串长度差的绝对值 13 | 14 | 对每组数据: 15 | 对数组a按和n的距离从小到大排序后输出。距离相同的,值小的排在前面。 16 | 然后对数组b,按照和s的距离从小到大输出。距离相同的,字典序小的排在前面 17 | 18 | 样例输入 19 | 2 a123456 20 | 4 a12345 21 | 22 | 样例输出 23 | 1,3,0,4,7,8,9,10,15,20, 24 | American,Peking,123456789,Jack,To,abcdefghijklmnop, 25 | 4,3,1,7,0,8,9,10,15,20, 26 | Peking,American,Jack,123456789,To,abcdefghijklmnop, 27 | **********************************************************************************************/ 28 | #include 29 | #include 30 | #include 31 | #include 32 | using namespace std; 33 | template 34 | struct Closer { 35 | // 在此处补充你的代码 36 | T1 base; 37 | T2 func; 38 | public: 39 | Closer(T1 n,T2 op):base(n),func(op){} 40 | bool operator()(const T1& a,const T1& b) 41 | { 42 | T1 tmp1,tmp2; 43 | tmp1= func(a, base); 44 | tmp2 = func(b, base); 45 | if (tmp1 == tmp2) return a < b; 46 | else return tmp1 < tmp2; 47 | } 48 | // 在此处补充你的代码 49 | }; 50 | 51 | int Distance1(int n1, int n2) { 52 | return abs(n1 - n2); 53 | } 54 | int Distance2(const string& s1, const string& s2) 55 | { 56 | return abs((int)s1.length() - (int)s2.length()); 57 | } 58 | int a[10] = { 0,3,1,4,7,9,20,8,10,15 }; 59 | string b[6] = { "American","Jack","To","Peking","abcdefghijklmnop","123456789" }; 60 | int main() 61 | { 62 | int n; string s; 63 | while (cin >> n >> s) { 64 | sort(a, a + 10, Closer(n, Distance1)); 65 | for (int i = 0; i < 10; ++i) 66 | cout << a[i] << ","; 67 | cout << endl; 68 | sort(b, b + 6, Closer(s, Distance2)); 69 | for (int i = 0; i < 6; ++i) 70 | cout << b[i] << ","; 71 | cout << endl; 72 | } 73 | return 0; 74 | } 75 | 76 | -------------------------------------------------------------------------------- /MOOC/036:很难蒙混过关的CArray3d三维数组模板类.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************************************** 2 | 036:很难蒙混过关的CArray3d三维数组模板类 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 实现一个三维数组模版CArray3D,可以用来生成元素为任意类型变量的三维数组,输出指定结果 6 | 7 | 输入 8 | 无 9 | 10 | 输出 11 | 等同于样例 12 | 13 | 样例输入 14 | 无 15 | 16 | 样例输出 17 | layer 0: 18 | 0,1,2,3,4, 19 | 5,6,7,8,9, 20 | 10,11,12,13,14, 21 | 15,16,17,18,19, 22 | layer 1: 23 | 20,21,22,23,24, 24 | 25,26,27,28,29, 25 | 30,31,32,33,34, 26 | 35,36,37,38,39, 27 | layer 2: 28 | 40,41,42,43,44, 29 | 45,46,47,48,49, 30 | 50,51,52,53,54, 31 | 55,56,57,58,59, 32 | layer 0: 33 | 0,1,2,3,4, 34 | 5,6,7,8,9, 35 | 10,11,12,13,14, 36 | 15,16,17,18,19, 37 | layer 1: 38 | -1,-1,-1,-1,-1, 39 | -1,-1,-1,-1,-1, 40 | -1,-1,-1,-1,-1, 41 | -1,-1,-1,-1,-1, 42 | layer 2: 43 | 40,41,42,43,44, 44 | 45,46,47,48,49, 45 | 50,51,52,53,54, 46 | 55,56,57,58,59, 47 | layer 0: 48 | 0,1,2,3,4, 49 | 5,6,7,8,9, 50 | 10,11,12,13,14, 51 | 15,16,17,18,19, 52 | layer 1: 53 | -1,-1,-1,-1,-1, 54 | 0,0,0,0,0, 55 | -1,-1,-1,-1,-1, 56 | -1,-1,-1,-1,-1, 57 | layer 2: 58 | 40,41,42,43,44, 59 | 45,46,47,48,49, 60 | 50,51,52,53,54, 61 | 55,56,57,58,59, 62 | layer 0: 63 | 10,5, 64 | 5,3.33333, 65 | layer 1: 66 | 5,3.33333, 67 | 3.33333,2.5, 68 | layer 2: 69 | 3.33333,2.5, 70 | 2.5,2, 71 | **** 72 | 7,3.33333 73 | 74 | 提示 75 | 建议做法: 76 | 1. a[i][j][k] 这个表达式的第一个[]返回一个内部类的对象,该内部类也重载了[],且返回值为指针。 77 | 2. 必要时需重载对象到指针的强制类型转换运算符 78 | ***********************************************************************************************/ 79 | #include 80 | #include 81 | #include 82 | using namespace std; 83 | template 84 | class CArray3D 85 | { 86 | // 在此处补充你的代码 87 | #if 0 //memset处有问题 88 | int block, row, col; 89 | T*** ptr; 90 | public: 91 | CArray3D(int a, int b, int c) :block(a), row(b), col(c) 92 | { 93 | ptr = new T * *[block]; 94 | for (int i = 0; i < block; ++i) 95 | { 96 | ptr[i] = new T * [row]; 97 | for (int j = 0; j < row; ++j) 98 | { 99 | ptr[i][j] = new T[col]; 100 | } 101 | } 102 | } 103 | ~CArray3D() { delete[]ptr; } 104 | T** operator[](int i) 105 | { 106 | return ptr[i]; 107 | } 108 | #endif 109 | #if 0 //ok 110 | class CArray2D { 111 | int y, z; 112 | T* p; 113 | public: 114 | ~CArray2D() { delete[]p; } 115 | void set(int m, int n) 116 | { 117 | y = m, z = n; 118 | p = new T[m * n]; 119 | } 120 | T* operator[](int k) 121 | { 122 | return p + k * z; 123 | } 124 | operator T* () 125 | { 126 | return this->p; 127 | } 128 | }; 129 | int x; 130 | CArray2D* ptoa; 131 | public: 132 | CArray3D(int m, int n, int k) :x(m) 133 | { 134 | ptoa = new CArray2D[x]; 135 | for (int i = 0; i < x; i++) 136 | ptoa[i].set(n, k); 137 | } 138 | ~CArray3D() { delete[]ptoa; } 139 | CArray2D& operator[](int k) 140 | { 141 | return ptoa[k]; 142 | } 143 | #endif 144 | #if 1 //ok 145 | public: 146 | class CArray2D { 147 | public: 148 | int y, z; 149 | T** p; 150 | ~CArray2D() { delete[]p; } 151 | void set(int m, int n) 152 | { 153 | y = m, z = n; 154 | p = new T*[m]; 155 | for (int i = 0; i < m; ++i) 156 | { 157 | p[i] = new T[n]; 158 | } 159 | } 160 | T*& operator[](int k) 161 | { 162 | return p[k]; 163 | } 164 | operator T* () 165 | { 166 | return *this->p; 167 | } 168 | }; 169 | int x; 170 | CArray2D* ptoa; 171 | CArray3D(int m, int n, int k) :x(m) 172 | { 173 | ptoa = new CArray2D[x]; 174 | for (int i = 0; i < x; i++) 175 | ptoa[i].set(n, k); 176 | } 177 | ~CArray3D() { delete[]ptoa; } 178 | CArray2D& operator[](int k) 179 | { 180 | return ptoa[k]; 181 | } 182 | #endif 183 | // 在此处补充你的代码 184 | }; 185 | 186 | CArray3D a(3, 4, 5); 187 | CArray3D b(3, 2, 2); 188 | void PrintA() 189 | { 190 | for (int i = 0; i < 3; ++i) { 191 | cout << "layer " << i << ":" << endl; 192 | for (int j = 0; j < 4; ++j) { 193 | for (int k = 0; k < 5; ++k) 194 | cout << a[i][j][k] << ","; 195 | cout << endl; 196 | } 197 | } 198 | } 199 | void PrintB() 200 | { 201 | for (int i = 0; i < 3; ++i) { 202 | cout << "layer " << i << ":" << endl; 203 | for (int j = 0; j < 2; ++j) { 204 | for (int k = 0; k < 2; ++k) 205 | cout << b[i][j][k] << ","; 206 | cout << endl; 207 | } 208 | } 209 | } 210 | 211 | int main() 212 | { 213 | int No = 0; 214 | for (int i = 0; i < 3; ++i) 215 | { 216 | a[i]; 217 | for (int j = 0; j < 4; ++j) 218 | { 219 | a[j][i]; 220 | for (int k = 0; k < 5; ++k) 221 | a[i][j][k] = No++; 222 | a[j][i][i]; 223 | } 224 | } 225 | PrintA(); 226 | memset(a[1], -1, 20 * sizeof(int)); 227 | memset(a[1], -1, 20 * sizeof(int)); 228 | PrintA(); 229 | memset(a[1][1], 0, 5 * sizeof(int)); 230 | PrintA(); 231 | 232 | for (int i = 0; i < 3; ++i) 233 | for (int j = 0; j < 2; ++j) 234 | for (int k = 0; k < 2; ++k) 235 | b[i][j][k] = 10.0 / (i + j + k + 1); 236 | PrintB(); 237 | int n = a[0][1][2]; 238 | double f = b[0][1][1]; 239 | cout << "****" << endl; 240 | cout << n << "," << f << endl; 241 | 242 | return 0; 243 | } 244 | 245 | -------------------------------------------------------------------------------- /MOOC/037:函数对象的过滤器.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | 037:函数对象的过滤器 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空输出指定结果 6 | 7 | 输入 8 | 多组数据 9 | 每组数据两行 10 | 11 | 第一行是两个整数 m 和 n 12 | 第二行先是一个整数k ,然后后面跟着k个整数 13 | 14 | 输出 15 | 对每组数据,按原顺序输出第二行的后k个整数中,大于m且小于n的数 16 | 输出两遍 17 | 数据保证一定能找到符合要求的整数 18 | 19 | 样例输入 20 | 1 3 21 | 1 2 22 | 2 8 23 | 5 1 2 3 4 9 24 | 25 | 样例输出 26 | 2, 27 | 2, 28 | 3,4, 29 | 3,4, 30 | *******************************************************************************/ 31 | #include 32 | #include 33 | using namespace std; 34 | struct A { 35 | int v; 36 | A() { } 37 | A(int n) :v(n) { }; 38 | bool operator<(const A& a) const { 39 | return v < a.v; 40 | } 41 | }; 42 | // 在此处补充你的代码 43 | template 44 | class FilterClass { 45 | T a, b; 46 | public: 47 | FilterClass(int m,int n):a(m),b(n){} 48 | bool operator()(T& t) 49 | { 50 | if (a < t&& t < b) return true; 51 | else return false; 52 | } 53 | }; 54 | // 在此处补充你的代码 55 | template 56 | void Print(T s, T e) 57 | { 58 | for (; s != e; ++s) 59 | cout << *s << ","; 60 | cout << endl; 61 | } 62 | template 63 | T2 Filter(T1 s, T1 e, T2 s2, T3 op) 64 | { 65 | for (; s != e; ++s) { 66 | if (op(*s)) { 67 | *s2 = *s; 68 | ++s2; 69 | } 70 | } 71 | return s2; 72 | } 73 | ostream& operator <<(ostream& o, A& a) 74 | { 75 | o << a.v; 76 | return o; 77 | } 78 | vector ia; 79 | vector aa; 80 | int main() 81 | { 82 | int m, n; 83 | while (cin >> m >> n) { 84 | ia.clear(); 85 | aa.clear(); 86 | int k, tmp; 87 | cin >> k; 88 | for (int i = 0; i < k; ++i) { 89 | cin >> tmp; 90 | ia.push_back(tmp); 91 | aa.push_back(tmp); 92 | } 93 | vector ib(k); 94 | vector ab(k); 95 | vector::iterator p = Filter(ia.begin(), ia.end(), ib.begin(), FilterClass(m, n)); 96 | Print(ib.begin(), p); 97 | vector::iterator pp = Filter(aa.begin(), aa.end(), ab.begin(), FilterClass(m, n)); 98 | Print(ab.begin(), pp); 99 | 100 | } 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /MOOC/038:白给的list排序.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************** 2 | 038:白给的list排序 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空,产生指定输出 6 | 7 | 输入 8 | 无 9 | 输出 10 | 9.8,7.3,3.4,2.6,1.2, 11 | ***************************************************************************************/ 12 | #include 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | int main() 18 | { 19 | double a[] = { 1.2, 3.4, 9.8, 7.3, 2.6 }; 20 | list lst(a, a + 5); 21 | lst.sort( 22 | // 在此处补充你的代码 23 | [](double a, double b)->bool 24 | { 25 | if (a > b)return true; 26 | else return false; 27 | } 28 | // 在此处补充你的代码 29 | ); 30 | 31 | for (list::iterator i = lst.begin(); i != lst.end(); ++i) 32 | cout << *i << ","; 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /MOOC/039:我自己的 ostream_iterator.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************************************* 2 | 039:我自己的 ostream_iterator 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 描述 5 | 程序填空输出指定结果 6 | 7 | 输入 8 | 无 9 | 输出 10 | 5,21,14,2,3, 11 | 1.4--5.56--3.2--98.3--3.3-- 12 | **********************************************************************************************/ 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | template 18 | void Copy(T1 s, T1 e, T2 x) 19 | { 20 | for (; s != e; ++s, ++x) 21 | *x = *s; 22 | } 23 | template 24 | class myostream_iteraotr 25 | { 26 | // 在此处补充你的代码 27 | string sep; 28 | ostream& os; 29 | public: 30 | myostream_iteraotr(ostream& o, string s) :os(o), sep(s) {} 31 | void operator++(){} 32 | #if 1 33 | void operator = (T& s) 34 | { 35 | os << s << sep; 36 | } 37 | #else 38 | myostream_iteraotr& operator = (T& s) 39 | { 40 | os << s << sep; 41 | return *this; 42 | } 43 | #endif 44 | myostream_iteraotr& operator * () 45 | { 46 | return *this; 47 | } 48 | // 在此处补充你的代码 49 | }; 50 | int main() 51 | { 52 | const int SIZE = 5; 53 | int a[SIZE] = { 5, 21, 14, 2, 3 }; 54 | double b[SIZE] = { 1.4, 5.56, 3.2, 98.3, 3.3 }; 55 | list lst(a, a + SIZE); 56 | myostream_iteraotr output(cout, ","); 57 | Copy(lst.begin(), lst.end(), output); 58 | cout << endl; 59 | myostream_iteraotr output2(cout, "--"); 60 | Copy(b, b + SIZE, output2); 61 | return 0; 62 | } 63 | 64 | 65 | -------------------------------------------------------------------------------- /MOOC/040:List.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************************* 2 | 040:List 3 | 总时间限制: 4000ms 内存限制: 65536kB 4 | 5 | 描述 6 | 写一个程序完成以下命令: 7 | new id ——新建一个指定编号为id的序列(id<10000) 8 | add id num——向编号为id的序列加入整数num 9 | merge id1 id2——合并序列id1和id2中的数,并将id2清空 10 | unique id——去掉序列id中重复的元素 11 | out id ——从小到大输出编号为id的序列中的元素,以空格隔开 12 | 13 | 输入 14 | 第一行一个数n,表示有多少个命令( n<=200000)。以后n行每行一个命令。 15 | 16 | 输出 17 | 按题目要求输出。 18 | 19 | 样例输入 20 | 16 21 | new 1 22 | new 2 23 | add 1 1 24 | add 1 2 25 | add 1 3 26 | add 2 1 27 | add 2 2 28 | add 2 3 29 | add 2 4 30 | out 1 31 | out 2 32 | merge 1 2 33 | out 1 34 | out 2 35 | unique 1 36 | out 1 37 | 38 | 样例输出 39 | 1 2 3 40 | 1 2 3 4 41 | 1 1 2 2 3 3 4 42 | 43 | 1 2 3 4 44 | **********************************************************************************/ 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | using namespace std; 52 | list all[10005]; 53 | int main() 54 | { 55 | int n; 56 | cin >> n; 57 | for (int i = 1; i <= n; i++) 58 | { 59 | string line; 60 | cin >> line; 61 | if (line == "new") 62 | { 63 | int id; 64 | cin >> id; 65 | } 66 | if (line == "add") 67 | { 68 | int id, num; 69 | cin >> id >> num; 70 | all[id].push_back(num); 71 | all[id].sort(); 72 | } 73 | if (line == "out") 74 | { 75 | int id; 76 | cin >> id; 77 | list::iterator p; 78 | p = all[id].begin(); 79 | if (p != all[id].end()) 80 | { 81 | cout << *p++; 82 | for (; p != all[id].end(); p++) 83 | cout << " " << *p; 84 | } 85 | cout << endl; 86 | } 87 | if (line == "merge") 88 | { 89 | int a, b; 90 | cin >> a >> b; 91 | all[a].merge(all[b]); 92 | all[a].sort(); 93 | } 94 | if (line == "unique") 95 | { 96 | int id; 97 | cin >> id; 98 | all[id].unique(); 99 | } 100 | } 101 | return 0; 102 | } 103 | 104 | -------------------------------------------------------------------------------- /MOOC/041:Set.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************************************************** 2 | 041:Set 3 | 总时间限制: 5000ms 内存限制: 100000kB 4 | 5 | 描述 6 | 现有一整数集(允许有重复元素),初始为空。我们定义如下操作: 7 | add x 把x加入集合 8 | del x 把集合中所有与x相等的元素删除 9 | ask x 对集合中元素x的情况询问 10 | 对每种操作,我们要求进行如下输出。 11 | add 输出操作后集合中x的个数 12 | del 输出操作前集合中x的个数 13 | ask 先输出0或1表示x是否曾被加入集合(0表示不曾加入),再输出当前集合中x的个数,中间用空格格开。 14 | 15 | 输入 16 | 第一行是一个整数n,表示命令数。0<=n<=100000。 17 | 后面n行命令,如Description中所述。 18 | 19 | 输出 20 | 共n行,每行按要求输出。 21 | 22 | 样例输入 23 | 7 24 | add 1 25 | add 1 26 | ask 1 27 | ask 2 28 | del 2 29 | del 1 30 | ask 1 31 | 32 | 样例输出 33 | 1 34 | 2 35 | 1 2 36 | 0 0 37 | 0 38 | 2 39 | 1 0 40 | 41 | 提示 42 | Please use STL’s set and multiset to finish the task 43 | ***********************************************************************************************/ 44 | #include 45 | #include 46 | #include 47 | using namespace std; 48 | int main() 49 | { 50 | multisets; 51 | multisets_erase; 52 | int n; 53 | cin >> n; 54 | for (int i = 1; i <= n; i++) 55 | { 56 | string line; 57 | cin >> line; 58 | if (line == "add") 59 | { 60 | int num; 61 | cin >> num; 62 | s.insert(num); 63 | cout << s.count(num) << endl; 64 | } 65 | if (line == "del") 66 | { 67 | int num; 68 | cin >> num; 69 | int ct; 70 | ct = s.erase(num); 71 | if (ct > 0)s_erase.insert(num); 72 | cout << ct << endl; 73 | } 74 | if (line == "ask") 75 | { 76 | int num; 77 | cin >> num; 78 | if (s.find(num) == s.end() && s_erase.find(num) == s_erase.end()) 79 | { 80 | cout << 0 << " "; 81 | } 82 | else cout << 1 << " "; 83 | cout << s.count(num) << endl; 84 | } 85 | } 86 | return 0; 87 | } 88 | 89 | 90 | -------------------------------------------------------------------------------- /MOOC/042:热血格斗场.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************************************* 2 | 042:热血格斗场 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 5 | 描述 6 | 为了迎接08年的奥运会,让大家更加了解各种格斗运动,facer新开了一家热血格斗场。 7 | 格斗场实行会员制,但是新来的会员不需要交入会费,而只要同一名老会员打一场表演赛,证明自己的实力。 8 | 我们假设格斗的实力可以用一个正整数表示,成为实力值。另外,每个人都有一个唯一的id,也是一个正整数。 9 | 为了使得比赛更好看,每一个新队员都会选择与他实力最为接近的人比赛,即比赛双方的实力值之差的绝对值越小越好, 10 | 如果有两个人的实力值与他差别相同,则他会选择比他弱的那个(显然,虐人必被虐好)。 11 | 不幸的是,Facer一不小心把比赛记录弄丢了,但是他还保留着会员的注册记录。 12 | 现在请你帮facer恢复比赛纪录,按照时间顺序依次输出每场比赛双方的id。 13 | 14 | 输入 15 | 第一行一个数n(0 < n <=100000),表示格斗场新来的会员数(不包括facer)。以后n行每一行两个数,按照入会的时间给出会员的id和实力值。 16 | 一开始,facer就算是会员,id为1,实力值1000000000。输入保证两人的实力值不同。 17 | 18 | 输出 19 | N行,每行两个数,为每场比赛双方的id,新手的id写在前面。 20 | 21 | 样例输入 22 | 3 23 | 2 1 24 | 3 3 25 | 4 2 26 | 27 | 样例输出 28 | 2 1 29 | 3 2 30 | 4 2 31 | ********************************************************************************************************/ 32 | #if 0 33 | // Time Limit Exceeded 34 | #include 35 | #include 36 | #include 37 | using namespace std; 38 | int main() 39 | { 40 | mapMembers; 41 | int newmembers,id,power; 42 | Members.insert(map::value_type(1, 1000000000)); 43 | cin >> newmembers; 44 | for (int i = 1; i <= newmembers; i++) 45 | { 46 | cin >> id >> power; 47 | map::iterator p = Members.begin(); 48 | int base = abs(p->second - power); 49 | map::iterator q = p; 50 | for (; p != Members.end(); ++p) 51 | { 52 | if (base > abs(p->second - power)) 53 | { 54 | base = abs(p->second - power); 55 | q = p; 56 | } 57 | else if (base == abs(p->second - power) && power > p->second) 58 | { 59 | base = abs(p->second - power); 60 | q = p; 61 | } 62 | } 63 | cout << id << " " << q->first << endl; 64 | Members.insert(map::value_type(id, power)); 65 | } 66 | return 0; 67 | } 68 | 69 | #else 70 | //Accepted 71 | #define _CRT_SECURE_NO_WARNINGS 72 | #include 73 | #include 74 | #include 75 | #include 76 | using namespace std; 77 | int main() 78 | { 79 | mapmember; 80 | member.insert(map::value_type(1000000000, 1)); 81 | int newmembers, id, power, adversary; 82 | scanf("%d", &newmembers);//cin >> newmembers; 83 | for (int i = 1; i <= newmembers; ++i) 84 | { 85 | scanf("%d %d", &id, &power);//cin >> id >> power; 86 | pair::iterator, map::iterator>p = member.equal_range(power); 87 | if (p.first == p.second) //没有和power相等的元素,寻找和power相差最小的元素,作为id的对手 88 | { 89 | if (p.first == member.begin()) adversary = p.first->second; //只有一个元素 90 | else 91 | { 92 | //p.first和p.second指向同一个元素,其实力大于power,但是所有大于power的值中差别最小的 93 | --p.first; //其实力小于power,且是所有小与power的值中差别最小的 94 | adversary = p.first->second; //先将其作为对手 95 | if (abs(p.first->first - power) > abs(p.second->first - power)) adversary = p.second->second; //比较哪个差别更小,将其作为对手 96 | } 97 | } 98 | else adversary = p.first->second;//有和power相等的元素,因为输入保证两人的实力值不同,所以这个值也是唯一的,作为id的对手 99 | printf("%d %d\n", id, adversary);//cout << id << " " << adversary << endl; 100 | member.insert(map::value_type(power, id)); 101 | } 102 | return 0; 103 | } 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /MOOC/043:冷血格斗场.cpp: -------------------------------------------------------------------------------- 1 | /*********************************************************************************************** 2 | 043:冷血格斗场 3 | 总时间限制: 1000ms 内存限制: 65536kB 4 | 5 | 描述 6 | 为了迎接08年的奥运会,让大家更加了解各种格斗运动,facer新开了一家冷血格斗场。 7 | 格斗场实行会员制,但是新来的会员不需要交入会费,而只要同一名老会员打一场表演赛,证明自己的实力。 8 | 我们假设格斗的实力可以用一个正整数表示,成为实力值,两人的实力值可以相同。另外,每个人都有一个唯一的id,也是一个正整数。 9 | 为了使得比赛更好看,每一个新队员都会选择与他实力最为接近的人比赛,即比赛双方的实力值之差的绝对值越小越好, 10 | 如果有多个人的实力值与他差别相同,则他会选择id最小的那个。 11 | 不幸的是,Facer一不小心把比赛记录弄丢了,但是他还保留着会员的注册记录。 12 | 现在请你帮facer恢复比赛纪录,按照时间顺序依次输出每场比赛双方的id。 13 | 14 | 输入 15 | 第一行一个数n(0 < n <=100000),表示格斗场新来的会员数(不包括facer)。 16 | 以后n行每一行两个数,按照入会的时间给出会员的id和实力值。 17 | 一开始,facer就算是会员,id为1,实力值1000000000。 18 | 19 | 输出 20 | N行,每行两个数,为每场比赛双方的id,新手的id写在前面。 21 | 22 | 样例输入 23 | 3 24 | 2 3 25 | 3 1 26 | 4 2 27 | 28 | 样例输出 29 | 2 1 30 | 3 2 31 | 4 2 32 | 33 | ***********************************************************************************************/ 34 | #if 0 35 | // Time Limit Exceeded 36 | #define _CRT_SECURE_NO_WARNINGS 37 | #include 38 | #include 39 | #include 40 | #include 41 | using namespace std; 42 | int main() 43 | { 44 | mapMembers; 45 | int newmembers,id,power; 46 | Members.insert(map::value_type(1, 1000000000)); 47 | scanf("%d", &newmembers);//cin >> newmembers; 48 | for (int i = 1; i <= newmembers; i++) 49 | { 50 | scanf("%d %d", &id,&power);//cin >> id >> power; 51 | map::iterator p = Members.begin(); 52 | int base = abs(p->second - power); 53 | map::iterator q = p; 54 | for (; p != Members.end(); ++p) 55 | { 56 | if (base > abs(p->second - power)) 57 | { 58 | base = abs(p->second - power); 59 | q = p; 60 | } 61 | else if (base == abs(p->second - power) && q->first > p->first) //power > p->second) 62 | { 63 | base = abs(p->second - power); 64 | q = p; 65 | } 66 | } 67 | printf("%d %d\n", id, q->first);//cout << id << " " << q->first << endl; 68 | Members.insert(map::value_type(id, power)); 69 | } 70 | return 0; 71 | } 72 | #endif 73 | #if 0 74 | // Wrong Answer 75 | #define _CRT_SECURE_NO_WARNINGS 76 | #include 77 | #include 78 | #include 79 | #include 80 | using namespace std; 81 | int main() 82 | { 83 | multimapmember; 84 | member.insert(multimap::value_type(1000000000, 1)); 85 | int newmembers, id, power, adversary; 86 | scanf("%d", &newmembers);//cin >> newmembers; 87 | for (int i = 1; i <= newmembers; ++i) 88 | { 89 | scanf("%d %d", &id, &power);//cin >> id >> power; 90 | pair::iterator, multimap::iterator>p = member.equal_range(power); 91 | if (p.first == p.second) //没有和power相等的元素,寻找和power相差最小的元素,作为id的对手 92 | { 93 | if (p.first == member.begin()) adversary = p.first->second; //只有一个元素 94 | else 95 | { 96 | //p.first和p.second指向同一个元素,其实力大于power,但是所有大于power的值中差别最小的 97 | --p.first; //其实力小于power,且是所有小与power的值中差别最小的 98 | if (abs(p.first->first - power) > abs(p.second->first - power)) //比较哪个差别更小,将其作为对手 99 | { 100 | //寻找和p.second->first相同的元素,其中id最小的作为对手 101 | pair::iterator, multimap::iterator>pp = member.equal_range(p.second->first); 102 | adversary = pp.first->second; 103 | for (; pp.first != pp.second; ++pp.first) 104 | { 105 | if (adversary > pp.first->second) 106 | { 107 | adversary = pp.first->second; 108 | } 109 | } 110 | } 111 | else if (abs(p.first->first - power) < abs(p.second->first - power)) 112 | { 113 | //寻找和p.first->first相同的元素,其中id最小的作为对手 114 | pair::iterator, multimap::iterator>pp = member.equal_range(p.first->first); 115 | adversary = pp.first->second; 116 | for (; pp.first != pp.second; ++pp.first) 117 | { 118 | if (adversary > pp.first->second) 119 | { 120 | adversary = pp.first->second; 121 | } 122 | } 123 | } 124 | else //abs(p.first->first - power) == abs(p.second->first - power) 125 | { 126 | //寻找和p.second->first相同的元素,找出其最小的id 127 | pair::iterator, multimap::iterator>pp = member.equal_range(p.second->first); 128 | adversary = pp.first->second; 129 | for (; pp.first != pp.second; ++pp.first) 130 | { 131 | if (adversary > pp.first->second) 132 | { 133 | adversary = pp.first->second; 134 | } 135 | } 136 | 137 | //寻找和p.first->first相同的元素,其中id最小的作为对手 138 | pp = member.equal_range(p.first->first); 139 | for (; pp.first != pp.second; ++pp.first) 140 | { 141 | if (adversary > pp.first->second) 142 | { 143 | adversary = pp.first->second; 144 | } 145 | } 146 | } 147 | } 148 | } 149 | else //有和power相等的元素 150 | { 151 | //在这些相同元素中寻找id最小的最为对手 152 | adversary = p.first->second; 153 | for (; p.first != p.second; ++p.first) 154 | { 155 | if (adversary > p.first->second) 156 | { 157 | adversary = p.first->second; 158 | } 159 | } 160 | } 161 | printf("%d %d\n", id, adversary);//cout << id << " " << adversary << endl; 162 | member.insert(multimap::value_type(power, id)); 163 | } 164 | return 0; 165 | } 166 | #endif 167 | #if 1 168 | // Accepted 169 | #include 170 | #include 171 | #include 172 | using namespace std; 173 | class A { 174 | public: 175 | int power; 176 | int id; 177 | A(int m, int n) :power(m), id(n) {} 178 | }; 179 | class Cmp { 180 | public: 181 | bool operator()(const A& m, const A& n)const { 182 | return (m.power < n.power || (m.power == n.power && m.id < n.id)); 183 | } 184 | }; 185 | int main() 186 | { 187 | multimap member; 188 | multimap::iterator i, j; 189 | int n; 190 | cin >> n; 191 | A tmp(1000000000, 1); 192 | member.insert(pair(tmp, 1)); 193 | for (int k = 1; k <= n; ++k) 194 | { 195 | int id, power, adver = 1, adverp; 196 | int min = 1000000000; 197 | scanf("%d%d", &id, &power); 198 | tmp.id = id; tmp.power = power; 199 | i = member.lower_bound(tmp); 200 | j = member.upper_bound(tmp); 201 | if (i == j) 202 | { 203 | if (i != member.begin()) 204 | { 205 | --i; 206 | adverp = i->first.power; 207 | min = abs(power - adverp); 208 | while (i->first.power == adverp) 209 | { 210 | adver = i->first.id; 211 | if (i == member.begin())break; 212 | --i; 213 | } 214 | if (min > j->first.power - power || (min == j->first.power - power && j->first.id < adver)) 215 | adver = j->first.id; 216 | } 217 | else adver = i->first.id; 218 | } 219 | else 220 | { 221 | adverp = i->first.power; 222 | while (i->first.power == adverp) 223 | { 224 | adver = i->first.id; 225 | if (i == member.begin())break; 226 | --i; 227 | } 228 | } 229 | printf("%d %d\n", id, adver); 230 | member.insert(pair(tmp, id)); 231 | } 232 | return 0; 233 | } 234 | #endif 235 | -------------------------------------------------------------------------------- /MOOC/044:编程填空:数据库内的学生信息 .cpp: -------------------------------------------------------------------------------- 1 | /*************************************************************************************************** 2 | 044:编程填空:数据库内的学生信息 3 | 总时间限制: 3000ms 内存限制: 20480kB 4 | 5 | 描述 6 | 7 | 程序填空,使得下面的程序,先输出 8 | (Tom,80),(Tom,70),(Jone,90),(Jack,70),(Alice,100), 9 | (Tom,78),(Tom,78),(Jone,90),(Jack,70),(Alice,100), 10 | (70,Jack),(70,Tom),(80,Tom),(90,Jone),(100,Alice), 11 | (70,Error),(70,Error),(80,Tom),(90,Jone),(100,Alice), 12 | ****** 13 | 然后,再根据输入数据按要求产生输出数据 14 | 15 | 输入 16 | 输入数据的每一行,格式为以下之一: 17 | A name score 18 | Q name score 19 | 20 | name是个不带个空格的字符串,长度小于 20 21 | score是个整数,能用int表示 22 | A name score 表示往数据库中新增一个姓名为name的学生,其分数为score。开始时数据库中一个学生也没有。 23 | Q name 表示在数据库中查询姓名为name的学生的分数 24 | 数据保证学生不重名。 25 | 输入数据少于200,000行。 26 | 27 | 输出 28 | 对于每个查询,输出学生的分数。如果查不到,则输出 "Not Found" 29 | 30 | 样例输入 31 | A Tom1 30 32 | A Tom2 40 33 | Q Tom3 34 | A Tom4 89 35 | Q Tom1 36 | Q Tom2 37 | 38 | 样例输出 39 | (Tom,80),(Tom,70),(Jone,90),(Jack,70),(Alice,100), 40 | (Tom,78),(Tom,78),(Jone,90),(Jack,70),(Alice,100), 41 | (70,Jack),(70,Tom),(80,Tom),(90,Jone),(100,Alice), 42 | (70,Error),(70,Error),(80,Tom),(90,Jone),(100,Alice), 43 | ****** 44 | Not Found 45 | 30 46 | 40 47 | 48 | 提示 49 | 1) 编写模板的时候,连续的两个 “>”最好要用空格分开,以免被编译器看作是 ">>"运算符。VS可能无此问题,但是Dev C++和服务器上的编译环境会有这个问题。 50 | 比如 vector> 有可能出错,要改成 vector > 51 | 52 | 2) 在模板中写迭代器时,最好在前面加上 typename关键字,否则可能会编译错。VS可能无此问题,但是Dev C++和服务器上的编译环境会有这个问题。 53 | ***************************************************************************************************/ 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | using namespace std; 60 | // 在此处补充你的代码 61 | #if 0 62 | #include 63 | template> 64 | #else 65 | template 66 | class cmp { 67 | public: 68 | bool operator ()(const Key&a, const Key& b)const 69 | { 70 | return a > b; 71 | } 72 | }; 73 | template> 74 | #endif 75 | class MyMultimap{ 76 | public: 77 | typedef multimap mmp; 78 | typedef typename multimap::iterator iterator; 79 | mmp mp; 80 | iterator insert(pair a) 81 | { 82 | return mp.insert(a); 83 | } 84 | iterator begin() 85 | { 86 | return mp.begin(); 87 | } 88 | iterator end() 89 | { 90 | return mp.end(); 91 | } 92 | void Set(Key k, T t) 93 | { 94 | pairrange = mp.equal_range(k); 95 | for (; range.first != range.second; ++range.first) 96 | { 97 | range.first->second = t; 98 | } 99 | } 100 | iterator find(Key n) 101 | { 102 | return mp.find(n); 103 | } 104 | void clear() 105 | { 106 | mp.clear(); 107 | } 108 | }; 109 | template 110 | ostream& operator<<(ostream&os, pair m) 111 | { 112 | os << "(" << m.first << "," << m.second << ")"; 113 | return os; 114 | } 115 | // 在此处补充你的代码 116 | struct Student 117 | { 118 | string name; 119 | int score; 120 | }; 121 | template 122 | void Print(T first, T last) 123 | { 124 | for (; first != last; ++first) 125 | cout << *first << ","; 126 | cout << endl; 127 | } 128 | int main() 129 | { 130 | Student s[] = { { "Tom", 80 }, { "Jack", 70 }, { "Jone", 90 }, { "Tom", 70 }, { "Alice", 100 } }; 131 | 132 | MyMultimap mp; 133 | for (int i = 0; i<5; ++i) 134 | mp.insert(make_pair(s[i].name, s[i].score)); 135 | Print(mp.begin(), mp.end()); //按姓名从大到小输出 136 | 137 | mp.Set("Tom", 78); //把所有名为"Tom"的学生的成绩都设置为78 138 | Print(mp.begin(), mp.end()); 139 | 140 | MyMultimap > mp2; 141 | for (int i = 0; i<5; ++i) 142 | mp2.insert(make_pair(s[i].score, s[i].name)); 143 | 144 | Print(mp2.begin(), mp2.end()); //按成绩从小到大输出 145 | mp2.Set(70, "Error"); //把所有成绩为70的学生,名字都改为"Error" 146 | Print(mp2.begin(), mp2.end()); 147 | cout << "******" << endl; 148 | 149 | mp.clear(); 150 | 151 | string name; 152 | string cmd; 153 | int score; 154 | while (cin >> cmd) 155 | { 156 | if (cmd == "A") 157 | { 158 | cin >> name >> score; 159 | if (mp.find(name) != mp.end()) 160 | { 161 | cout << "erroe" << endl; 162 | } 163 | mp.insert(make_pair(name, score)); 164 | } 165 | else if (cmd == "Q") 166 | { 167 | cin >> name; 168 | MyMultimap::iterator p = mp.find(name); 169 | if (p != mp.end()) 170 | { 171 | cout << p->second << endl; 172 | } 173 | else 174 | { 175 | cout << "Not Found" << endl; 176 | } 177 | } 178 | } 179 | return 0; 180 | } 181 | 182 | 183 | -------------------------------------------------------------------------------- /MOOC/045:魔兽世界三(开战).cpp: -------------------------------------------------------------------------------- 1 | /*********************************************************************************************** 2 | 045:魔兽世界三(开战) 3 | 总时间限制: 2000ms 内存限制: 65536kB 4 | 5 | 描述 6 | 魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市,城市从西向东依次编号为1,2,3 .... N ( N <= 20)。 7 | 红魔军的司令部算作编号为0的城市,蓝魔军的司令部算作编号为N+1的城市。司令部有生命元,用于制造武士。 8 | 9 | 两军的司令部都会制造武士。武士一共有dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值、攻击力这三种属性。 10 | 11 | 双方的武士编号都是从1开始计算。红方制造出来的第n 个武士,编号就是n。同样,蓝方制造出来的第n 个武士,编号也是n。 12 | 13 | 武士在刚降生的时候有一个初始的生命值,生命值在战斗中会发生变化,如果生命值减少到0(生命值变为负数时应当做变为0处理),则武士死亡(消失)。 14 | 武士可以拥有武器。武器有三种,sword, bomb,和arrow,编号分别为0,1,2。 15 | sword的攻击力是使用者当前攻击力的20%(去尾取整)。 16 | bomb的攻击力是使用者当前攻击力的40%(去尾取整),但是也会导致使用者受到攻击,对使用者的攻击力是对敌人取整后的攻击力的1/2(去尾取整)。Bomb一旦使用就没了。 17 | arrow的攻击力是使用者当前攻击力的30%(去尾取整)。一个arrow用两次就没了。 18 | 19 | 武士降生后就朝对方司令部走,在经过的城市如果遇到敌人(同一时刻每个城市最多只可能有1个蓝武士和一个红武士),就会发生战斗。战斗的规则是: 20 | 1.在奇数编号城市,红武士先发起攻击 21 | 2.在偶数编号城市,蓝武士先发起攻击 22 | 3.战斗开始前,双方先对自己的武器排好使用顺序,然后再一件一件地按顺序使用。编号小的武器,排在前面。若有多支arrow,用过的排在前面。 23 | 排好序后,攻击者按此排序依次对敌人一件一件地使用武器。如果一种武器有多件,那就都要用上。每使用一件武器,被攻击者生命值要减去武器攻击力。 24 | 如果任何一方生命值减为0或小于0即为死去。有一方死去,则战斗结束。 25 | 4.双方轮流使用武器,甲用过一件,就轮到乙用。某一方把自己所有的武器都用过一轮后,就从头开始再用一轮。 26 | 如果某一方没有武器了,那就挨打直到死去或敌人武器用完。武器排序只在战斗前进行,战斗中不会重新排序。 27 | 5.如果双方武器都用完且都还活着,则战斗以平局结束。如果双方都死了,也算平局。 28 | 6.有可能由于武士自身攻击力太低,而导致武器攻击力为0。攻击力为0的武器也要使用。如果战斗中双方的生命值和武器的状态都不再发生变化,则战斗结束,算平局。 29 | 7.战斗的胜方获得对方手里的武器。武士手里武器总数不超过10件。缴获武器时,按照武器种类编号从小到大缴获。如果有多件arrow,优先缴获没用过的。 30 | 8.如果战斗开始前双方都没有武器,则战斗视为平局。如果先攻击方没有武器,则由后攻击方攻击。 31 | 32 | 不同的武士有不同的特点。 33 | 编号为n的dragon降生时即获得编号为n%3 的武器。dragon在战斗结束后,如果还没有战死,就会欢呼。 34 | 35 | 编号为n的ninjia降生时即获得编号为n%3 和(n+1)%3的武器。ninja 使用bomb不会让自己受伤。 36 | 37 | 编号为n的iceman降生时即获得编号为n%3 的武器。iceman每前进一步,生命值减少10%(减少的量要去尾取整)。 38 | 39 | 编号为n的lion降生时即获得编号为n%3 的武器。lion 有“忠诚度”这个属性,其初始值等于它降生之后其司令部剩余生命元的数目。每前进一步忠诚度就降低K。 40 | 忠诚度降至0或0以下,则该lion逃离战场,永远消失。但是已经到达敌人司令部的lion不会逃跑。lion在己方司令部可能逃跑。 41 | 42 | wolf降生时没有武器,但是在战斗开始前会抢到敌人编号最小的那种武器。如果敌人有多件这样的武器,则全部抢来。Wolf手里武器也不能超过10件。 43 | 如果敌人arrow太多没法都抢来,那就先抢没用过的。如果敌人也是wolf,则不抢武器。 44 | 45 | 以下是不同时间会发生的不同事件: 46 | 在每个整点,即每个小时的第0分, 双方的司令部中各有一个武士降生。 47 | 红方司令部按照iceman、lion、wolf、ninja、dragon 的顺序制造武士。 48 | 蓝方司令部按照lion、dragon、ninja、iceman、wolf 的顺序制造武士。 49 | 50 | 制造武士需要生命元。 51 | 制造一个初始生命值为m 的武士,司令部中的生命元就要减少m 个。 52 | 如果司令部中的生命元不足以制造某本该造的武士,那就从此停止制造武士。 53 | 54 | 在每个小时的第5分,该逃跑的lion就在这一时刻逃跑了。 55 | 在每个小时的第10分:所有的武士朝敌人司令部方向前进一步。即从己方司令部走到相邻城市,或从一个城市走到下一个城市。或从和敌军司令部相邻的城市到达敌军司令部。 56 | 在每个小时的第35分:在有wolf及其敌人的城市,wolf要抢夺对方的武器。 57 | 在每个小时的第40分:在有两个武士的城市,会发生战斗。 58 | 在每个小时的第50分,司令部报告它拥有的生命元数量。 59 | 在每个小时的第55分,每个武士报告其拥有的武器情况。 60 | 武士到达对方司令部后就算完成任务了,从此就呆在那里无所事事。 61 | 任何一方的司令部里若是出现了敌人,则认为该司令部已被敌人占领。 62 | 任何一方的司令部被敌人占领,则战争结束。战争结束之后就不会发生任何事情了。 63 | 64 | 给定一个时间,要求你将从0点0分开始到此时间为止的所有事件按顺序输出。事件及其对应的输出样例如下: 65 | 1) 武士降生 66 | 输出样例:000:00 blue dragon 1 born 67 | 表示在0点0分,编号为1的蓝魔dragon武士降生 68 | 69 | 如果造出的是lion,那么还要多输出一行,例: 70 | 000:00 blue lion 1 born 71 | Its loyalty is 24 72 | 表示该lion降生时的忠诚度是24 73 | 74 | 2) lion逃跑 75 | 输出样例:000:05 blue lion 1 ran away 76 | 表示在0点5分,编号为1的蓝魔lion武士逃走 77 | 78 | 3) 武士前进到某一城市 79 | 输出样例: 80 | 000:10 red iceman 1 marched to city 1 with 20 elements and force 30 81 | 表示在0点10分,红魔1号武士iceman前进到1号城市,此时他生命值为20,攻击力为30 82 | 对于iceman,输出的生命值应该是变化后的数值 83 | 84 | 4) wolf抢敌人的武器 85 | 000:35 blue wolf 2 took 3 bomb from red dragon 2 in city 4 86 | 表示在0点35分,4号城市中,红魔1号武士wolf 抢走蓝魔2号武士dragon 3个bomb。为简单起见,武器不写复数形式 87 | 88 | 5) 报告战斗情况 89 | 战斗只有3种可能的输出结果: 90 | 000:40 red iceman 1 killed blue lion 12 in city 2 remaining 20 elements 91 | 表示在0点40分,1号城市中,红魔1号武士iceman 杀死蓝魔12号武士lion后,剩下生命值20 92 | 000:40 both red iceman 1 and blue lion 12 died in city 2 93 | 注意,把红武士写前面 94 | 000:40 both red iceman 1 and blue lion 12 were alive in city 2 95 | 注意,把红武士写前面 96 | 97 | 6) 武士欢呼 98 | 输出样例:003:40 blue dragon 2 yelled in city 4 99 | 100 | 7) 武士抵达敌军司令部 101 | 输出样例:001:10 red iceman 1 reached blue headquarter with 20 elements and force 30 102 | (此时他生命值为20,攻击力为30)对于iceman,输出的生命值和攻击力应该是变化后的数值 103 | 104 | 8) 司令部被占领 105 | 输出样例:003:10 blue headquarter was taken 106 | 107 | 9)司令部报告生命元数量 108 | 000:50 100 elements in red headquarter 109 | 000:50 120 elements in blue headquarter 110 | 表示在0点50分,红方司令部有100个生命元,蓝方有120个 111 | 112 | 10)武士报告情况 113 | 000:55 blue wolf 2 has 2 sword 3 bomb 0 arrow and 7 elements 114 | 为简单起见,武器都不写复数形式。elements一律写复数,哪怕只有1个 115 | 交代武器情况时,次序依次是:sword,bomb, arrow。 116 | 117 | 输出事件时: 118 | 首先按时间顺序输出; 119 | 同一时间发生的事件,按发生地点从西向东依次输出. 武士前进的事件, 算是发生在目的地。 120 | 在一次战斗中有可能发生上面的 5 至 6 号事件。这些事件都算同时发生,其时间就是战斗开始时间。一次战斗中的这些事件,序号小的应该先输出。 121 | 两个武士同时抵达同一城市,则先输出红武士的前进事件,后输出蓝武士的。 122 | 对于同一城市,同一时间发生的事情,先输出红方的,后输出蓝方的。 123 | 显然,8号事件发生之前的一瞬间一定发生了7号事件。输出时,这两件事算同一时间发生,但是应先输出7号事件 124 | 虽然任何一方的司令部被占领之后,就不会有任何事情发生了。但和司令部被占领同时发生的事件,全都要输出。 125 | 126 | 127 | 输入 128 | 第一行是t,代表测试数据组数 129 | 每组样例共三行。 130 | 131 | 第一行,4个整数 M,N,K, T。其含义为: 132 | 每个司令部一开始都有M个生命元( 1 <= M <= 100000) 133 | 两个司令部之间一共有N个城市( 1 <= N <= 20 ) 134 | lion每前进一步,忠诚度就降低K。(0<=K<=100) 135 | 要求输出从0时0分开始,到时间T为止(包括T) 的所有事件。T以分钟为单位,0 <= T <= 6000 136 | 137 | 第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于200 138 | 139 | 第三行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的攻击力。它们都大于0小于等于200 140 | 141 | 142 | 输出 143 | 对每组数据,先输出一行: 144 | Case n: 145 | 如对第一组数据就输出 Case 1: 146 | 然后按恰当的顺序和格式输出到时间T为止发生的所有事件。每个事件都以事件发生的时间开头,时间格式是“时: 分”,“时”有三位,“分”有两位。 147 | 148 | 样例输入 149 | 1 150 | 20 1 10 400 151 | 20 20 30 10 20 152 | 5 5 5 5 5 153 | 154 | 样例输出 155 | Case 1: 156 | 000:00 blue lion 1 born 157 | Its loyalty is 10 158 | 000:10 blue lion 1 marched to city 1 with 10 elements and force 5 159 | 000:50 20 elements in red headquarter 160 | 000:50 10 elements in blue headquarter 161 | 000:55 blue lion 1 has 0 sword 1 bomb 0 arrow and 10 elements 162 | 001:05 blue lion 1 ran away 163 | 001:50 20 elements in red headquarter 164 | 001:50 10 elements in blue headquarter 165 | 002:50 20 elements in red headquarter 166 | 002:50 10 elements in blue headquarter 167 | 003:50 20 elements in red headquarter 168 | 003:50 10 elements in blue headquarter 169 | 004:50 20 elements in red headquarter 170 | 004:50 10 elements in blue headquarter 171 | 005:50 20 elements in red headquarter 172 | 005:50 10 elements in blue headquarter 173 | 提示 174 | 请注意浮点数精度误差问题。OJ上的编译器编译出来的可执行程序,在这方面和你电脑上执行的程序很可能会不一致。 175 | 5 * 0.3 的结果,有的机器上可能是 15.00000001,去尾取整得到15,有的机器上可能是14.9999999,去尾取整后就变成14。 176 | 因此,本题不要写 5 * 0.3,要写 5 * 3 / 10。 177 | ************************************************************************************************/ 178 | 179 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C++ 2 | 新标准C++程序设计---郭炜 3 | -------------------------------------------------------------------------------- /n皇后.cpp: -------------------------------------------------------------------------------- 1 | /***************************************************************************************** 2 | N皇后问题: 3 | n个皇后,摆放在n×n的棋盘上,任意2个皇后,不能在同一行同一列,且不能在对角线。 4 | 求所有的摆放方式。 5 | *****************************************************************************************/ 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | bool Valid(int rows, const vector&pos) //前rows行皇后是否有冲突,行从0算起 12 | {//pos各行皇后的位置,即在第几列,从0算起 13 | for (int i = 0; i < rows; ++i) 14 | { 15 | for (int j = 0; j < i; ++j) 16 | { 17 | //pos[i] == pos[j]表示这两行的皇后在同一位置,即同一列 18 | //abs(i - j) == abs(pos[i] - pos[j])表示这两行皇后的位置在对角线上 19 | if (pos[i] == pos[j] || abs(i - j) == abs(pos[i] - pos[j])) 20 | return false; 21 | } 22 | } 23 | return true; 24 | } 25 | int main() 26 | { 27 | int n; 28 | cin >> n; 29 | vectorpos(n); 30 | for (int i = 0; i < n; ++i) //最小的排列方式0,1,2...n-1,所有皇后在一个对角线上 31 | pos[i] = i; 32 | while (next_permutation(pos.begin(), pos.end())) //从最小的排列开始,遍历所有排列方式,从中判断符合条件的排列 33 | { 34 | if (Valid(n, pos)) 35 | { 36 | for (int k = 0; k < n; ++k) 37 | { 38 | cout << pos[k] << " "; 39 | } 40 | cout << endl; 41 | } 42 | } 43 | return 0; 44 | } 45 | --------------------------------------------------------------------------------