From:http://blog.csdn.net/ebowtang/article/details/38277903
题目1151:位操作练习
-
题目描述:
-
给出两个不大于65535的非负整数,判断其中一个的16位二进制表示形式,是否能由另一个的16位二进制表示形式经过循环左移若干位而得到。
循环左移和普通左移的区别在于:最左边的那一位经过循环左移一位后就会被移到最右边去。比如:
1011 0000 0000 0001 经过循环左移一位后,变成 0110 0000 0000 0011, 若是循环左移2位,则变成 1100 0000 0000 0110
-
输入:
-
第一行是个整数n, 0 < n < 300000,表示后面还有n行数据
后面是n行,每行有两个不大于65535的非负整数
-
输出:
-
对于每一行的两个整数,输出一行,内容为YES或NO
-
样例输入:
-
4 2 4 9 18 45057 49158 7 12
-
样例输出:
-
YES YES YES NO
题目1153:括号匹配问题
-
题目描述:
-
在某个字符串(长度不超过100)中有左括号、右括号和大小写字母;规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配。写一个程序,找到无法匹配的左括号和右括号,输出原来字符串,并在下一行标出不能匹配的括号。不能匹配的左括号用"$"标注,不能匹配的右括号用"?"标注.
-
输入:
-
输入包括多组数据,每组数据一行,包含一个字符串,只包含左右括号和大小写字母,字符串长度不超过100。
注意:cin.getline(str,100)最多只能输入99个字符!
-
输出:
-
对每组输出数据,输出两行,第一行包含原始输入字符,第二行由"$","?"和空格组成,"$"和"?"表示与之对应的左括号和右括号不能匹配。
-
样例输入:
-
)(rttyy())sss)(
-
样例输出:
-
)(rttyy())sss)( ? ?$
- #include "iostream"
- #include "string"
- #include "stack"
- using namespace std;
- void BracketMatch(const string srcStr);
- int main()
- {
- string str;
- while (cin >> str)
- {
- cout << str << endl;
- BracketMatch(str);
- }
- return 0;
- }
- void BracketMatch(const string srcStr)
- {
- stack<int> s;
- string ansStr(srcStr.length(),'0');
- for (unsigned int i = 0; i < srcStr.length(); i++)//从左往右开始遍历
- {
- if (isalpha(srcStr[i]))
- {
- ansStr[i]=' ';
- continue;
- }
- switch (srcStr[i])
- {
- //对左括号仅作压栈处理,也就是说栈中只可能是左括号
- case '(':
- if (i != (srcStr.length() - 1))//当是最后一个字符时已经没有必要输出空格了
- ansStr[i] = ' ';
- s.push(i);//注意,这里
- break;
- //对右括号做匹配判断
- case ')':
- if (!s.empty())
- {//如果不为空,说明一定有左括号(实际上是其数组下标)
- s.pop();
- ansStr[i] = ' ';
- }
- else//如果为空
- ansStr[i] = '?';
- break;
- default:
- cerr << "错误的括号" << endl;
- }
- }
- while (!s.empty())
- { //解决多余的左括号
- ansStr[s.top()] = '$';
- s.pop();
- }
- cout << ansStr << endl;
- }
- /**************************************************************
- Problem: 1153
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:10 ms
- Memory:1524 kb
- ****************************************************************/
#include "iostream"
#include "string"
#include "stack"
using namespace std;
void BracketMatch(const string srcStr);
int main()
{
string str;
while (cin >> str)
{
cout << str << endl;
BracketMatch(str);
}
return 0;
}
void BracketMatch(const string srcStr)
{
stack<int> s;
string ansStr(srcStr.length(),'0');
for (unsigned int i = 0; i < srcStr.length(); i++)//从左往右开始遍历
{
if (isalpha(srcStr[i]))
{
ansStr[i]=' ';
continue;
}
switch (srcStr[i])
{
//对左括号仅作压栈处理,也就是说栈中只可能是左括号
case '(':
if (i != (srcStr.length() - 1))//当是最后一个字符时已经没有必要输出空格了
ansStr[i] = ' ';
s.push(i);//注意,这里
break;
//对右括号做匹配判断
case ')':
if (!s.empty())
{//如果不为空,说明一定有左括号(实际上是其数组下标)
s.pop();
ansStr[i] = ' ';
}
else//如果为空
ansStr[i] = '?';
break;
default:
cerr << "错误的括号" << endl;
}
}
while (!s.empty())
{ //解决多余的左括号
ansStr[s.top()] = '$';
s.pop();
}
cout << ansStr << endl;
}
/**************************************************************
Problem: 1153
User: EbowTang
Language: C++
Result: Accepted
Time:10 ms
Memory:1524 kb
****************************************************************/
题目1156:谁是你的潜在朋友
-
题目描述:
-
“臭味相投”——这是我们描述朋友时喜欢用的词汇。两个人是朋友通常意味着他们存在着许多共同的兴趣。然而作为一个宅男,你发现自己与他人相互了解的机会并不太多。幸运的是,你意外得到了一份北大图书馆的图书借阅记录,于是你挑灯熬夜地编程,想从中发现潜在的朋友。
首先你对借阅记录进行了一番整理,把N个读者依次编号为1,2,…,N,把M本书依次编号为1,2,…,M。同时,按照“臭味相投”的原则,和你喜欢读同一本书的人,就是你的潜在朋友。你现在的任务是从这份借阅记录中计算出每个人有几个潜在朋友。
-
输入:
-
每个案例第一行两个整数N,M,2 <= N ,M<= 200。接下来有N行,第i(i = 1,2,…,N)行每一行有一个数,表示读者i-1最喜欢的图书的编号P(1<=P<=M)
-
输出:
-
每个案例包括N行,每行一个数,第i行的数表示读者i有几个潜在朋友。如果i和任何人都没有共同喜欢的书,则输出“BeiJu”(即悲剧,^ ^)
-
样例输入:
-
4 5 2 3 2 1
-
样例输出:
-
1 BeiJu 1 BeiJu
- #include <iostream>
- #include <cstring>
- using namespace std;
- int findK(int key,int A[],int s,int n){
- int count = 1;
- for (int i = s + 1; i < n; i++){
- if (A[i]==key){
- count++;
- }
- }
- return count;
- }
- int pCount[200];
- int f[1000];
- int main(){
- int N, M;
- int key;
- while (cin >> N >> M){
- memset(pCount, 0, 200 * sizeof(int));
- for (int i = 0; i < N; i++){
- cin >> key;
- pCount[key]++;
- f[i] = key;
- }
- for (int i = 0; i < N; i++){
- key = f[i];
- if (pCount[key] == 1)
- cout << "BeiJu" << endl;
- else
- cout << pCount[key]-1<< endl;
- }
- }
- return 0;
- }
- /**************************************************************
- Problem: 1156
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:10 ms
- Memory:1524 kb
- ****************************************************************/
#include <iostream>
#include <cstring>
using namespace std;
int findK(int key,int A[],int s,int n){
int count = 1;
for (int i = s + 1; i < n; i++){
if (A[i]==key){
count++;
}
}
return count;
}
int pCount[200];
int f[1000];
int main(){
int N, M;
int key;
while (cin >> N >> M){
memset(pCount, 0, 200 * sizeof(int));
for (int i = 0; i < N; i++){
cin >> key;
pCount[key]++;
f[i] = key;
}
for (int i = 0; i < N; i++){
key = f[i];
if (pCount[key] == 1)
cout << "BeiJu" << endl;
else
cout << pCount[key]-1<< endl;
}
}
return 0;
}
/**************************************************************
Problem: 1156
User: EbowTang
Language: C++
Result: Accepted
Time:10 ms
Memory:1524 kb
****************************************************************/
题目1169:比较奇偶数个数
-
题目描述:
-
第一行输入一个数,为n,第二行输入n个数,这n个数中,如果偶数比奇数多,输出NO,否则输出YES。
-
输入:
-
输入有多组数据。
每组输入n,然后输入n个整数(1<=n<=1000)。
-
输出:
-
如果偶数比奇数多,输出NO,否则输出YES。
-
样例输入:
-
5 1 5 2 4 3
-
样例输出:
-
YES
- #include "vector"
- #include <iostream>
- using namespace std;
- int main()
- {
- int n = 0;
- while (cin >> n)
- {
- vector<int> vec(n);
- int count = 0;
- for (int i = 0; i < n; i++)
- {
- cin >> vec[i];
- if (vec[i] % 2 == 1)
- count++;
- }
- if (count < (n - count))
- cout << "NO" << endl;
- else
- cout << "YES" << endl;
- }
- return 0;
- }
- /**************************************************************
- Problem: 1169
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:10 ms
- Memory:1520 kb
- ****************************************************************/
题目1170:找最小数
-
题目描述:
-
第一行输入一个数n,1 <= n <= 1000,下面输入n行数据,每一行有两个数,分别是x y。输出一组x y,该组数据是所有数据中x最小,且在x相等的情况下y最小的。
-
输入:
-
输入有多组数据。
每组输入n,然后输入n个整数对。
-
输出:
-
输出最小的整数对。
-
样例输入:
-
5 3 3 2 2 5 5 2 1 3 6
-
样例输出:
-
2 1
- #include <iostream>
- #include "algorithm"
- using namespace std;
- typedef struct _Point
- {
- int x;
- int y;
- }Point;
- bool cmpPoint(Point a, Point b)
- {
- if (a.x != b.x)
- return a.x < b.x;
- else
- return a.y<b.y;
- }
- int main(void)
- {
- int n = 0;
- while (cin>>n)
- {
- Point w[1000];
- for (size_t i = 0; i < n; i++)
- cin >> w[i].x >> w[i].y;
- //多关键字排序
- sort(w, w + n, cmpPoint);//功能是先排x,若相等则在其基础上对y进行排序。
- //输出最小点
- cout << w[0].x << " " << w[0].y << endl;
- }
- return 0;
- }
- /**************************************************************
- Problem: 1170
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:10 ms
- Memory:1520 kb
- ****************************************************************/
#include <iostream>
#include "algorithm"
using namespace std;
typedef struct _Point
{
int x;
int y;
}Point;
bool cmpPoint(Point a, Point b)
{
if (a.x != b.x)
return a.x < b.x;
else
return a.y<b.y;
}
int main(void)
{
int n = 0;
while (cin>>n)
{
Point w[1000];
for (size_t i = 0; i < n; i++)
cin >> w[i].x >> w[i].y;
//多关键字排序
sort(w, w + n, cmpPoint);//功能是先排x,若相等则在其基础上对y进行排序。
//输出最小点
cout << w[0].x << " " << w[0].y << endl;
}
return 0;
}
/**************************************************************
Problem: 1170
User: EbowTang
Language: C++
Result: Accepted
Time:10 ms
Memory:1520 kb
****************************************************************/
题目1180:对称矩阵
-
题目描述:
-
输入一个N维矩阵,判断是否对称。
-
输入:
-
输入第一行包括一个数:N(1<=N<=100),表示矩阵的维数。
接下来的N行,每行包括N个数,表示N*N矩阵的元素。
-
输出:
-
可能有多组测试数据,对于每组数据,
输出"Yes!”表示矩阵为对称矩阵。
输出"No!”表示矩阵不是对称矩阵。
-
样例输入:
-
4 16 19 16 6 19 16 14 5 16 14 16 3 6 5 3 16 2 1 2 3 4
-
样例输出:
-
Yes! No!
- #include <iostream>
- #include "algorithm"
- using namespace std;
- int main()
- {
- int n = 0;
- while (cin>>n)
- {
- //vector<vector<int>> array(n);//定义二维数组
- //for (int i = 0; i <n; i++)
- // array[i].resize(n);//注意:必须设置二维数组的列数,即每一行有多少个元素
- //array[2][1] = 12;//将第三行的第二个元素初始化为12
- int **array=new int*[n];
- for (int i = 0; i < n; i++)
- array[i] = new int[n];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- cin >> array[i][j];//一行一行获取数据
- }
- }
- bool flag = false;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (array[i][j] != array[j][i])//对称的位置上必须相等
- flag = true;
- }
- if (flag)
- break;
- }
- if (flag)
- cout << "No!" << endl;
- else
- cout << "Yes!" << endl;
- }
- }
- /**************************************************************
- Problem: 1180
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:50 ms
- Memory:1916 kb
- ****************************************************************/
题目1182:统计单词
-
题目描述:
-
编一个程序,读入用户输入的,以“.”结尾的一行文字,统计一共有多少个单词,并分别输出每个单词含有多少个字符。
(凡是以一个或多个空格隔开的部分就为一个单词)
-
输入:
-
输入包括1行字符串,以“.”结束,字符串中包含多个单词,单词之间以一个或多个空格隔开。
-
输出:
-
可能有多组测试数据,对于每组数据,
输出字符串中每个单词包含的字母的个数。
-
样例输入:
-
hello how are you.
-
样例输出:
-
5 3 3 3
- #include "stdio.h"
- #include "vector"
- #include "string"
- #include "iostream"
- using namespace std;
- int main()
- {
- string str;
- while (getline(cin,str))
- {
- for (size_t i = 0; i < str.length(); i++)
- {
- int count = 0;
- //while (str[i] <= 'z' && str[i] >= 'a' || str[i] <= 'Z' && str[i] >= 'A')
- while (isalpha(str[i]))
- {//只要是字母就统计
- count++;
- i++;
- }
- if (count)//防止多个空格
- {
- if (i==(str.length()-1))
- cout << count;
- else
- cout << count<<" ";
- }
- }
- cout << endl;
- }
- return 0;
- }
- /**************************************************************
- Problem: 1182
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:20 ms
- Memory:1520 kb
- ****************************************************************/
#include "stdio.h"
#include "vector"
#include "string"
#include "iostream"
using namespace std;
int main()
{
string str;
while (getline(cin,str))
{
for (size_t i = 0; i < str.length(); i++)
{
int count = 0;
//while (str[i] <= 'z' && str[i] >= 'a' || str[i] <= 'Z' && str[i] >= 'A')
while (isalpha(str[i]))
{//只要是字母就统计
count++;
i++;
}
if (count)//防止多个空格
{
if (i==(str.length()-1))
cout << count;
else
cout << count<<" ";
}
}
cout << endl;
}
return 0;
}
/**************************************************************
Problem: 1182
User: EbowTang
Language: C++
Result: Accepted
Time:20 ms
Memory:1520 kb
****************************************************************/
题目1183:守形数
-
题目描述:
-
守形数是这样一种整数,它的平方的低位部分等于它本身。
比如25的平方是625,低位部分是25,因此25是一个守形数。
编一个程序,判断N是否为守形数。
-
输入:
-
输入包括1个整数N,2<=N<100。
-
输出:
-
可能有多组测试数据,对于每组数据,
输出"Yes!”表示N是守形数。
输出"No!”表示N不是守形数。
-
样例输入:
-
25 4
-
样例输出:
-
Yes! No!
- #include "vector"
- #include "string"
- #include <iostream>
- #include "algorithm"
- #include<stdio.h>
- using namespace std;
- int main()
- {
- int n, m, f;
- while (scanf("%d", &n) != EOF)
- {
- if(n<2||n>=100)
- break;
- m = n;
- n = n*n;
- if (n % 1000 == m)
- printf("Yes!\n");
- else if (n % 100 == m)
- printf("Yes!\n");
- else if (n % 10 == m)
- printf("Yes!\n");
- else
- printf("No!\n");
- }
- return 0;
- }
- /**************************************************************
- Problem: 1183
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:0 ms
- Memory:1520 kb
- ****************************************************************/
#include "vector"
#include "string"
#include <iostream>
#include "algorithm"
#include<stdio.h>
using namespace std;
int main()
{
int n, m, f;
while (scanf("%d", &n) != EOF)
{
if(n<2||n>=100)
break;
m = n;
n = n*n;
if (n % 1000 == m)
printf("Yes!\n");
else if (n % 100 == m)
printf("Yes!\n");
else if (n % 10 == m)
printf("Yes!\n");
else
printf("No!\n");
}
return 0;
}
/**************************************************************
Problem: 1183
User: EbowTang
Language: C++
Result: Accepted
Time:0 ms
Memory:1520 kb
****************************************************************/
题目1184:二叉树遍历
-
题目描述:
-
编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储)。
例如如下的先序遍历字符串:
ABC##DE#G##F###
其中“#”表示的是空格,空格字符代表空树。建立起此二叉树以后,再对二叉树进行中序遍历,输出遍历结果。
-
输入:
-
输入包括1行字符串,长度不超过100。
-
输出:
-
可能有多组测试数据,对于每组数据,
输出将输入字符串建立二叉树后中序遍历的序列,每个字符后面都有一个空格。
每个输出结果占一行。
-
样例输入:
-
abc##de#g##f###
-
样例输出:
-
c b e g d f a
- #include "string"
- #include "algorithm"
- #include <iostream>
- #include "stack"
- #include <cmath>
- #include <set>
- using namespace std;
- string preStr;
- // 节点定义
- class BSTNode
- {
- public:
- BSTNode()// 默认构造
- {
- pRight = NULL;
- pLeft = NULL;
- value = 0;
- }
- ~BSTNode();
- friend class LinkBST;// 允许链表类随意访问节点数据
- private:
- char value;
- BSTNode *pRight;
- BSTNode *pLeft;
- };
- // 无头结点的二叉树定义
- class LinkBST
- {
- public:
- LinkBST(){};
- ~LinkBST(){};
- // 根据字符串重建二叉树
- void ReBuildTree(BSTNode *&pNode, int &index);
- // 中序遍历
- void InOrder(BSTNode *&pRoot);
- };
- // 中序遍历
- void LinkBST::InOrder(BSTNode *&pNode)
- {
- if (pNode != NULL)
- {
- InOrder(pNode->pLeft);
- cout << pNode->value << " ";
- InOrder(pNode->pRight);
- }
- }
- // 根据前序字符串重建二叉树
- void LinkBST::ReBuildTree(BSTNode *&pNode, int &index)
- {
- if (index == preStr.size())
- return;
- if (preStr[index] == '#')
- {
- pNode = NULL;
- index++;
- }
- else
- {
- pNode = new BSTNode;
- pNode->value = preStr[index];
- index++;
- ReBuildTree(pNode->pLeft, index);//总是先建完左子树,再建右子树
- ReBuildTree(pNode->pRight, index);
- }
- }
- /*
- a
- / \
- b #
- / \
- c d <------本例题的二叉树逻辑结构
- / \ / \
- # # e f
- / \ / \
- # g # #
- / \
- # #
- */
- int main()
- {
- while (cin >> preStr)
- {
- BSTNode *pRoot = NULL;
- LinkBST bst;
- int index = 0;
- bst.ReBuildTree(pRoot,index);
- bst.InOrder(pRoot);
- cout << endl;
- }
- }
#include "string"
#include "algorithm"
#include <iostream>
#include "stack"
#include <cmath>
#include <set>
using namespace std;
string preStr;
// 节点定义
class BSTNode
{
public:
BSTNode()// 默认构造
{
pRight = NULL;
pLeft = NULL;
value = 0;
}
~BSTNode();
friend class LinkBST;// 允许链表类随意访问节点数据
private:
char value;
BSTNode *pRight;
BSTNode *pLeft;
};
// 无头结点的二叉树定义
class LinkBST
{
public:
LinkBST(){};
~LinkBST(){};
// 根据字符串重建二叉树
void ReBuildTree(BSTNode *&pNode, int &index);
// 中序遍历
void InOrder(BSTNode *&pRoot);
};
// 中序遍历
void LinkBST::InOrder(BSTNode *&pNode)
{
if (pNode != NULL)
{
InOrder(pNode->pLeft);
cout << pNode->value << " ";
InOrder(pNode->pRight);
}
}
// 根据前序字符串重建二叉树
void LinkBST::ReBuildTree(BSTNode *&pNode, int &index)
{
if (index == preStr.size())
return;
if (preStr[index] == '#')
{
pNode = NULL;
index++;
}
else
{
pNode = new BSTNode;
pNode->value = preStr[index];
index++;
ReBuildTree(pNode->pLeft, index);//总是先建完左子树,再建右子树
ReBuildTree(pNode->pRight, index);
}
}
/*
a
/ \
b #
/ \
c d <------本例题的二叉树逻辑结构
/ \ / \
# # e f
/ \ / \
# g # #
/ \
# #
*/
int main()
{
while (cin >> preStr)
{
BSTNode *pRoot = NULL;
LinkBST bst;
int index = 0;
bst.ReBuildTree(pRoot,index);
bst.InOrder(pRoot);
cout << endl;
}
}
题目1185:特殊排序
-
题目描述:
-
输入一系列整数,将其中最大的数挑出,并将剩下的数进行排序。
-
输入:
-
输入第一行包括1个整数N,1<=N<=1000,代表输入数据的个数。接下来的一行有N个整数。
-
输出:
-
可能有多组测试数据,对于每组数据,第一行输出一个整数,代表N个整数中的最大值,并将此值从数组中去除,将剩下的数进行排序。第二行将排序的结果输出。
-
样例输入:
-
4 1 3 4 2
-
样例输出:
-
4 1 2 3
-
提示:
-
如果数组中只有一个数,当第一行将其输出后,第二行请输出"-1"。
- #include "vector"
- #include "string"
- #include "algorithm"
- #include <iostream>
- #include "stack"
- using namespace std;
- int main()
- {
- int n = 0;
- while (cin >> n)
- {
- vector<int> vec(n);
- if (n >= 2)
- {
- for (int i = 0; i < n; i++)
- cin >> vec[i];
- sort(vec.begin(), vec.end());
- cout << vec[n - 1] << endl;
- for (int i = 0; i < n - 2; i++)
- cout << vec[i] << " ";
- cout << vec[n - 2] << endl;
- }
- else
- {
- int val = 0;
- cin >> val;
- cout << val << endl << -1 << endl;
- }
- }
- }
- /**************************************************************
- Problem: 1185
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:50 ms
- Memory:1520 kb
- ****************************************************************/
#include "vector"
#include "string"
#include "algorithm"
#include <iostream>
#include "stack"
using namespace std;
int main()
{
int n = 0;
while (cin >> n)
{
vector<int> vec(n);
if (n >= 2)
{
for (int i = 0; i < n; i++)
cin >> vec[i];
sort(vec.begin(), vec.end());
cout << vec[n - 1] << endl;
for (int i = 0; i < n - 2; i++)
cout << vec[i] << " ";
cout << vec[n - 2] << endl;
}
else
{
int val = 0;
cin >> val;
cout << val << endl << -1 << endl;
}
}
}
/**************************************************************
Problem: 1185
User: EbowTang
Language: C++
Result: Accepted
Time:50 ms
Memory:1520 kb
****************************************************************/
题目1187:最小年龄的3个职工
-
题目描述:
-
职工有职工号,姓名,年龄.输入n个职工的信息,找出3个年龄最小的职工打印出来。
-
输入:
-
输入第一行包括1个整数N,1<=N<=30,代表输入数据的个数。
接下来的N行有N个职工的信息:
包括职工号(整数), 姓名(字符串,长度不超过10), 年龄(1<=age<=100)。
-
输出:
-
可能有多组测试数据,对于每组数据,
输出结果行数为N和3的较小值,分别为年龄最小的职工的信息。关键字顺序:年龄>工号>姓名,从小到大。
-
样例输入:
-
5 501 Jack 6 102 Nathon 100 599 Lily 79 923 Lucy 15 814 Mickle 65
-
样例输出:
-
501 Jack 6 923 Lucy 15 814 Mickle 65
- #include "string"
- #include "algorithm"
- #include <iostream>
- using namespace std;
- typedef struct _Worker
- {
- int num;
- string name;
- int age;
- }Worker;
- bool cmpWorker(Worker x, Worker y)
- {
- if (x.age != y.age)
- {
- return x.age < y.age;
- }
- else
- {
- if (x.num != y.num)
- return x.num<y.num;
- else
- return x.age<y.age;
- }
- }
- int main(void)
- {
- int n = 0;
- while (cin>>n)
- {
- Worker w[600];
- //Worker *w=new Worker[n];
- for (size_t i = 0; i < n; i++)
- cin >> w[i].num >> w[i].name >> w[i].age;
- //多关键字排序
- sort(w, w + n, cmpWorker);//功能是先排年龄,若相等则在其基础上对工号和姓名进行排序。
- //输出信息
- int min = (n>3) ? 3 : n;
- for (int i = 0; i < min; i++)
- cout << w[i].num << " " << w[i].name << " " << w[i].age << endl;
- }
- return 0;
- }
- /**************************************************************
- Problem: 1187
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:20 ms
- Memory:1528 kb
- ****************************************************************/
#include "string"
#include "algorithm"
#include <iostream>
using namespace std;
typedef struct _Worker
{
int num;
string name;
int age;
}Worker;
bool cmpWorker(Worker x, Worker y)
{
if (x.age != y.age)
{
return x.age < y.age;
}
else
{
if (x.num != y.num)
return x.num<y.num;
else
return x.age<y.age;
}
}
int main(void)
{
int n = 0;
while (cin>>n)
{
Worker w[600];
//Worker *w=new Worker[n];
for (size_t i = 0; i < n; i++)
cin >> w[i].num >> w[i].name >> w[i].age;
//多关键字排序
sort(w, w + n, cmpWorker);//功能是先排年龄,若相等则在其基础上对工号和姓名进行排序。
//输出信息
int min = (n>3) ? 3 : n;
for (int i = 0; i < min; i++)
cout << w[i].num << " " << w[i].name << " " << w[i].age << endl;
}
return 0;
}
/**************************************************************
Problem: 1187
User: EbowTang
Language: C++
Result: Accepted
Time:20 ms
Memory:1528 kb
****************************************************************/
题目1189:还是约瑟夫环
-
题目描述:
-
生成一个长度为21的数组,依次存入1到21;
建立一个长度为21的单向链表,将上述数组中的数字依次存入链表每个结点中;
将上述链表变为单向封闭(循环)链表;从头结点开始数,将第17个结点删除,将它的下一个结点作为新的头结点;
重复上述过程,直到该链表中只剩一个结点,显示该结点中存入的数字。
-
输入:
-
没有任何输入数据。
-
输出:
-
输出上面题目描述中最后剩下的节点中存入的数字。
-
样例输入:
-
样例输出:
-
提示:
-
请不要直接输出数据水过去,这样达不到提升自己的目的,
请按照题目要求来做题,这样到真正考试时才能应对自如。
- #include "iostream"
- using namespace std;
- //节点定义
- class LinkNode
- {
- public:
- LinkNode(int item)//有参数的构造
- {//函数参数表中的形参允许有默认值,但是带默认值的参数需要放后面
- next = NULL;
- data = item;
- }
- friend class LinkList;//允许链表类随意访问节点数据
- private:
- int data;
- LinkNode *next;
- };
- // 带头结点的单链表定义
- class LinkList
- {
- public:
- //有参数的构造函数
- LinkList(int size)
- {
- head = new LinkNode(0);//头结点,并未该节点赋值0
- nLength = 0;
- }
- ~LinkList(){}
- //定位指定的位置,返回该位置上的结点指针
- LinkNode* Locate(int pos);
- //在指定位置pos插入值为item的结点,失败返回false
- bool Insert(int item, int pos);
- //创建一个链表环
- void CreatCircle();
- //数数出局
- bool CountMove(int nStep = 0, int nPersons = 0);
- private:
- LinkNode *head;//头结点指针
- int nLength;//统计节点长度,不算头结点
- };
- //返回链表中第pos个元素的地址,第0个元素是头结点
- LinkNode* LinkList::Locate(int pos)
- {
- LinkNode *p = head;
- int i = 0;
- while (p != NULL && i < pos)//p==NULL说明是末尾了
- {
- p = p->next;
- i++;
- }
- return p;
- }
- //在pos位置的节点后面插入新节点并赋值item
- bool LinkList::Insert(int item, int pos)
- {
- LinkNode *p = Locate(pos);
- LinkNode *newNode = new LinkNode(item);//创建新节点,该节点值为item
- //建立连接
- newNode->next = p->next;
- p->next = newNode;
- nLength++;
- return true;
- }
- //创建一个链表环 ,将最后一个节点连接第1个节点从而形成环,注意head是第0个节点
- void LinkList::CreatCircle()
- {
- int nEnd = this->nLength;
- int nStart = 1;
- LinkNode *pTail = Locate(nEnd);
- LinkNode *pcirStart = Locate(nStart);
- pTail->next = pcirStart;
- }
- //从npersons个人中,每数到nstep就出局
- bool LinkList::CountMove(int nStep, int nPersons)//指定出局人数
- {
- LinkNode *pCurr = NULL, *pPrev = NULL;
- int i = 0,n = 0;//n统计已经出局的人数
- pCurr = pPrev = head;
- while (n < nPersons)
- {
- pPrev = pCurr;//这三行代码模拟数数
- pCurr = pCurr->next;
- i++;
- if (i == nStep)// 此时踢出环 ,删除当前节点并且输出该节点的值
- {
- pPrev->next = pCurr->next;//重新建立连接
- delete pCurr;
- pCurr = pPrev->next;//指向新位置的下一个节点
- i = 1;
- n++;
- }
- if (pPrev == pCurr)//此时最后一个节点
- {
- cout << pCurr->data << endl;
- delete pCurr;
- pCurr = NULL;//最后一个节点删除后的“擦屁股”处理
- head->next = head;
- n++;
- break;
- }
- }
- return true;
- }
- int main()
- {
- int nPersons = 21,count = 17;
- LinkList ysf(nPersons);
- for (int i = 0; i < nPersons; i++)
- ysf.Insert(i + 1, i);//在第i个位置插入值为i+1的节点
- ysf.CreatCircle();
- ysf.CountMove(count, nPersons);//数到count出局
- return 0;
- }
- /**************************************************************
- Problem: 1189
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:0 ms
- Memory:1516 kb
- ****************************************************************/
#include "iostream"
using namespace std;
//节点定义
class LinkNode
{
public:
LinkNode(int item)//有参数的构造
{//函数参数表中的形参允许有默认值,但是带默认值的参数需要放后面
next = NULL;
data = item;
}
friend class LinkList;//允许链表类随意访问节点数据
private:
int data;
LinkNode *next;
};
// 带头结点的单链表定义
class LinkList
{
public:
//有参数的构造函数
LinkList(int size)
{
head = new LinkNode(0);//头结点,并未该节点赋值0
nLength = 0;
}
~LinkList(){}
//定位指定的位置,返回该位置上的结点指针
LinkNode* Locate(int pos);
//在指定位置pos插入值为item的结点,失败返回false
bool Insert(int item, int pos);
//创建一个链表环
void CreatCircle();
//数数出局
bool CountMove(int nStep = 0, int nPersons = 0);
private:
LinkNode *head;//头结点指针
int nLength;//统计节点长度,不算头结点
};
//返回链表中第pos个元素的地址,第0个元素是头结点
LinkNode* LinkList::Locate(int pos)
{
LinkNode *p = head;
int i = 0;
while (p != NULL && i < pos)//p==NULL说明是末尾了
{
p = p->next;
i++;
}
return p;
}
//在pos位置的节点后面插入新节点并赋值item
bool LinkList::Insert(int item, int pos)
{
LinkNode *p = Locate(pos);
LinkNode *newNode = new LinkNode(item);//创建新节点,该节点值为item
//建立连接
newNode->next = p->next;
p->next = newNode;
nLength++;
return true;
}
//创建一个链表环 ,将最后一个节点连接第1个节点从而形成环,注意head是第0个节点
void LinkList::CreatCircle()
{
int nEnd = this->nLength;
int nStart = 1;
LinkNode *pTail = Locate(nEnd);
LinkNode *pcirStart = Locate(nStart);
pTail->next = pcirStart;
}
//从npersons个人中,每数到nstep就出局
bool LinkList::CountMove(int nStep, int nPersons)//指定出局人数
{
LinkNode *pCurr = NULL, *pPrev = NULL;
int i = 0,n = 0;//n统计已经出局的人数
pCurr = pPrev = head;
while (n < nPersons)
{
pPrev = pCurr;//这三行代码模拟数数
pCurr = pCurr->next;
i++;
if (i == nStep)// 此时踢出环 ,删除当前节点并且输出该节点的值
{
pPrev->next = pCurr->next;//重新建立连接
delete pCurr;
pCurr = pPrev->next;//指向新位置的下一个节点
i = 1;
n++;
}
if (pPrev == pCurr)//此时最后一个节点
{
cout << pCurr->data << endl;
delete pCurr;
pCurr = NULL;//最后一个节点删除后的“擦屁股”处理
head->next = head;
n++;
break;
}
}
return true;
}
int main()
{
int nPersons = 21,count = 17;
LinkList ysf(nPersons);
for (int i = 0; i < nPersons; i++)
ysf.Insert(i + 1, i);//在第i个位置插入值为i+1的节点
ysf.CreatCircle();
ysf.CountMove(count, nPersons);//数到count出局
return 0;
}
/**************************************************************
Problem: 1189
User: EbowTang
Language: C++
Result: Accepted
Time:0 ms
Memory:1516 kb
****************************************************************/
题目1192:回文字符串
-
题目描述:
-
给出一个长度不超过1000的字符串,判断它是不是回文(顺读,逆读均相同)的。
-
输入:
-
输入包括一行字符串,其长度不超过1000。
-
输出:
-
可能有多组测试数据,对于每组数据,如果是回文字符串则输出"Yes!”,否则输出"No!"。
-
样例输入:
-
hellolleh helloworld
-
样例输出:
-
Yes! No!
- #include "string"
- #include <iostream>
- #include "algorithm"
- using namespace std;
- int main()
- {
- string str;
- while (getline(cin, str))
- {
- bool flag = false;
- for (int i = 0; i < str.length(); i++)
- {//回文的重要判断依据就是前后对应位置字符必须一样
- if (str[i] != str[str.length() - 1 - i])
- {
- flag = true;
- break;
- }
- }
- if (flag)
- cout << "No!" << endl;
- else
- cout << "Yes!" << endl;
- }
- return 0;
- }
- /**************************************************************
- Problem: 1192
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:40 ms
- Memory:1520 kb
- ****************************************************************/
#include "string"
#include <iostream>
#include "algorithm"
using namespace std;
int main()
{
string str;
while (getline(cin, str))
{
bool flag = false;
for (int i = 0; i < str.length(); i++)
{//回文的重要判断依据就是前后对应位置字符必须一样
if (str[i] != str[str.length() - 1 - i])
{
flag = true;
break;
}
}
if (flag)
cout << "No!" << endl;
else
cout << "Yes!" << endl;
}
return 0;
}
/**************************************************************
Problem: 1192
User: EbowTang
Language: C++
Result: Accepted
Time:40 ms
Memory:1520 kb
****************************************************************/
题目1193:矩阵转置
-
题目描述:
-
输入一个N*N的矩阵,将其转置后输出。要求:不得使用任何数组(就地逆置)。
-
输入:
-
输入的第一行包括一个整数N,(1<=N<=100),代表矩阵的维数。
接下来的N行每行有N个整数,分别代表矩阵的元素。
-
输出:
-
可能有多组测试数据,对于每组数据,将输入的矩阵转置后输出。
-
样例输入:
-
3 1 2 3 4 5 6 7 8 9
-
样例输出:
-
1 4 7 2 5 8 3 6 9
- #include <cstdio>
- #include <cstdlib>
- #include "vector"
- #include "string"
- #include "algorithm"
- #include <iostream>
- #include "stack"
- #include "math.h"
- #define MAXN 100
- using namespace std;
- int main()
- {
- int n, i, j;
- int array[MAXN][MAXN];
- while (scanf("%d", &n) != EOF)
- {
- for (i = 0; i < n; i++)
- {//获取数据
- for (j = 0; j < n; j++)
- scanf("%d", &array[i][j]);
- }
- for (i = 0; i < n; i++)
- {//交换array[i][j]和array[j][i]
- for (j = i; j < n; j++)
- {
- int temp;
- temp = array[i][j];
- array[i][j] = array[j][i];
- array[j][i] = temp;
- }
- }
- for (i = 0; i < n; i++)
- {
- printf("%d", array[i][0]);
- for (j = 1; j < n; j++)
- printf(" %d", array[i][j]);
- printf("\n");
- }
- }
- return 0;
- }
- /**************************************************************
- Problem: 1193
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:10 ms
- Memory:1520 kb
- ****************************************************************/
#include <cstdio>
#include <cstdlib>
#include "vector"
#include "string"
#include "algorithm"
#include <iostream>
#include "stack"
#include "math.h"
#define MAXN 100
using namespace std;
int main()
{
int n, i, j;
int array[MAXN][MAXN];
while (scanf("%d", &n) != EOF)
{
for (i = 0; i < n; i++)
{//获取数据
for (j = 0; j < n; j++)
scanf("%d", &array[i][j]);
}
for (i = 0; i < n; i++)
{//交换array[i][j]和array[j][i]
for (j = i; j < n; j++)
{
int temp;
temp = array[i][j];
array[i][j] = array[j][i];
array[j][i] = temp;
}
}
for (i = 0; i < n; i++)
{
printf("%d", array[i][0]);
for (j = 1; j < n; j++)
printf(" %d", array[i][j]);
printf("\n");
}
}
return 0;
}
/**************************************************************
Problem: 1193
User: EbowTang
Language: C++
Result: Accepted
Time:10 ms
Memory:1520 kb
****************************************************************/
题目1194:八进制
-
题目描述:
-
输入一个整数,将其转换成八进制数输出。
-
输入:
-
输入包括一个整数N(0<=N<=100000)。
-
输出:
-
可能有多组测试数据,对于每组数据,
输出N的八进制表示数。
-
样例输入:
-
7 8 9
-
样例输出:
-
7 10 11
- #include "vector"
- #include <iostream>
- using namespace std;
- int main()
- {
- int n = 0;
- while (cin >> n )
- {
- if (n == 0)
- {
- cout << 0 << endl;
- continue;
- }
- vector<int> vec(100,0);
- int nsize = 0;
- while (n > 0)
- {//获取8进制数,放入数组
- vec[nsize++] = n % 8;
- n = n / 8;
- }
- for (int i = nsize-1; i >= 0; i--)
- cout << vec[i];
- cout << endl;
- }
- return 0;
- }
- /**************************************************************
- Problem: 1194
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:200 ms
- Memory:1520 kb
- ****************************************************************/
#include "vector"
#include <iostream>
using namespace std;
int main()
{
int n = 0;
while (cin >> n )
{
if (n == 0)
{
cout << 0 << endl;
continue;
}
vector<int> vec(100,0);
int nsize = 0;
while (n > 0)
{//获取8进制数,放入数组
vec[nsize++] = n % 8;
n = n / 8;
}
for (int i = nsize-1; i >= 0; i--)
cout << vec[i];
cout << endl;
}
return 0;
}
/**************************************************************
Problem: 1194
User: EbowTang
Language: C++
Result: Accepted
Time:200 ms
Memory:1520 kb
****************************************************************/
题目1196:成绩排序
-
题目描述:
-
用一维数组存储学号和成绩,然后,按成绩排序输出。
-
输入:
-
输入第一行包括一个整数N(1<=N<=100),代表学生的个数。
接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。
-
输出:
-
按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。
如果学生的成绩相同,则按照学号的大小进行从小到大排序。
-
样例输入:
-
3 1 90 2 87 3 92
-
样例输出:
-
2 87 1 90 3 92
- #include "vector"
- #include<algorithm>
- #include <iostream>
- using namespace std;
- typedef struct _StuInfo
- {
- int num;//学号
- int score;//分数
- }StuInfo;
- bool cmpStu(StuInfo x, StuInfo y)
- {
- if (x.score != y.score)
- return x.score < y.score;
- else
- {
- if (x.num != y.num)//如果分数相同则按照学号排名
- return x.num<y.num;
- }
- }
- int main()
- {
- StuInfo a[100];
- int n;
- while (cin >> n)//人数
- {
- //获取数据
- for (int i = 0; i<n; i++)
- cin >> a[i].num >> a[i].score;
- //多关键字排序
- sort(a, a + n, cmpStu);//功能是先排分数,若相等则在其基础上对名字和年龄进行排序。
- //输出信息
- for (int i = 0; i<n; i++)
- cout << a[i].num << " " << a[i].score << endl;
- }
- return 0;
- }
- /**************************************************************
- Problem: 1196
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:470 ms
- Memory:1520 kb
- ****************************************************************/
#include "vector"
#include<algorithm>
#include <iostream>
using namespace std;
typedef struct _StuInfo
{
int num;//学号
int score;//分数
}StuInfo;
bool cmpStu(StuInfo x, StuInfo y)
{
if (x.score != y.score)
return x.score < y.score;
else
{
if (x.num != y.num)//如果分数相同则按照学号排名
return x.num<y.num;
}
}
int main()
{
StuInfo a[100];
int n;
while (cin >> n)//人数
{
//获取数据
for (int i = 0; i<n; i++)
cin >> a[i].num >> a[i].score;
//多关键字排序
sort(a, a + n, cmpStu);//功能是先排分数,若相等则在其基础上对名字和年龄进行排序。
//输出信息
for (int i = 0; i<n; i++)
cout << a[i].num << " " << a[i].score << endl;
}
return 0;
}
/**************************************************************
Problem: 1196
User: EbowTang
Language: C++
Result: Accepted
Time:470 ms
Memory:1520 kb
****************************************************************/
题目1198:a+b
-
题目描述:
-
实现一个加法器,使其能够输出a+b的值。
-
输入:
-
输入包括两个数a和b,其中a和b的位数不超过1000位。
-
输出:
-
可能有多组测试数据,对于每组数据,
输出a+b的值。
-
样例输入:
-
2 6 10000000000000000000 10000000000000000000000000000000
-
样例输出:
-
8 10000000000010000000000000000000
- #include "vector"
- #include "string"
- #include <iostream>
- #include "algorithm"
- #define N 1010
- int len = 1001;
- using namespace std;
- void AddBigNum(const string &a, const string &b, vector<int> &ansStr);
- int main()
- {
- string bigNum1, bigNum2; // 初始状态用string来存储大数
- while (cin >> bigNum1 >> bigNum2)
- {
- int c = max(bigNum1.size(),bigNum2.size());
- vector<int> ans(len, 0);//接收答案
- AddBigNum(bigNum1, bigNum2, ans);//执行大整数加法
- if (ans[0] != 0) //看看最高位是否有进位
- for (int i = 0; i <= c; i++)
- cout << ans[i];
- else
- for (int i = 1; i <= c; i++)
- cout << ans[i];
- cout << endl;
- ans.clear();
- }
- return 0;
- }
- void AddBigNum(const string &a, const string &b, vector<int> &ans)
- {
- int length1=a.size(), length2=b.size();
- vector<int> anum(len,0),bnum(len,0);
- int k = max(a.size(), b.size());
- int c = k;
- //按照字符串中的数据顺序转换成整数
- for (int i = 0; i<length1; k--, i++) //低位放在数组的前面,比如523,a[3]=3,a[2]=2,a[1]=5
- anum[k] = a[length1 - 1 - i] - '0';
- for (int i = 0, k = c; i<length2; k--, i++) //低位放在数组的前面,比如19,b[3]=9,b[2]=1,b[1]=0
- bnum[k] = b[length2 - 1 - i] - '0';
- //作位相加
- for (int i = c; i>0; i--)
- ans[i] = anum[i] + bnum[i];//ans[3]=12,ans[2]=3,ans[1]=5
- //进位,低位向高位进位
- for (int i = c; i>0; i--)
- {
- if (ans[i] >= 10)
- {//ans[i-1]是高位,ans[i]是低位
- ans[i - 1] += ans[i] / 10;
- ans[i] = ans[i] % 10;
- }
- }
- }
- /**************************************************************
- Problem: 1198
- User: EbowTang
- Language: C++
- Result: Accepted
- Time:150 ms
- Memory:1520 kb
- ****************************************************************/