A查成绩
题目描述
期末考试结束后,数学老师给出了班里同学们的数学成绩,为了快速查成绩,请编程帮助查成绩。
输入
第一行为N(N<1000)表示班级人数,第一行后N行,每行两个部分,一个是学号(符号最多8个),一个是成绩(整数)。
最后一行是要查找成绩同学的学号。
输出
输出要查找同学的学号。
样例输入
2
001 90
002 95
002
样例输出
95
代码:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int N;
cin >> N;
vector<pair<string, int> > scores(N);
for (int i = 0; i < N; i++)
{
cin >> scores[i].first >> scores[i].second;
}
string target_id;
cin >> target_id;
for (const auto &score : scores)
{
if (score.first == target_id)
{
cout << score.second << endl;
return 0;
}
}
cout << "学号不存在" << endl;
return 0;
}
代码2
#include<iostream>
#include<string>
using namespace std;
struct Student {
string num;
int score;
};
Student test[1001];
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> test[i].num >> test[i].score;
}
string target;
cin >> target;
for (int i = 0; i < N; i++) {
if (target == test[i].num)cout << test[i].score;
}
}
B插入数据
题目描述
已有一个整数序列,现在要在不同的位置插入一些整数,请输出插入数据后的序列。
输入
第一行是N(N<1000)表示原序列中元素的个数,紧接着第二行是N个整数,第三行是要插入的元素个数M(M<1000),第四开始M行,每行是一对数据k(要插入到原序列的位置,从1开始计数)和x。
输出
输出插入元素后的整数序列。
样例输入
5
1 2 3 4 5
2
1 11
3 33
样例输出
11 1 2 33 3 4 5
代码
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int N, M;
cin >> N;
vector<int> nums(N);
for (int i = 0; i < N; i++)
{
cin >> nums[i];
}
cin >> M;
for (int i = 0; i < M; i++)
{
int k, x;
cin >> k >> x;
nums.insert(nums.begin() + k-1+i, x); //插入的位置k是原始序列的位置
}
for (int i = 0; i < nums.size(); i++)
{
cout << nums[i] << " ";
}
cout << endl;
return 0;
}
代码2
#include<iostream>
#include<string>
#include<vector>
#include <stack>
using namespace std;
int main() {
int n;
cin >> n;
stack<int> data[1001];
for (int i = 0; i < n; i++)
{
int t;
cin >> t;
data[i].push(t);
}
int m;
cin >> m;
for (int i = 0; i < m; i++)
{
int k,x;
cin >> k >> x;
data[k-1].push(x);
}
n += m;
for (int i = 0; i < n; i++)
{
while (!data[i].empty())
{
int t = data[i].top();
data[i].pop();
printf("%d ", t);
}
}
}
C二路归并
题目描述
有两个按元素值递增有序的整数顺序表A和B,设计一个算法将顺序表A和B的全部元素合并到一个递增有序顺序表C中,并依次输出C中的元素。
输入
占两行,依次是A和B的序列(元素个数都小于100)。
输出
依次输出C中的元素。
样例输入
1 2 3 4 5
1 2 3 4 6
样例输出
1 1 2 2 3 3 4 4 5 6
代码
#include<iostream>
#include<string>
#include<vector>
#include <stack>
using namespace std;
int main() {
stack<int> a;
stack<int> b;
stack<int> c;
int x, y;
while (cin >> x)
{
a.push(x);
if (cin.get() == '\n')break;
}
while (cin >> y)
{
b.push(y);
if (cin.get() == '\n')break;
}
while (!a.empty() && !b.empty())
{
x = a.top();
y = b.top();
if (x >= y)
{
c.push(x);
a.pop();
}
else
{
c.push(y);
b.pop();
}
}
while (!a.empty()) {
x = a.top();
c.push(x);
a.pop();
}
while (!b.empty()) {
y = b.top();
c.push(y);
b.pop();
}
while (!c.empty()) {
x = c.top();
printf("%d ", x);
c.pop();
}
}
D最大值个数
题目描述
有一个整数单链表L,其中可能存在多个值相同的结点,设计一个算法查找L中最大值结点的个数。
输入
单链表L中的元素,个数不定。
输出
查找L中最大值结点的个数。
样例输入
1 2 3 4 5 5 20 20 1 2 3 4 5
样例输出
2
代码
#include<iostream>
#include<string>
#include<vector>
#include <stack>
using namespace std;
int main()
{
int max = -9999;
int x;
int num = 0;
while (cin >> x)
{
if (x > max)
{
max = x;
num = 1;
}
else if (x == max)
num++;
if (cin.get() == '\n')
break;
}
cout << num;
}
E约瑟夫问题
题目描述
编写一个程序求解约瑟夫(Joseph)问题。有n个小孩围成一圈,给他们从1开始依次编号,从编号为1的小孩开始报数,数到第m(0<m<n)个小孩出列,然后从出列的下一个小孩重新开始报数,数到第m个小孩又出列,…,如此反复直到所有的小孩全部出列为止,求整个出列序列。
输入
占一行为n和m(n<100)。
输出
整个出列序列。
样例输入
6 5
样例输出
5 4 6 2 3 1
代码
#include <iostream>
using namespace std;
struct Child
{
int no;
Child*next;
Child(int d):no(d),next(NULL){}
};
class Joseph
{
int n,m;
Child *first;
public:
Joseph(int nl, int ml) :n(nl),m(ml){}
void CreateList()
{
first = new Child(1);
Child* r = first,*p;
for(int i = 2;i <= n; i++)
{
p = new Child(i);
r -> next = p;
r = p;
}
r -> next = first;
}
void Jsequence()
{
Child* p,* q;
for(int i = 1;i <= n;i++)
{
p = first;
int j = 1;
while(j!=m)
{
j++;
p = p->next;
}
cout << p->no << " ";
q = p->next;
p->no=q->no;
p->next = q->next;
delete q;
first = p;
}
cout << endl;
}
};
int main()
{
int n, m;
cin >> n >> m;
Joseph L(n,m);
L.CreateList();
L.Jsequence();
return 0;
}
F括号配对
题目描述
设计一个算法利用顺序栈检查用户输入的表达式中括号是否配对(假设表达式中可能含有圆括号()、中括号[]和大括号{})。
输入
占一行为含有三种括号的表达式(最长100个符号)。
输出
匹配时输出YES,小括号不匹配输出NO1,中括号不匹配时输出NO2,大括号不匹配时输出NO3。
样例输入
{([a])}
样例输出
YES
代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
char ch[1000];
while(cin.getline(ch,1000)){
vector<char>k;
k.push_back(' ');
bool flag=1;
for(int i=0;i<strlen(ch);i++){
if(ch[i]=='('||ch[i]=='['||ch[i]=='{') k.push_back(ch[i]);
else if(ch[i]==')'){
if(k.back()=='['){
cout<<"NO2";
flag=0;
break;
}
else if(k.back()=='{'){
cout<<"NO3";
flag=0;
break;
}
else if(k.back()!='('){
cout<<"NO1";
flag=0;
break;
}
else k.pop_back();
}
else if(ch[i]==']'){
if(k.back()!='['){
cout<<"NO2";
flag=0;
break;
}
else if(k.back()=='('){
cout<<"NO1";
flag=0;
break;
}
else if(k.back()=='{'){
cout<<"NO3";
flag=0;
break;
}
else k.pop_back();
}
else if(ch[i]=='}'){
if(k.back()=='('){
cout<<"NO1";
flag=0;
break;
}
else if(k.back()=='['){
cout<<"NO2";
flag=0;
break;
}
else if(k.back()!='{'){
cout<<"NO3";
flag=0;
break;
}
else k.pop_back();
}
}
if(flag){
if(k.back()=='('){
cout<<"NO1";
break;
}
else if(k.back()=='['){
cout<<"NO2";
break;
}
else if(k.back()=='{'){
cout<<"NO3";
break;
}
}
if(flag){
cout<<"YES";
break;
}
}
return 0;
}
G后缀表达式
题目描述
给出一个中缀表达式,输出该表达式的后缀表达式。
输入
占一行,一个中缀表达式(运算符只有±*/,最多1000个字符),输出后缀表达式。
输出
输出后缀表达式。
样例输入
(56-20)/(4+2)
样例输出
56 20 - 4 2 + /
代码
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include<string.h>
typedef struct{
int top;
char ch[100];
}Stack;
int flag=1;
#define LEN sizeof(Stack)
Stack *init(){
Stack* stack=(Stack*)malloc(LEN);
stack->top=0;
return stack;
}
void push(Stack* stack,char ch){
stack->top++;
stack->ch[stack->top]=ch;
}
void pop(Stack* stack){
if(stack->top==0){
return;
}
printf("%c",stack->ch[stack->top]);
stack->top--;
}
bool check(char a,char b){
if(a=='('){
return true;
}
if((a=='+'||a=='-')&&(b=='*'||b=='/')){
return true;
}else{
return false;
}
}
void InfixToPostfix(char *infix){
Stack *stack=init();
int i;
for(i=0;i<strlen(infix);){
if(infix[i]>='0'&&infix[i]<='9'){
while(infix[i]>='0'&&infix[i]<='9'){
printf("%c",infix[i]);
i++;
}
printf(" ");
}
if(infix[i]==')'){
while(stack->ch[stack->top]!='('){
pop(stack);
if(stack->top>0||i<strlen(infix)){
printf(" ");
}
}
stack->top--;
i++;
}else if(stack->top==0||infix[i]=='('){
push(stack,infix[i]);
i++;
}else if(check(stack->ch[stack->top],infix[i])){
push(stack,infix[i]);
i++;
}else{
pop(stack);
if(stack->top>0||i<strlen(infix)){
printf(" ");
}
}
}
while(stack->top){
pop(stack);
if(stack->top>0||i<strlen(infix)){
printf(" ");
}
}
}
int main(){
char infix[100];
std::cin>>infix;
InfixToPostfix(infix);
return 0;
}
H字符串反转
题目描述
小C很喜欢倒着写单词,现在给你一行小C写的文本,你能把每个单词都反转并输出它们吗?
输入
输入包含多组测试样例。第一行为一个整数T,代表测试样例的数量,后面跟着T个测试样例。
每个测试样例占一行,包含多个单词。一行最多有1000个字符。
输出
对于每一个测试样例,你应该输出转换后的文本。
样例输入
3
olleh !dlrow
I ekil .bulcmca
I evol .mca
样例输出
hello world!
I like acmclub.
I love acm.
代码
#include <bits/stdc++.h>
using namespace std;
char ch[1001];
void fz(int l,int r){
while(l<=r){
swap(ch[l],ch[r]);
l++;
r--;
}
}
int main()
{
int n,len,l,r;
cin>>n;
cin.ignore();
while(n>0){
cin.getline(ch,1001);
len=strlen(ch);
l=r=0;
for(int i=0;i<len;i++){
if(ch[i]==' '){
r=i-1;
fz(l,r);
l=i+1;
}
else if(i==len-1){
r=i;
fz(l,r);
}
}
cout<<ch<<endl;
n--;
}
return 0;
}