1.【P5735 【深基7.例1】距离函数】
【题解】
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double x1, x2, y, y2,x3,y3;
double m(double x1, double y1, double x2, double y2,double x3,double y3)
{
double n = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))+
sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3))
+sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
return n;
}
int main()
{
double m(double x1, double y1, double x2, double y2,double x3,double y3);
double q;
cin >> x1 >> y >> x2 >> y2>>x3>>y3;
q = m(x1, y, x2, y2,x3,y3);
cout << fixed<<setprecision(2)<<q;//保留小数点后两位小数
return 0;
}
2.【P5736 【深基7.例2】质数筛】
#include <iostream>
using namespace std;
int pd(int m)
{
if(m==1)return 0;//特判1的情况
for(int i=2;i<m;i++)
{
if(m%i==0)
return 0;
}
return 1;
}
int main()
{
int n,m,i=0;
cin>>m;
int a[m];
for(i=0;i<m;i++)
cin>>a[i];
for(i=0;i<m;i++)
{
if(pd(a[i]))
cout<<a[i];}
return 0;
}
3【P5738 【深基7.例4】歌唱比赛】
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
int m,a[20];
double hs(int a[],int m)
{
int s=0;double s2=0;
for(int i=1;i<m-1;i++)//跳过了最低分a[0]和最高分a[m-1];
{
s+=a[i];
}
s2=s*1.0/(m-2);
return s2;
}
int main()
{
double hs(int a[],int m);
int i,n,m;
cin>>n>>m;int a[m];double pj[n];
for(int j=0;j<n;j++)
{for(i=0;i<m;i++)
{
cin>>a[i];//i在输入每组唱歌成绩时从0--m的变化;
}
sort(a,a+m);
pj[j]=hs(a,m);//传的是排好顺序的a的首地址;pj[j]存的是每组平均成绩
}
sort(pj,pj+n);
cout<<fixed<<setprecision(2)<<pj[n-1];
}
【运行结果】
7 6
4 7 2 6 10 7
0 5 0 10 3 10
2 6 8 4 3 6
6 3 6 7 5 8
5 9 3 3 8 1
5 9 9 3 2 0
5 8 0 4 1 10
6.00
Process returned 0 (0x0) execution time : 64.820 s
Press any key to continue.
4.【P1075 [NOIP2012 普及组] 质因数分解】
#include <iostream>
using namespace std;
int sz(int a[2],int n)
{
for (int i = 2; i < n; i++)
{
if(n%i==0)
{
a[0] = i;
a[1]=n/i;//减少时间复杂度,否则会超时
break;
}
}
return a[1];//a[1]即较大的那个质数
}
int main()
{
int n, a[2];
cin >> n;
int max = sz(a, n);
cout << max;
}
5.【P5742 【深基7.例11】评等级】
#include<bits/stdc++.h>
using namespace std;
struct student
{
int xh,xy,sz;
};
int main()
{
int N;
cin>>N;struct student a[N];//知道N,方便设数组长度
for(int i=0;i<N;i++)
{
cin>>a[i].xh>>a[i].xy>>a[i].sz;
if(a[i].xy+a[i].sz>140&&(0.7*a[i].xy+a[i].sz*0.3)>=80)
cout<<"Excellent"<<"\n";
else cout<<"Not excellent\n";
}
}
6.【旗鼓相当的对手-加强版】
#include <iostream>
#include <algorithm>
using namespace std;
struct stu
{
string name;
int cg,mg,eg,s;
}z[1000],a[1005];
bool cmp(stu a,stu b){return a.name<b.name;}//注意传的参数,是新定义的 结构体类型名+结构体对象;
int main()
{
int N,i,j;
cin>>N;
for(i=0;i<N;i++){
cin>>z[i].name>>z[i].cg>>z[i].mg>>z[i].eg;z[i].s=z[i].cg+z[i].mg+z[i].eg;}
sort(z,z+N,cmp);//先排序再处理;
for(i=0;i<N-1;i++)
for(j=i+1;j<N;j++)//依次查找符合条件的情况
{
int scj=abs(z[i].s-z[j].s);//abs()返回()内的绝对值;
int ccj=abs(z[i].cg-z[j].cg);
int mcj=abs(z[i].mg-z[j].mg);
int ecj=abs(z[i].eg-z[j].eg);
if(scj<=10&&ccj<=5&&mcj<=5&&ecj<=5)
cout<<z[i].name<<" "<<z[j].name<<endl;
}
return 0;
}
7【P1304哥德巴赫猜想】
#include <iostream>
using namespace std;
bool pd(int n)
{
for(int i=2;i<n;i++)
if(n%i==0)
return false;
return true;
}
int main()
{
int N,i,j=2;
cin>>N;
for(i=4;i<=N;i+=2)
{
for(j=2;j<=i/2;j++)
{
if(pd(j)&&pd(i-j)) //用i-j减少时间复杂度
{cout<<i<<'='<<j<<'+'<<i-j<<endl;
break;}
}
}
return 0;
}
8.P2415【集合求和】
#include<iostream>
#include<cmath>
using namespace std;
long long s=0,a,i=0;
int main()
{
while(cin>>a) //结束循环的方式为先按 Ctrl+z 键,再回车;
{
i++;s+=a;
}
cout<<(long long)(s*pow(2,i-1));
return 0;
}
说明:(1)long long在计算过程被隐式转换成了double,需要用强制类型转换转换回long long输出。
(2)while(cin>>a) //结束循环的方式为先按 Ctrl+z 键,再回车;
(3)规律:设集合中的元素总共有n个,该集合的所有子集中,每个元素出现的次数为2的(n-1)次方;
9.【P5744 【深基7.习9】培训】
#include<iostream>
using namespace std;
struct student
{
string name;
int age,score;
}a[10000];
int main()
{
int n,i;
cin>>n;
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i].name;
cin>>a[i].age>>a[i].score;
a[i].age++;
if(a[i].score/5*6<600)
a[i].score=a[i].score/5*6;
else a[i].score=600;
}
for(i=0;i<n;i++)
{
cout<<a[i].name<<' '<<a[i].age<<" "<<a[i].score;
cout<<endl;
}
return 0;
}
10.【P1217 [USACO1.5]回文质数 Prime Palindromes】
#include <iostream>
#include <cmath>
using namespace std;
int ss(int m) // 判断素数(质数)
{
if(m==1||m%2==0) return 0;
for(int i=2;i<=sqrt(m);i++)
{
if(m%i==0)
return 0;
}return 1;
}
int pd(int m) //判断回文数
{
int n=m,s=0;
while(n!=0)
{
s=s*10+n%10;
n/=10;
}
if(m==s)return 1;
return 0;
}
int main()
{
int i,n,j;
cin>>i>>n;
for(j=i;j<=n;j++)
{
if(j>1e7)break; //实验得的 !不加上本句会RE
if(j%2==0) continue; //回文质数不可能为偶数
if((pd(j))&&(ss(j)))
cout<<j<<endl;
}
return 0;
}
11.【P5743 【深基7.习8】猴子吃桃】
(用函数做真的香,少死了多少脑细胞)
#include<iostream>
#include<cmath>
using namespace std;
int f(int l)
{
return (l+1)*2; //上一天没吃之前的数目
}
int main()
{
int n,i,s=1; // s为最后一天剩余的桃子数
cin>>n;
for(i=0;i<n-1;i++)
{s=f(s);}
cout<<s;
return 0;
}
12.【P5461 赦免战俘】
(推荐学习路径>>杨辉三角--南蛮图腾--赦免战俘)
#include <iostream>
using namespace std;
int a[1050][1050]; //找最大的内存容量,并且定义在主函数外面
void cal(int x,int y,int n)
{
if(n==0) a[x][y]=1;
else {
cal(x+(1<<(n-1)),y,n-1);//右上
cal(x,y+(1<<(n-1)),n-1);//左下}
cal(x+(1<<(n-1)),y+(1<<(n-1)),n-1);//右下}
}
int main()
{
int n,i,j;
cin>>n;
cal(0,0,n);
for(i=0;i<1<<n;i++)
for(j=0;j<1<<n;j++)
{
cout<<a[i][j];
if(j==(1<<n)-1) //1<<n相当于1*2^n
cout<<endl;
else cout<<' ';
}
return 0;
}
13.P5737 【深基7.例3】闰年展示
闰年的计算方法
判断是否为闰年的两个条件
1、能被400整除
2、能被4整除但不能被100整除
#include <iostream>
using namespace std;
int pd(int x)
{
if(((x%4==00)&&(x%100!=0))||(x%400==0))
return 1;
return 0;
}
int main()
{
int n,m,i,k=0,a[1500];
cin>>n>>m;
for(i=n;i<=m;i++)
{
if(pd(i))
{a[k]=i;k++;}
}
cout<<k<<endl;
for(i=0;i<k;i++)
cout<<a[i]<<" ";
}
14.P5740 【深基7.例9】最厉害的学生
#include <iostream>
using namespace std;
struct stu
{
string name;
int cg,mg,eg;
}z[1000];
int main()
{
int N,max=0,t=0;
cin>>N;
for(int i=0;i<N;i++) //打擂台的方法找max
{
cin>>z[i].name>>z[i].cg>>z[i].mg>>z[i].eg;
if(z[i].cg+z[i].mg+z[i].eg>max)
{max=z[i].cg+z[i].mg+z[i].eg;
t=i;}
}
cout<<z[t].name<<' '<<z[t].cg<<" "<<z[t].mg<<' '<<z[t].eg;
return 0;
}