1136 1137 1138 1139 模拟练习

1136 A Delayed Palindrome (20 分)

Sample Input 1:

97152

Sample Output 1:

97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.

Sample Input 2:

196

Sample Output 2:

196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.

Note

  1. 题意:判断对称
  2. 思路: 大数加减 + check判断 好像做过

Code

#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
#include<map>
using namespace std;
const int maxn = 1e4;
bool check(string s){
	int temp = s.size();
	temp--;
	int i = 0;
	while(s[i] == s[temp]){
		if(i >= temp) return 1;
		temp--;
		i++;
	}
	return 0;
}
string addString(string a, string b){
	reverse(a.begin() , a.end());
	reverse(b.begin(), b.end());
	string sum = a;
	int carry = 0, i; 
	for(i = 0; i < a.size(); i++){
		sum[i] = a[i] + b[i] + carry - '0';
		carry = 0;
		if(sum[i] > '9'){
			sum[i] -= 10;
			carry = 1;
		}
 
	}
	if(carry) sum += "1";
	reverse(sum.begin(), sum.end());
	return sum;
}

int main(){
#ifdef _DEBUG 
	ifstream cin("data.txt");
#endif
	string input;
	int cnt = 0;
	cin >> input;
	while(check(input) == 0 && cnt < 10){
		string s1, s2;
		s1 = s2 = input;
		reverse(s2.begin(), s2.end());
		input = addString(s1, s2);
		cout << s1 << " + " << s2 << " = " << input << endl;
		cnt++;
	}
	if(cnt < 10) cout << input <<" is a palindromic number.";
	else cout <<  "Not found in 10 iterations.";

#ifdef _DEBUG 
	cin.close();
#endif
	return 0;
}

1137 Final Grading (25 分)

Sample Input:

6 6 7
01234 880
a1903 199
ydjh2 200
wehu8 300
dx86w 220
missing 400
ydhfu77 99
wehu8 55
ydjh2 98
dx86w 88
a1903 86
01234 39
ydhfu77 88
a1903 66
01234 58
wehu8 84
ydjh2 82
missing 99
dx86w 81

Sample Output:

missing 400 -1 99 99
ydjh2 200 98 82 88
dx86w 220 88 81 84
wehu8 300 55 84 84

Note

  1. 题意: 输入n1个人的pat成绩, n2个人的mid成绩, n3个人的final成绩,只有pat过200且总成绩过60的可以参与排名,若期末成绩高于其中,则只看期末成绩排名,否则其中期末加权(注意round),输出成绩单按总成绩降序,id升序。
  2. 思路: have[]记录有pat成绩的。myHash记录有其中成绩的

问题

  1. myhash访问不存在,为啥会纳入
  2. 遍历map为啥auto it没用
  3. map【*】 *为没有的变量到底会怎样

Code

#include<iostream>
#include<algorithm>
#include<vector>
#include<fstream>
#include<queue>
#include<string>
#include<cstring>
#include<map>
using namespace std;
const int maxn = 1e7;
struct node{
	string name;
	int pat;
	int mid;
	int Finals;
	int last;
};
map <string, int>  have;
map<string, int> myhash;
bool cmp(node a, node b){
	if( a.last == b.last) return a.name < b.name;
	else return a.last > b.last;
}

int main(){ 
#ifdef _DEBUG
	ifstream cin("data2.txt");
#endif
    int num1, num2, num3, i;
	cin >> num1 >> num2 >> num3;
	vector<node> result;
	for( i = 0; i < num1; i++){
		string s;
		int temp;
		cin >> s >> temp;
		if(temp >= 200 && temp <= 900){
			have[s] = temp;
		}
	}
	for( i = 0; i < num2;i ++){
		string s;
		double temp;
		cin >> s >> temp;
		if(have[s] != 0  && temp <= 100) {
			myhash[s] = temp;
		}
		else continue;
	}

	for( i = 0; i < num3;i ++){
		string s;
		double temp;
		node save;
		cin >> s >> temp;
		if(have[s] != 0 && temp <= 100){			
			if(myhash[s] != 0){
				save.Finals = temp;
				if(myhash[s] > temp){
					double t =  (0.4 * myhash[s] + 0.6* temp);
					if(t - (int)t >= 0.5) t++;
					save.last =t;
				}
				else 
					save.last = temp;
				save.mid = myhash[s];
			}
			else{
				save.Finals = temp;
				save.last = temp;
				save.mid = -1;
			}
		}
        else continue;
		if(save.last <= 60) continue;
		save.pat = have[s];
		save.name = s;
		result.push_back(save);
	}
	sort(result.begin(), result.end(), cmp);
	//for(auto it = a.begin(); it != a.end(); it++)}{
	//		node temp = a[it -> first];
	//		cout << it -> first << " " <<  temp.mid << " " << temp.Finals << temp.last;
	//}
	for( i = 0; i < result.size(); i++){
		cout << result[i].name  << " "<< result[i].pat <<  " " <<  result[i].mid << " " << result[i].Finals << " "<< result[i].last << endl;
	}



	
#ifdef _DEBUG
	 cin.close();
#endif
	return 0;

 
}

