1136
Palindrome 回文
standard notation 标准符号
iteration 迭代
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
string plus0(string a, string b){
int c = 0;
string ans;
for(int i = a.length()-1;i>=0;i--){
char temp = c + a[i] + b[i] - '0';
if(temp > '9'){
temp -= 10;
c = 1;
}else{
c = 0;
}
ans += temp;
}
if(c == 1){
ans += '1';
}
reverse(ans.begin(), ans.end());
return ans;
}
bool judge(string s){
for(int i = 0, j = s.length()-1; i <= j; i++, j--){
if(s[i]!=s[j]){
return false;
}
}
return true;
}
int main(){
int i;
string s1;
cin>>s1;
//别忘了首先判断s1 !!! 有两个测试用例是专门测试这个的!
if(judge(s1)){
cout<<s1<<" is a palindromic number."<<endl;
return 0;
}
for(i=0;i<10;i++){
string s2 = s1;
reverse(s1.begin(), s1.end());
string s3 = plus0(s1, s2);
cout<<s2<<" + "<<s1<<" = "<<s3<<endl;
if( judge(s3) ){
cout<<s3<<" is a palindromic number."<<endl;
break;
}
s1 = s3;
}
if(i==10){
printf("Not found in 10 iterations.\n");
}
return 0;
}
1137 STL
题目大意:
- 网上编程至少获得200分
- 期末考试至少60分(100分制)
- 按照公式计算最终得分
#include<stdio.h>
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct node{
string id;
int p, mid, final;
int g;
}node;
bool cmp(node a, node b){
if(a.g == b.g){
return a.id < b.id;
}
return a.g > b.g;
}
int main(){
int p, m, n;
cin>>p>>m>>n;
map<string, int> mp;
vector<node> v;
int count = 0;
for(int i=0;i<p;i++){
string id;
int score;
cin>>id>>score;
if(score >= 200){
count++;
mp[id] = count;
node temp;
temp.id = id;
temp.p = score;
temp.mid = temp.final = temp.g = -1;
v.push_back(temp);
}
}
for(int i=0;i<m;i++){
string id;
int score;
cin>>id>>score;
if(mp[id]!=0){
v[mp[id]-1].mid = score;
}
}
for(int i=0;i<n;i++){
string id;
int score;
cin>>id>>score;
if(mp[id]!=0){
v[mp[id]-1].final = score;
v[mp[id]-1].g = v[mp[id]-1].mid > v[mp[id]-1].final ? 0.4*v[mp[id]-1].mid+0.6*v[mp[id]-1].final + 0.5 : v[mp[id]-1].final;
}
}
sort(v.begin(), v.end(), cmp);
for(int i=0;i<count && v[i].g >=60; i++){
cout<<v[i].id<<" "<<v[i].p<<" "<<v[i].mid<<" "<<v[i].final<<" "<<v[i].g<<endl;
}
return 0;
}
1138 二叉树
题目大意: 给前序和中序遍历,输出后序遍历的第一个值 。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> preorder;
vector<int> inorder;
bool flag = false;
void find(int prest, int inst, int inen){
if(inst > inen || flag == true){
return;
}
int root = inst;
while(inorder[root] != preorder[prest]){
root++;
}
find(prest+1, inst, root-1);
find(prest+root-inst+1, root+1, inen);
if(flag == false){
printf("%d", inorder[root]);
flag = true;
}
}
int main(){
int n;
cin>>n;
preorder.resize(n);
inorder.resize(n);
for(int i=0;i<n;i++){
cin>>preorder[i];
}
for(int i=0;i<n;i++){
cin>>inorder[i];
}
find(0, 0, n-1);
return 0;
}
1139
题目大意: A-C-D-B,知道A,B,找C 和D
analogously 类似的
注:
- 如果是同性恋,那么要保证a找的朋友不能是b,b找的朋友不能是a (3.4测试用例针对这一点,一开始我没考虑到 )
- 考虑到0000之类的,还是用了string读入
- 第五个测试用例针对大数据,我就超时了555
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
#include<iostream>
using namespace std;
typedef struct node{
string c,d;
}node;
map<string, bool> gender;//记录性别
map<string, vector<string> > relation;//记录关系
bool e[10000][10000];//记录是否存在关系
bool cmp(node a, node b){
if(a.c == b.c){
return a.d < b.d;
}
return a.c < b.c;
}
int main(){
int n,m;
scanf("%d %d", &n, &m);
for(int i=0;i<m;i++){
string s1, s2;
cin>>s1>>s2;
if(s1.length() == 5){
s1.erase(0,1);
gender[s1] = true;
}else{
gender[s1] = false;
}
if(s2.length() == 5){
s2.erase(0,1);
gender[s2] = true;
}else{
gender[s2] = false;
}
relation[s1].push_back(s2);
relation[s2].push_back(s1);
e[atoi(s1.c_str())][atoi(s2.c_str())] = e[atoi(s2.c_str())][atoi(s1.c_str())] = true;
}
int k;
scanf("%d", &k);
for(int i=0;i<k;i++){
string s1, s2;
cin>>s1>>s2;
if(s1.length() == 5){
s1.erase(0,1);
}
if(s2.length() == 5){
s2.erase(0,1);
}
vector<node> ans;
for(int j=0;j<relation[s1].size();j++){
if(gender[relation[s1][j]] == gender[s1] && relation[s1][j] != s2){
for(int p=0;p<relation[relation[s1][j]].size();p++){
if(gender[relation[relation[s1][j]][p]] == gender[s2] && relation[relation[s1][j]][p]!=s1){
if(e[ atoi(relation[relation[s1][j]][p].c_str()) ][ atoi(s2.c_str()) ] == true){
node temp;
temp.c = relation[s1][j];
temp.d = relation[relation[s1][j]][p];
ans.push_back(temp);
}
}
}
}
}
sort(ans.begin(),ans.end(),cmp);
printf("%d\n",ans.size());
for(int j=0;j<ans.size();j++){
cout<<ans[j].c<<" "<<ans[j].d<<endl;
}
}
return 0;
}
网友评论