T1
#include<iostream> using namespace std; int main(){ int n; while(cin>>n&&n!=0){//连续输入 int a[n]; for(int i=0;i<n;i++)cin>>a[i]; int count1=0,count2=0,m=a[0]; for(int i=0;i<n;i++){ count2=0; for(int j=0;j<n;j++)if(a[i]==a[j])count2++; if(count1<count2){ count1=count2; m=a[i]; } } cout<<m<<endl; } return 0; }
T2
#include<iostream> #include<cmath> using namespace std; int main(){ int n; cin>>n; for(int i=0;i<n;i++){ string a; cin>>a; int num=0; for(unsigned int j=0;j<a.size();j++){ if(a[j]=='4')a[j]='3'; if(a[j]=='5')a[j]='4'; if(a[j]=='6')a[j]='5'; if(a[j]=='7')a[j]='6'; if(a[j]=='9')a[j]='7'; } for(unsigned int j=0;j<a.size();j++){ num+=(int)(a[j]-48)*pow(8,a.size()-j-1); } cout<<num<<endl; } return 0; }
T3
#include<bits/stdc++.h> using namespace std; typedef long long ll; int day(string str) { int n; if(str=="monday")n=1; else if(str=="tuesday")n=2; else if(str=="thursday")n=4; else if(str=="wednesday")n=3; else if(str=="friday")n=5; else if(str=="saturday")n=6; else if(str=="sunday")n=7; return n; } int main() { ll t; string str1,str2; cin>>t; ll l,r; ll begin,end; for(int j=0; j<t; j++) { cin>>str1>>str2; cin>>l>>r; begin=day(str1); end=day(str2)+1; while(end-begin<l) { end+=7; } if(end-begin>r)cout<<"impossible"<<endl; int key=0; int onlyday=0; while(end-begin>=l&&end-begin<=r) { key++; onlyday=end-begin; end+=7; } if(key==1)cout<<onlyday<<endl; else if(key>1)cout<<"many"<<endl; } return 0; }
T4
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;
struct team{
int score;
int goal;
};
struct teamb{
string name;
int score;
int goal;
};
bool cmp(teamb a,teamb b){
if(a.score!=b.score)
return a.score>b.score;
else
return a.goal>b.goal;
}
int main(){
int t;
cin>>t;
vector<teamb> ranking; //将map数据放到vector中方便sort
while(t--){
ranking.clear();
map<string,team> res;
for(int i=0;i<12;i++){
int a,b;
string A,B;
string vs;//vs.
cin>>A>>a>>vs>>b>>B;
if(a>b) res[A].score+=3;
else if(a==b){
res[A].score+=1;
res[B].score+=1;
}else{
res[B].score+=3;
}
res[A].goal+=a-b;
res[B].goal+=b-a;
}
for(map<string,team>::iterator it=res.begin();it!=res.end();it++){
teamb temp;
temp.name=it->first;
temp.score=it->second.score;
temp.goal=it->second.goal;
ranking.push_back(temp);
}
sort(ranking.begin(),ranking.end(),cmp);
cout<<ranking[0].name<<" "<<ranking[1].name<<endl;
}
return 0;
}
T5
#include<iostream>
#include<stack>
#include<string>
using namespace std;
char let[4]={'<','(','[','{'};
char rit[4]={'>',')',']','}'};
int main(){
int n;
cin>>n;
while(n--){
string str;
cin>>str;
stack<char> q;
for(int i=0;i<str.length();i++){
int index;
if(!q.empty()){
char temp=q.top();
index=-1;
for(int j=0;j<4;j++){
if(temp==let[j]){
index=j;break;
}
}
if(str[i]==rit[index]){
q.pop();//弹出配对成功的字符
}else{
q.push(str[i]);//匹配失败,入队,等待配对
}
if(index==-1){
break;
}
}else{
q.push(str[i]);
}
}
if(q.empty()) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
T6
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;
struct team{
string name;
int pure_score=0;
int points=0;
int score=0;
};
bool cmp1(team x,team y){
if(x.points>y.points)
return true;
else if(x.points==y.points&&x.pure_score>y.pure_score)
return true;
else if(x.points==y.points&&x.pure_score==y.pure_score&&x.score>y.score)
return true;
else return false;
}
bool cmp2(team x,team y){
if(x.name<y.name)
return true;
else
return false;
}
int main(){
int n;
cin>>n;
team m[n];
for (int i = 0; i < n; i++) {
cin>>m[i].name;
}
for (int i = 0; i < (n*(n-1))/2; i++) {
string x, y;
int a = 0, b = 0, c = 0, d = 0;
cin >> x >> y;
int pos = x.find('-');
string xx = x.substr(0, pos);
string yy = x.substr(pos + 1);
for (int j = 0; j < n; j++) {
if (xx == m[j].name) {
a = j;
}
if (yy == m[j].name) {
b = j;
}
}
c = y[0] - '0';
d = y[2] - '0';//转为数字
if (c > d) {
m[a].points += 3;
}
if (c == d) {
m[a].points += 1;
m[b].points += 1;
}
if (c < d) {
m[b].points += 3;
}
m[a].score+=c;
m[b].score+=d;
m[a].pure_score+=(c-d);
m[b].pure_score+=(d-c);
}
sort(m,m+n,cmp1);
sort(m,m+(n/2),cmp2);
for (int i = 0; i < n/2; i++) {
cout<<m[i].name<<endl;
}
return 0;
}
T10
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct memory
{
int len;
int number;
bool Is_use=false;
memory(int L,int N)
{
len=L;
number=N;
Is_use=true;
}
};
bool cmp(const memory &l,const memory &r)
{
if(l.Is_use==r.Is_use)
{
return l.number<r.number;
}
return l.Is_use>r.Is_use;
}
int main()
{
vector<memory> v;
vector<memory>::iterator it;
int num_of_block=1;
int lmemory;
int t,m;
cin>>t>>m;
lmemory=m;
while(t--)
{
string op;
int num;
cin>>op;
if(op=="alloc")
{
cin>>num;
if(lmemory<num) cout<<"NULL"<<endl;
else
{
bool flag=0;
for(it=v.begin();it!=v.end();it++)
{
if(it->Is_use==false&&it->len>=num)
{
// cout<<num_of_block<<endl;
// it->len-=num;
// //v.insert(it-1,memory(num,num_of_block));
// leftmemory-=num;
// num_of_block++;
// flag=true;
// break;
cout<<num_of_block<<endl;
int temp=it->len;
it->number=num_of_block;
it->Is_use=true;
it->len=num;
v.insert(it+1,memory(temp-num,-1));
lmemory-=num;
num_of_block++;
flag=true;
break;
}
}
if(!flag)
{
int sum=0;
for(it=v.begin();it!=v.end();it++)
{
sum+=it->len;
}
if(sum+num>m) cout<<"NULL"<<endl;
else
{
cout<<num_of_block<<endl;
lmemory-=num;
v.push_back(memory(num,num_of_block));
num_of_block++;
}
}
}
}
else if(op=="erase")
{
cin>>num;
bool flag=0;
for(it=v.begin();it!=v.end();it++)
{
if(it->number==num&&it->Is_use==true)
{
it->Is_use=false;
flag=true;
lmemory+=it->len;
break;
}
}
if(!flag) cout<<"ILLEGAL_ERASE_ARGUMENT"<<endl;
}
else
{
sort(v.begin(),v.end(),cmp);
for(it=v.begin();it!=v.end();it++)
if(it->Is_use==false)
{
lmemory+=it->len;
v.erase(it);
it--;
}
}
}
system("pause");
return 0;
}
T11
#include<bits/stdc++.h>
using namespace std;
int main() {
for (int i = 0; i < 100; i++) {
int n;
double sum=0;
cin>>n;
double x=0;
int y=0;
if(n==NULL) break;
if(n==0) continue;
double num[n];
for (int j = 0; j < n; j++) {
cin>>num[j];
sum+=num[j];
}
double m=sum/n;
for (int j = 0; j < n; j++) {
x+=(num[j]-m)*(num[j]-m);
}
y=x/n;
cout<<y<<endl;
}
}
T12
#include<bits/stdc++.h>
using namespace std;
int main() {
int n,num=0;
cin>>n;
for (int i =0;i<n ;i++){
string str[4],s="00000000";
getline(cin,str[0],'.');
getline(cin,str[1],'.');
getline(cin,str[2],'.');
getline(cin,str[3]);//输入四段ip;
for(int i=0;i<4;i++){
int t= atoi(str[i].c_str());
int j=7;
while(t>0){
s[j]=t%2+48;
t/=2;
j--;
}
for(int j=0;j<8;j++){
if(s[j]=='1') num++;
}
s="00000000";
}
cout << num<< endl;
num=0;
}
return 0;
}
T13
#include<iostream>
#include<vector>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
vector<int> res(m,0);
vector<vector<int> > map(m);
for(int i=0;i<n;i++){
string s;
cin>>s;
for(int j=0;j<m;j++){
map[i].push_back(s[j]-'0');//记录输入,方便后面减去
res[j]+=map[i][j];//求出m个灯每个灯可以被多少个开关控制
}
}
for(int i=0;i<n;i++){
int j;
for(j=0;j<m;j++){
int temp=res[j]-map[i][j];
if(temp<=0) break;//减去某一次开关。如果有个灯没亮,说明不能不开这个开关
}
if(j==m) {
cout<<"YES"<<endl;return 0;//j==m说明减去后,灯还是全亮的
}
}
cout<<"NO"<<endl;//一直没yes就no了呗
return 0;
}
T14
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int x, y;//坐标
int l = 0, r = 0;//左右的数量
for (int i = 0; i < n; i++)
{
cin >> x >> y;
if (x < 0) //统计左右的点的个数
l++;
else
r++;
}
if (l <= 1 || r <= 1)//存在小于1的
cout << "Yes";
else
cout << "No";
}
T15
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string reverseWords(string sentence) {
stringstream ss(sentence);
string word;
string reversedSentence;
while (ss >> word) {
for (int i = word.length() - 1; i >= 0; i--) {
reversedSentence += word[i];
}
reversedSentence += " ";
}
return reversedSentence.substr(0, reversedSentence.length() - 1);
}
int main() {
int N;
cin >> N;
cin.ignore(); // 忽略第一行末尾的换行符
for (int i = 0; i < N; i++) {
string sentence;
getline(cin, sentence);
string reversedSentence = reverseWords(sentence);
cout << reversedSentence << endl;
}
return 0;
}
T16
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<sstream>//某种字符串流基类
using namespace std;
int main()
{
int m,n;
cin>>m>>n;
stringstream ss3;
ss3<<n;//将n转化为字符存入ss3
string str1=ss3.str();
for(int i=1;i<=m;i++)
{
if(i%n==0)cout<<i<<" ";
else
{
stringstream ss;
ss<<i;
string str=ss.str();
int p=str.find(str1);//find()函数查找包含情况,返回位置
if(p>=0)cout<<i<<" ";//存在返回位置,说明字符串中包含该字符
}
}
}
T17
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<sstream>//某种字符串流基类
#include <algorithm>
using namespace std;
struct st{
string name;
int numb;
};
bool cmp(st st1,st st2){
return st1.numb<st2.numb;
}
int main()
{
int n,m;
cin>>n>>m;
cin.ignore();
st arr[m];
for (int i = 0; i < m; i++) {
string str;
getline(cin,str);
int num=0;
for (int j = 0; j < n; j++) {
for (int k = j+1; k < n; k++) {
if (str[k]-str[j]<0) num++;
}
}
arr[i].name=str;
arr[i].numb=num;
}
sort(arr, arr+m, cmp);
for (int i = 0; i < m; i++) {
cout<<arr[i].name<<endl;
}
return 0;
}
T18
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double calculateArea(double x1, double y1, double x2, double y2, double x3, double y3) {
double area = 0.5 * abs((x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3));
return area;
}
int main() {
while (true) {
double x1, y1, x2, y2, x3, y3;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
// 判断是否输入结束
if (x1 == 0 && y1 == 0 && x2 == 0 && y2 == 0 && x3 == 0 && y3 == 0) {
break;
}
double area = calculateArea(x1, y1, x2, y2, x3, y3);
cout << fixed << setprecision(6) << area << endl;
}
return 0;
}
T19
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string str1;
cin>>str1;//读入字符串
long long int n;
stringstream ss;
ss<<str1;
ss>>n;//转化为整型,为了后面的计算
ss.clear();//一定要记得clear
int count=str1.length();
str1=str1+str1;
bool flag=true;
for(int i=1;i<=count;i++)
{
flag=1;
long long int m=n*i;
string strm;
ss<<m;
ss>>strm;
ss.clear();
int p=str1.find(strm);
if(p==-1)
{
flag=0;
break;
}
}
if(flag==1) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
T20
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, P1, P2, P3, T1, T2;
std::cin >> n >> P1 >> P2 >> P3 >> T1 >> T2;
int totalPower = 0;
int previousEndTime = 0;
for (int i = 0; i < n; i++) {
int startTime, endTime=0;
cin >> startTime >> endTime;
int workTime = endTime - startTime;
totalPower += workTime * P1;
if ((startTime-previousEndTime>0)&&i>0){
int time=min(startTime-previousEndTime, T1);
totalPower += time * P1;
if (startTime-previousEndTime> T1) {
int screenSaverTime = min(startTime-previousEndTime-T1, T2);
totalPower += screenSaverTime * P2;
if (startTime-previousEndTime > T2) {
int sleepTime = startTime-previousEndTime-T2-T1;
totalPower += sleepTime * P3;
}
}
}
previousEndTime = endTime;
}
std::cout << totalPower << std::endl;
return 0;
}
T21
#include <iostream>
#include <string>
using namespace std;
// 函数:计算校验码
int calculateChecksum(string number, int base) {
int sum = 0;
// 将B进制转换为十进制
for (char digit : number) {
if (digit >= 'a' && digit <= 'f') {
sum += digit - 'a' + 10;
} else {
sum += digit - '0';
}
}
// 判断数字和是否被B-1整除
if (sum % (base - 1) == 0) {
return 0;
} else {
int checksum = (base - 1) - sum % (base - 1);
return checksum;
}
}
int main() {
int t;
cin >> t;
while (t--) {
int base;
string number;
cin >> base >> number;
int checksum = calculateChecksum(number, base);
if(checksum<10) {
cout << checksum << endl;
} else if (checksum==10) cout << 'a' << endl;
else if (checksum==11) cout << 'b' << endl;
else if (checksum==12) cout << 'c' << endl;
else if (checksum==13) cout << 'd' << endl;
else if (checksum==14) cout << 'e' << endl;
else if (checksum==15) cout << 'f' << endl;
}
return 0;
}
T18-三角形面积
#include <iostream> #include <cmath> #include <iomanip> using namespace std; double calculateArea(double x1, double y1, double x2, double y2, double x3, double y3) { double area = 0.5 * abs((x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3)); return area; } int main() { while (true) { double x1, y1, x2, y2, x3, y3; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; // 判断是否输入结束 if (x1 == 0 && y1 == 0 && x2 == 0 && y2 == 0 && x3 == 0 && y3 == 0) { break; } double area = calculateArea(x1, y1, x2, y2, x3, y3); cout << fixed << setprecision(6) << area << endl; } return 0; }
注意输出的小数点精度,用cout << fixed << setprecision(6) << area << endl;控制
T19-循环数
#include <iostream> #include <sstream> using namespace std; int main() { string str1; cin>>str1;//读入字符串 long long int n; stringstream ss; ss<<str1; ss>>n;//转化为整型,为了后面的计算 ss.clear();//一定要记得clear int count=str1.length(); str1=str1+str1; bool flag=true; for(int i=1;i<=count;i++) { flag=1; long long int m=n*i; string strm; ss<<m; ss>>strm; ss.clear(); int p=str1.find(strm); if(p==-1) { flag=0; break; } } if(flag==1) cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; }
把数直接复制两份拼接一下,142857变成142857142857,再用find判断