1138 Postorder Traversal (25 分)

Sample Input:

7
1 2 3 4 5 6 7
2 3 1 5 4 7 6

Sample Output:

3

Note

  1. 题意: 给定前序中序,求后序的第一个元素
  2. 思路: 递归的时有一层完全结束flag置为1,结束

code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
#include<queue>
using namespace std;
const int maxn = 1e5;
struct node{			
	int id;
	int data;
};
vector <node> result;
vector <int> inter, pre, visited;
int flag = 0;
void postorder(int root, int start, int finish, int index){
	if(start > finish || flag == 1) return ;
	int i = start;
	while(i < finish && inter[i] != pre[root]) i++;
	node temp = {index, pre[root]};
	result.push_back(temp);
	postorder(root+1, start, i - 1, 2 * index + 1);
	postorder(root + i - start + 1, i + 1, finish, 2 * index + 2);
	if(start > i - 1 && i+1 > finish){
		flag =1;
		cout << pre[root];
		return;
	}

}



int main(){
#ifdef _DEBUG 
	ifstream cin("data3.txt");
#endif
	int num, edge;
	cin >> num ;
	visited.resize(num);
	for(int i = 0; i < num; i++){
		int temp;
		cin >> temp;
		pre.push_back(temp);
	}
	for(int i = 0; i < num; i++){
		int temp;
		cin >> temp;
		inter.push_back(temp);
	}
	postorder(0, 0, num -1, 0);
#ifdef _DEBUG 
	cin.close();
#endif
	return 0;
}

1139 First Contact (30 分)

##Sample Input:
10 18
-2001 1001
-2002 -2001
1004 1001
-2004 -2001
-2003 1005
1005 -2001
1001 -2003
1002 1001
1002 -2004
-2004 1001
1003 -2002
-2003 1003
1004 -2002
-2001 -2003
1001 1003
1003 -2001
1002 -2001
-2002 -2003
5
1001 -2001
-2003 1001
1005 -2001
-2002 -2004
1111 -2003

Sample Output:

4
1002 2004
1003 2002
1003 2003
1004 2002
4
2001 1002
2001 1003
2002 1003
2002 1004
0
1
2003 2001
0

Note

  1. 题意: 找朋友在这里插入图片描述2. 思路: 偶然得知此题为简单题,但是自己做的时候并不感觉到简单,还是思路训练的太少了啊

Code

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<stack>
#include<map>
#include<fstream>
#include<unordered_map>
using namespace std;
const int maxn = 1e4;
struct node{
	int x;
	int y;
};
bool cmp(node a, node b){
	if(a.x == b.x) return a.y < b.y;
	else return a.x < b.x;
}
int friends[maxn][maxn];
vector <node> results;
vector<int> a[maxn];
int main()
{
#ifdef _DEBUG
	ifstream cin("data4.txt");
#endif
	int num, edge, i, j, queries;
	cin >> num >>edge;
	for(i = 0; i < edge; i++){
		string tempa, tempb;
		cin >> tempa >> tempb;
        int aa = stoi(tempa);
        int bb = stoi(tempb);
		if(tempa[0] == '-' && tempb[0] == '-' || tempa[0] != '-' && tempb[0] != '-' ){
			aa = abs(aa); bb = abs(bb);
			a[aa].push_back(bb);
			a[bb].push_back(aa);
			friends[aa] [bb] = 1;
			friends[bb] [aa] = 1;
		}else {
			friends[abs(aa)] [abs(bb)] = 1;
			friends[abs(bb)] [abs(aa)] = 1;
		}
	}
	cin >> queries;
	for(i = 0; i < queries; i++){
		int aa, bb, cnt = 0;
		cin >> aa >> bb;
		results.clear();
		int temp1 = abs(aa), temp2 = abs(bb);
		for(j = 0; j < a[temp1].size(); j++){
			int str1 = a[temp1][j];
			for(int k = 0; k < a[temp2].size(); k++){
				int str2 = a[temp2][k];
                if(str1 == temp2 ||  str2 == temp1)continue;
				if(friends[str1][str2] == 1 ){
					node temp;
					temp.x = str1; 
					temp.y = str2;
					cnt ++;
					results.push_back(temp);
				}
			}
		}
		sort(results.begin(), results.end(), cmp);
		cout << cnt << endl;
		for(j = 0; j < cnt; j++){
            printf("%04d %04d\n", results[j].x, results[j].y);
		}
	}
#ifdef _DEBUG
	cin.close();
#endif
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值