POJ 1003
题目链接
http://poj.org/problem?id=1003
大意:长度=1/2+1/3+…+1/n,给定长度值,求n
#include<iostream>
using namespace std;
int main()
{float len = 0,sum;int n;while(cin >> len && len != 0){for(n=2,sum=0;sum<len;++n){sum += 1/(n*1.0);}cout << n-2 << " " << "card(s)" << endl;}return 0;
}
POJ 1004
题目链接
http://poj.org/problem?id=1004
大意:求平均数
#include<iostream>
using namespace std;
int main()
{float money,avgmoney=0;int i = 0;while(cin >> money && i != 12){avgmoney += money;++i;}cout << "$" << avgmoney/12 << endl;
}
POJ 1005
题目链接
http://poj.org/problem?id=1005
大意:求一个点什么时候被慢慢变大的半圆吃掉
#include<iostream>
using namespace std;
#define PI 3.141592654
int main()
{double X,Y;double area,distance,dangerdistance;int year;int N;cin >> N;for(int i = 1; i <= N; ++i){cin >> X >> Y;distance = X*X+Y*Y;dangerdistance=0; //之前没有写这句,没有初始化,踩过好几次地雷了!!!!!!!!for(area = 0,year =0; dangerdistance <= distance; ++year){area += 50;dangerdistance = 2*area/PI;}cout << "Property " << i << ": This property will begin eroding in year " << year << "." << endl;}cout << "END OF OUTPUT.";return 0;
}
POJ 1207
题目链接
http://poj.org/problem?id=1207
大意:有一个数,按那个规则,最后能够转换到1,算出这个序列的长度,然后输入两个数,在这两个数构成的闭区间中,每个数都有其序列长度,求这个序列中最长的一个。
#include<iostream>
using namespace std;
int main()
{int a,b;while(cin >> a >> b){cout << a << " " << b << " " ;//坑,要先输出a,b,如果调换了,输出就颠倒了if(a > b)swap(a,b);int maxcyclen=0;int cyctime=0;int num=0;for(int i = a,j=0; i <= b; ++i,++j){num = i;cyctime=1;while(num != 1){if(num%2==1){num = 3*num+1;}else{num /= 2;}++cyctime;}if(maxcyclen < cyctime){maxcyclen = cyctime;}}cout << maxcyclen << endl;}return 0;
}
POJ 3299
题目链接
http://poj.org/problem?id=3299
大意:公式推导,给任意两个,求第三个。(注意输入顺序)
#include<iostream>
using namespace std;
#include<iomanip>
#include<math.h>int main()
{cout << setiosflags(ios::fixed) << setprecision(1);//需要头文件#include <iomanip>,小数点精度1位double t,d,h,num1,num2;char alpha,beta;while(cin >> alpha && alpha != 'E'){cin >> num1 >> beta >> num2;if(alpha=='T'&&beta=='D' || alpha=='D'&&beta=='T'){t = num1;d = num2;if(alpha=='D')//输入顺序变了,名称与内容不符,换回来swap(t,d);h=t+0.5555*(6.11*exp(5417.7530*((1/273.16)-(1/(d+273.16))))-10);}else if(alpha=='T'&&beta=='H' || alpha=='H'&&beta=='T'){t = num1;h = num2;if(alpha=='H') //输入顺序变了,名称与内容不符,换回来swap(t,h);d=1/(1/273.16-log(((h-t)/0.5555+10.0)/6.11)/5417.7530)-273.16;;}else if(alpha=='D'&&beta=='H' || alpha=='H'&&beta=='D'){d = num1;h = num2;if(alpha=='H') //输入顺序变了,名称与内容不符,换回来swap(d,h);t=h-0.5555*(6.11*exp(5417.7530*((1/273.16)-(1/(d+273.16))))-10.0);} cout << "T " << t << " D " << d << " H " << h << endl; }return 0;
}
POJ 2159
题目链接
http://poj.org/problem?id=2159
大意:
1.一串字符串(大写字母),每个字母可以按照一个数平移成另一个字母
2.每一个字符加密后,再乱序
3.给定A,B两个字符串,求A是否可能是B的密文
之前题目理解错了,以为每个字母都是平移1位(按照示例的理解)
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{int secretnum = 1; //移动的位数(每个都一样)char word1,word2;vector<char> secrettext,origintext;while(cin.get(word1)){if(word1 == '\n')break;if(word1 >= 'A' && word1 <= 'Z'){word1 = word1 - secretnum;if(word1 < 'A')word1 = word1 + 26;secrettext.push_back(word1);}else{continue;}}while(cin.get(word2)){if(word2 == '\n')break;if(word2 >= 'A' && word2 <= 'Z'){origintext.push_back(word2);}else{continue;}}sort(secrettext.begin(),secrettext.end());//字母升序排序sort(origintext.begin(),origintext.end());//字母升序排序for(int i = 0;i != secrettext.size();++i){if(secrettext[i] != origintext[i]){cout << "NO" ;return 0;}}cout << "YES" ;//都升序排列后,每一位都相等则ok(题目没说都移动同样位数,解答错误)return 0;
}
正确的解答
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{char word1, word2;int secrettext[26]={0}, origintext[26]={0};while(cin.get(word1)){if(word1 == '\n')break;if(word1 >= 'A' && word1 <= 'Z'){++secrettext[word1-'A']; //统计每个字符的频次存入数组}else{continue;}}while(cin.get(word2)){if(word2 == '\n')break;if(word2 >= 'A' && word2 <= 'Z'){++origintext[word2-'A']; //统计每个字符的频次存入数组}else{continue;}}sort(secrettext,secrettext+26); // 对频次进行排序sort(origintext,origintext+26); // 对频次进行排序for(int i = 0;i != 26;++i){if(secrettext[i] != origintext[i]){cout << "NO" ;return 0;}}cout << "YES" ; //频次完全一致,则每个字符经过一定平移即可得到第二行的字符串return 0;
}
POJ 1083
题目链接
http://poj.org/problem?id=1083
大意:
1.400个房间,从一个房间移动1张桌子到另一个房间,需要10分钟。
2.过道只能有一张桌子,包含门前的位置,被占用的时候,其它需要经过的移动需要等待,不能同时进行。
解法:
1.申请200大小的数组代表门前的过道,从房间m到房间n,(m < n),则 ( m - 1 ) /2 到(n - 1)/ 2 的元素都加10;
2.扫描数组,最大的元素即为搬运的时间。
#include<iostream>
#include<cstring>
using namespace std;
int main()
{int testtime, table, roomid1, roomid2;int corridor[200]={0}, corridor_id, maxtime;cin >> testtime;while(testtime--){memset(corridor,0,sizeof(corridor)); //清零初始化(不能写corridor[200]={0};只能定义的时候写一次),memset需要cstring头文件//经测试,单独写的corridor[200]={0};对数组没有任何操作,编译器可能不报错,需要避开这个坑!!!cin >> table;while(table--){cin >> roomid1 >> roomid2;if(roomid1 > roomid2)swap(roomid1,roomid2);for(corridor_id = (roomid1-1)/2; corridor_id <= (roomid2-1)/2; ++corridor_id){corridor[corridor_id] += 10;}}maxtime = 0;for(corridor_id = 0; corridor_id != 200; ++corridor_id){if(maxtime < corridor[corridor_id])maxtime = corridor[corridor_id];}cout << maxtime << endl;}
}
POJ 3094
题目链接
http://poj.org/problem?id=3094
大意:
每一行字符串包括空格,经过对每个字符乘以系数,然后加总求和。
例如: ACM:1 * 1 + 2 * 3 + 3 * 13 = 46 (A=1,B=2,…空格=0)
MID CENTRAL:1 * 13 + 2 * 9 + 3 * 4 + 4 * 0 (空格占位置)+ 5 * 3 + 6 * 5 + 7 * 14 + 8 * 20 + 9 * 18 + 10 * 1 + 11 * 12 = 650
#include<iostream>
using namespace std;
int main()
{char alpha;int quicksum = 0;while(cin.get(alpha) ){if(alpha == '#')break;quicksum = 0;for(int i = 1; alpha != '\n';++i){if(alpha != ' ')quicksum = quicksum + i*(alpha-'A'+1);cin.get(alpha); //cin.get()可以捕捉空格, cin >> char 不可以捕捉空格回车}cout << quicksum << endl;}
}
POJ 2388
题目链接
http://poj.org/problem?id=2388
大意:求中位数,简单。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{int num;cin >> num;int *milk = new int[num];for(int i = 0; i != num; ++i){cin >> milk[i];}sort(milk,milk+num);cout << milk[num/2];delete [] milk;milk = NULL;return 0;
}
/*** @description: poj2388 求中位数* @author: michael ming* @date: 2019/5/31 0:12* @modified by: */
#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
int main()
{int N, num, i = 0;cin >> N;vector<int> maxheap, minheap;while(i++ < N && cin >> num){if(maxheap.empty()){maxheap.push_back(num);continue;}//----选择插入哪个堆-----if(!maxheap.empty() && num <= maxheap[0]){maxheap.push_back(num);push_heap(maxheap.begin(),maxheap.end());//默认采用 < , 大堆}else if(!maxheap.empty() && num > maxheap[0]){minheap.push_back(num);push_heap(minheap.begin(),minheap.end(),greater<int>());//小堆,采用 >}//----平衡两个堆的节点比列----if(maxheap.size() > minheap.size() && maxheap.size() - minheap.size() > 1){minheap.push_back(maxheap[0]);//大堆顶进入小堆push_heap(minheap.begin(),minheap.end(),greater<int>());pop_heap(maxheap.begin(),maxheap.end());//堆顶到末尾了maxheap.pop_back();//删除到末尾的"堆顶"}else if(maxheap.size() < minheap.size()){maxheap.push_back(minheap[0]);push_heap(maxheap.begin(),maxheap.end());//默认采用 < , 大堆pop_heap(minheap.begin(),minheap.end(),greater<int>());minheap.pop_back();}}if(maxheap.size())cout << maxheap[0] << endl;return 0;
}