题目地址 题目难度就是入门难度,总的来说难度不高。
noob1 Hello Nowcoder
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
cout<<"Hello Nowcoder!";
return 0;
}
noob2 牛牛学说话之-整数
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a; cin>>a; cout<<a;
return 0;
}
noob3 牛牛学说话之-浮点数
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
double a; cin>>a;
printf("%.3lf",a);
return 0;
}
noob4 牛牛学说话之-字符串
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
string s; cin>>s;
cout<<s;
return 0;
}
noob5 复读机
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
long long int a,b;
double c;
string d,e;
cin>>a>>b>>c>>d>>e;
cout<<a<<endl;
cout<<b<<endl;
printf("%.1lf\n",c);
cout<<d<<endl;
cout<<e<<endl;
return 0;
}
noob6 牛牛学加法
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a,b; cin>>a>>b;
cout<<a+b;
return 0;
}
noob7 疫情死亡率
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
double a,b; cin>>a>>b;
printf("%.3lf%%",b/a*100);
return 0;
}
noob8 计算带余除法
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a,b; cin>>a>>b;
cout<<a/b<<" "<<a%b;
return 0;
}
noob9 整数的个位
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a; cin>>a;
a=abs(a);
cout<<a%10;
return 0;
}
noob10 整数的十位
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a; cin>>a;
cout<<(a%100)/10;
return 0;
}
noob11 平方根
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a; cin>>a;
cout<<(int)sqrt(a);
return 0;
}
noob12 反向输出一个四位数
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
string a; cin>>a;
reverse(a.begin(),a.end());
cout<<a<<endl;
return 0;
}
noob13 温标转换
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
double k; cin>>k;
double c; c=k-273.15;
printf("%.6lf",c*1.8+32);
return 0;
}
noob14 绕距
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
double x,y,xx,yy; cin>>x>>y>>xx>>yy;
double x1=abs(x-xx)+abs(y-yy);
double x2=sqrt((x-xx)*(x-xx)+(y-yy)*(y-yy));
printf("%.6lf",abs(x1-x2));
return 0;
}
noob15 求四位数各个数位之和
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a; cin>>a;
int ans=0;
while(a) ans+=a%10,a/=10;
cout<<ans;
}
noob16 时间转换
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a; cin>>a;
printf("%d %d %d",a/3600,(a%3600)/60,a%60);
}
noob17 计算机内存
#include <iostream>
using namespace std;
int main() {
long long int a; cin>>a;
cout<<a*1024*1024/4;
}
// 64 位输出请用 printf("%lld")
noob18 牛牛学立体
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a,b,c; cin>>a>>b>>c;
cout<<(a*b+a*c+b*c)*2<<endl<<a*b*c;
return 0;
}
noob19 成绩
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a,b,c; cin>>a>>b>>c;
cout<<(int)(a*0.2+b*0.3+c*0.5);
return 0;
}
noob20 小乐乐求和
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
long long int a; cin>>a;
cout<<a*(a+1)/2;
return 0;
}
noob21 明天星期几?
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a; cin>>a;
a=a+1;
if(a>7) cout<<1;
else cout<<a;
return 0;
}
noob22 判断闰年
#include<bits/stdc++.h>
using namespace std;
bool check(int x)
{
if(x%400==0) return true;
if((x%4==0) && x%100) return true;
return false;
}
int main(void)
{
int a; cin>>a;
if(check(a)) cout<<"yes";
else cout<<"no";
return 0;
}
noob23 比大小
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a,b; cin>>a>>b;
if(a>b) cout<<">";
else if(a<b) cout<<"<";
else cout<<"=";
return 0;
}
noob24 卡拉兹函数
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int n; cin>>n;
if(n&1) cout<<n*3+1;
else cout<<n/2;
return 0;
}
noob25 牛妹数
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a; cin>>a;
if(a%2==0 && a>50) cout<<"yes";
else cout<<"no";
return 0;
}
noob26 牛牛是否被叫家长
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a,b,c; cin>>a>>b>>c;
double x=(a+b+c)/3.0;
if(x<60) puts("YES");
else puts("NO");
return 0;
}
noob27 最大最小值
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a,b,c; cin>>a>>b>>c;
printf("The maximum number is : %d\n",max({a,b,c}));
printf("The minimum number is : %d",min({a,b,c}));
return 0;
}
noob28 四季
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a; cin>>a;
int b=a%10;
if(b>=3&&b<=5) puts("spring");
else if(b>=6&&b<=8) puts("summer");
else if(b>=9&&b<=11) puts("autumn");
else puts("winter");
return 0;
}
noob29 多组输入a+b II
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a,b,t; cin>>t;
while(t--){
cin>>a>>b;
cout<<a+b<<endl;
}
return 0;
}
noob30 多组数据a+b III
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int a,b;
while(cin>>a>>b,a&&b) cout<<a+b<<endl;
return 0;
}
noob31 素数判断
#include<bits/stdc++.h>
using namespace std;
bool check(int x)
{
if(x==1) return false;
for(int i=2;i<=x/i;i++)
{
if(x%i==0) return false;
}
return true;
}
int main(void)
{
int t; cin>>t;
while(t--)
{
int x; cin>>x;
if(check(x)) puts("Yes");
else puts("No");
}
return 0;
}
noob32 牛牛学数列
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int flag=1;
int n,ans=0; cin>>n;
for(int i=1;i<=n;i++) ans+=flag*i,flag=flag*-1;
cout<<ans;
return 0;
}
noob33 牛牛学数列2
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int n; cin>>n;
double ans=0;
for(int i=1;i<=n;i++) ans+=1.0/i;
printf("%.6lf",ans);
return 0;
}
noob34 最大的差
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int n; cin>>n;
vector<int>ve;
for(int i=0;i<n;i++)
{
int x; cin>>x;
ve.push_back(x);
}
sort(ve.begin(),ve.end());
cout<<ve[n-1]-ve[0];
return 0;
}
noob35 牛牛学数列4
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int n; cin>>n;
int ans=0,temp=0;
for(int i=1;i<=n;i++)
{
temp+=i;
ans+=temp;
}
cout<<ans;
return 0;
}
noob36 牛牛学数列5
#include<bits/stdc++.h>
using namespace std;
long long int a[55];
int main(void)
{
a[1]=1,a[2]=1;
int n; cin>>n;
for(int i=3;i<=n;i++) a[i]=a[i-1]+a[i-2];
cout<<a[n];
return 0;
}
noob37 数位之和
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int n; cin>>n;
n=abs(n);
int ans=0;
while(n) ans+=(n%10),n/=10;
cout<<ans;
return 0;
}
noob38 牛牛数数
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int n; cin>>n;
for(int i=1;i<=n;i++)
{
int temp=i,flag=0;
if(temp%4==0) continue;
while(temp)
{
if(temp%10==4) {
flag=1;
break;
}
temp/=10;
}
if(flag) continue;
cout<<i<<endl;
}
return 0;
}
noob39 牛牛学数列6
#include<bits/stdc++.h>
using namespace std;
long long int a[25];
int main(void)
{
a[1]=0,a[2]=1,a[3]=1;
int n; cin>>n;
for(int i=4;i<=n;i++)a[i]=a[i-1]+2*a[i-2]+a[i-3];
cout<<a[n]<<endl;
return 0;
}
noob40 二维斐波那契数列
#include<bits/stdc++.h>
using namespace std;
long long int a[1010][1010];
const int mod=1e9+7;
int main(void)
{
int n,m; cin>>n>>m;
for(int i=1;i<=n;i++) a[i][1]=1;
for(int i=1;i<=m;i++) a[1][i]=1;
for(int i=2;i<=n;i++)
{
for(int j=2;j<=m;j++)
a[i][j]=(a[i-1][j]+a[i][j-1])%mod;
}
cout<<a[n][m]<<endl;
return 0;
}
noob41 神秘石像的镜像序列
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
vector<int>ve;
int x;
while(cin>>x,x) ve.push_back(x);
for(int i=ve.size()-1;i>=0;i--) cout<<ve[i]<<" ";
return 0;
}
noob42 左侧严格小于计数
#include<bits/stdc++.h>
using namespace std;
int a[105],n,b[105];
int main(void)
{
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=1;i<n;i++)
{
int flag=0;
for(int j=0;j<i;j++) if(a[j]<a[i]) flag++;
b[i]=flag;
}
for(int i=0;i<n;i++) cout<<b[i]<<" ";
return 0;
}
noob43 牛牛的数学作业
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
long long int a[N];
int main(void)
{
int t; cin>>t;
while(t--)
{
int n; cin>>n;
double avg=0;
for(int i=0;i<n;i++) cin>>a[i],avg+=a[i];
sort(a,a+n);
avg/=n;
double sum=0;
for(int i=0;i<n;i++)
{
sum+=(a[i]-avg)*(a[i]-avg);
}
sum=sum*1.0/n;
printf("%d %.3lf\n",a[n-1]-a[0],sum);
}
return 0;
}
noob44 数组计数维护
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int a[N],t;
int main(void)
{
cin>>t;
while(t--)
{
int n,k; cin>>n>>k;
int s=0,cnt=0;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++)
{
if(a[i]>=k) s+=a[i];
if(a[i]==0 && s>=1) s--,cnt++;
}
cout<<cnt<<endl;
}
return 0;
}
noob45 记数问题
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int n,x,cnt; cin>>n>>x;
int ans=0;
for(int i=1;i<=n;i++)
{
int temp=i;
while(temp){
if(temp%10==x) ans++;
temp/=10;
}
}
cout<<ans;
return 0;
}
noob46 约瑟夫环
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int n,k,m; cin>>n>>k>>m;
deque<int>q;
for(int i=k;i<n;i++) q.push_back(i);
for(int i=0;i<k;i++) q.push_back(i);
int flag=1;
while(q.size()>=2)
{
int temp=q.front();
q.pop_front();
if(flag==m){
flag=1;
}
else{
flag++;
q.push_back(temp);
}
}
cout<<q.front();
return 0;
}
noob47 校门外的树
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int a[N],n,m,l,r;
int main(void)
{
cin>>n>>m;
while(m--)
{
cin>>l>>r;
for(int i=l;i<=r;i++) a[i]--;
}
int ans=0;
for(int i=0;i<=n;i++)
if(a[i]==0) ans++;
cout<<ans;
return 0;
}
noob48 单组_二维数组
#include<bits/stdc++.h>
#include <linux/limits.h>
using namespace std;
int main(void)
{
long long int n,m,x,ans=0;
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++) cin>>x,ans+=x;
cout<<ans;
return 0;
}
noob49 上三角矩阵判定
#include<bits/stdc++.h>
using namespace std;
int a[25][25],n;
int main(void)
{
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++) cin>>a[i][j];
int flag=1;
for(int i=1;i<n;i++)
for(int j=0;j<i;j++)
if(a[i][j]) flag=0;
if(flag) puts("YES");
else puts("NO");
return 0;
}
noob50 矩阵转置
#include<bits/stdc++.h>
using namespace std;
const int N=35;
int a[N][N],n,m;
int b[N][N];
int main(void)
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
b[j][i]=a[i][j];
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
noob51 杨辉三角
#include<bits/stdc++.h>
using namespace std;
const int N=150;
int a[N][N],n;
int main(void)
{
cin>>n;
for(int i=1;i<=n;i++) a[i][i]=1;
for(int i=1;i<=n;i++) a[i][1]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]==0) a[i][j]=a[i-1][j]+a[i-1][j-1];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++) cout<<a[i][j]<<" ";
puts("");
}
return 0;
}
noob52 扫雷
#include<bits/stdc++.h>
using namespace std;
const int N=1050;
string s[N];
int n,m;
int check(int x,int y)
{
int cnt=0;
for(int i=x-1;i<=x+1;i++)
{
for(int j=y-1;j<=y+1;j++)
{
if( i<0 || i>=n || y<0 || y>=m) continue;
if(s[i][j]=='*') cnt++;
}
}
return cnt;
}
int main(void)
{
cin>>n>>m;
for(int i=0;i<n;i++) cin>>s[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(s[i][j]=='.') cout<<check(i,j);
else cout<<"*";
}
puts("");
}
return 0;
}
noob53 年轻人不讲5的
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
string s; cin>>s;
for(int i=0;i<s.size();i++)
{
if(s[i]=='5') cout<<"*";
else cout<<s[i];
}
return 0;
}
noob54 斗兽棋
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
string a,b; cin>>a>>b;
if(a=="elephant" && b=="tiger") puts("win");
else if(a=="tiger" && b=="cat") puts("win");
else if(a=="cat" && b=="mouse") puts("win");
else if(a=="mouse" && b=="elephant") puts("win");
else if(b=="elephant" && a=="tiger") puts("lose");
else if(b=="tiger" && a=="cat") puts("lose");
else if(b=="cat" && a=="mouse") puts("lose");
else if(b=="mouse" && a=="elephant") puts("lose");
else puts("tie");
return 0;
}
noob55 添加逗号
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
string s;
string ans;
int cnt=1;
cin>>s;
for(int i=s.size()-1;i>=0;i--)
{
ans=s[i]+ans;
if(i&&cnt==3) ans=","+ans,cnt=1;
else cnt++;
}
cout<<ans;
return 0;
}
noob56 BFS
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
string a; cin>>a;
for(int i=0;i<a.size();i++) a[i]=tolower(a[i]);
int c=a.find("bob");
cout<<c;
return 0;
}
noob57 凯撒加密
#include<bits/stdc++.h>
#include <sys/types.h>
using namespace std;
int n;
string s;
int main(void)
{
cin>>n>>s;
for(int i=0;i<s.size();i++)
{
s[i]='a'+(s[i]-'a'+n)%26;
cout<<s[i];
}
return 0;
}
noob58 无限长正整数排列字符串
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int n; cin>>n;
string s; s="0";
for(int i=1;i<=1000;i++) s+=to_string(i);
cout<<s[n];
return 0;
}
noob59 简写单词
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
string s;
while(cin>>s){
cout<<char(toupper(s[0]));
}
return 0;
}
noob60 牛牛的考试
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int n; cin>>n;
while(n--)
{
map<int,int>mp;
vector<int>ve;
for(int i=0;i<4;i++){
string s; cin>>s;
ve.push_back(s.size());
mp[s.size()]=i;
}
sort(ve.begin(),ve.end());
int a,b,c,d;
a=ve[0],b=ve[1],c=ve[2],d=ve[3];
if(b>a && c>a && d>a && d==c){
cout<<(char)('A'+mp[a])<<endl;
}else if(d>a && d>b && d>c && a==b){
cout<<(char)('A'+mp[d])<<endl;
}else cout<<"C"<<endl;
}
return 0;
}
noob61 字符串操作
#include<bits/stdc++.h>
using namespace std;
int n,m;
string s;
int main(void)
{
cin>>n>>m>>s;
while(m--)
{
int l,r;
char c1,c2;
cin>>l>>r>>c1>>c2;
for(int i=l-1;i<=r-1;i++)
if(s[i]==c1) s[i]=c2;
}
cout<<s;
return 0;
}
noob62 a 加 b 问题(函数)
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 实现求两个参数的和
* @param Integer1 int整型
* @param Integer2 int整型
* @return int整型
*/
int addTwoInteger(int a, int b) {
return a+b;
}
};
noob63 a 乘 b 问题(函数)
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 计算两个参数的乘积
* @param Number1 int整型
* @param Number2 int整型
* @return long长整型
*/
public long aTimesB (int a, int b) {
// write code here
return 1l*a*b;
}
}
noob64 平方根
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 求非负整数 n 的平方根
* @param n int整型 你需要求 n 的平方根
* @return double浮点型
*/
double findSqrt(int n) {
// write code here
return sqrt(n);
}
};
noob65 一元二次方程
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 判断二元一次方程组是否有解
* @param a int整型 二次项系数
* @param b int整型 一次项系数
* @param c int整型 常数项
* @return bool布尔型
*/
public boolean judgeSolutions (int a, int b, int c) {
// write code here
return b*b-4*a*c>=0;
}
}
noob66 第一宇宙速度
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 计算星球的第一宇宙速度
* @param M double浮点型 星球的质量
* @param r double浮点型 星球的半径
* @return double浮点型
*/
double firstSpeed(double M, double r) {
// write code here
double G=6.67*1e-11;
return sqrt(G*M/r);
}
};
noob67 求阶乘
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 计算 n 的阶乘
* @param n int整型
* @return int整型
*/
int factorialOfN(int n) {
// write code here
long long int ans=1;
long long int p=1e9+7;
for(int i=1;i<=n;i++) ans=ans*i%p;
return ans%p;
}
};
noob68 凯撒解密
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 进行凯撒解密
* @param password string字符串 旺仔哥哥的密码
* @param n int整型 每个字符加密过程中错位的次数
* @return string字符串
*/
string decodeWangzai(string a, int n) {
// write code here
n=n%26;
for(int i=0;i<a.size();i++) a[i]='a'+(a[i]-'a'-n+26)%26;
return a;
}
};
noob69 最厉害的学生
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct node{
string s;
int a,b,c,id;
}ve[N];
bool cmp(node a,node b)
{
if( (a.a+a.b+a.c) == (b.a+b.b+b.c) ) return a.id<b.id;
return a.a+a.b+a.c>b.a+b.b+b.c;
}
int main()
{
int n; cin>>n;
for(int i=0;i<n;i++){
cin>>ve[i].s>>ve[i].a>>ve[i].b>>ve[i].c;
ve[i].id=i;
}
sort(ve,ve+n,cmp);
cout<<ve[0].s<<" "<<ve[0].a<<" "<<ve[0].b<<" "<<ve[0].c<<endl;
return 0;
}
// 64 位输出请用 printf("%lld")
noob70 两点间距离
/**
* struct Point {
* int x;
* int y;
* Point(int xx, int yy) : x(xx), y(yy) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 计算A点与B点之间的距离
* @param point_A Point类 A点
* @param point_B Point类 B点
* @return double浮点型
*/
double calculateDistance(Point point_A, Point point_B) {
// write code here
double x=point_A.x,y=point_A.y;
double xx=point_B.x,yy=point_B.y;
return sqrt((x-xx)*(x-xx)+(y-yy)*(y-yy));
}
};
noob71 学生综合评估系统
#include<bits/stdc++.h>
using namespace std;
// 定义学生结构体
struct Student{
int id;
int academic_score;
int activity_score;
};
// 评估函数:判断学生是否优秀
bool isExcellent(Student student){
// TODO: 实现优秀标准的判断逻辑
int a=student.academic_score;
int b=student.activity_score;
if( ( (a+b)>140) && ( (a*0.7+b*0.3) >= 80 )) return true; //true 代表学生优秀
return false;
}
//主函数用于读入数据调用函数,请勿修改
int main(){
int n;
cin >> n;
Student student;
for(int i=1;i<=n;i++){
cin >> student.id >> student.academic_score >> student.activity_score;
if (isExcellent(student)) cout << "Excellent\n";
else cout << "Not excellent\n";
}
return 0;
}
noob72 点到直线距离
#include <bits/stdc++.h>
using namespace std;
struct point{
double x,y;
point(double A,double B){
x=A,y=B;
}
point() = default;
};
struct line{
point point_A,point_B;
line(point A,point B){
point_A = A,point_B = B;
}
line() = default;
};
double getDistance(point P, line L){
// TODO: 计算点P到直线L的距离
point A=L.point_A;
point B=L.point_B;
double temp1=abs((B.x-A.x)*(P.y-A.y)-(B.y-A.y)*(P.x-A.x));
double temp2=sqrt( (B.x-A.x)*(B.x-A.x) + (B.y-A.y)*(B.y-A.y));
return temp1/temp2;
}
int main(){
int a, b, sx, sy, tx, ty;
cin >> a >> b >> sx >> sy >> tx >> ty;
point A(sx, sy), B(tx, ty), C(a, b);
line L(A, B);
printf("%.2lf", getDistance(C, L));
return 0;
}
#include <bits/stdc++.h>
using namespace std;
struct point{
double x,y;
point(double A,double B){
x=A,y=B;
}
point() = default;
};
struct line{
point point_A,point_B;
line(point A,point B){
point_A = A,point_B = B;
}
line() = default;
};
double getDistance(point P, line L){
// TODO: 计算点P到直线L的距离
point A=L.point_A;
point B=L.point_B;
double temp1=abs((B.y-A.y)*P.x-(B.x-A.x)*P.y+(B.x*A.y-B.y*A.x));
double temp2=sqrt((B.y-A.y)*(B.y-A.y)+(B.x-A.x)*(B.x-A.x));
return temp1/temp2;
}
int main(){
int a, b, sx, sy, tx, ty;
cin >> a >> b >> sx >> sy >> tx >> ty;
point A(sx, sy), B(tx, ty), C(a, b);
line L(A, B);
printf("%.2lf", getDistance(C, L));
return 0;
}
noob73 三角形面积
#include <bits/stdc++.h>
using namespace std;
struct point{
double x,y;
point(double A,double B){
x=A,y=B;
}
point() = default;
};
struct triangle{
point a,b,c;
triangle(point A,point B,point C){
a=A,b=B,c=C;
}
triangle() = default;
};
double getArea(triangle T){
// TODO: 计算三角形T的面积
point a=T.a;
point b=T.b;
point c=T.c;
double ab=sqrt((a.y-b.y)*(a.y-b.y)+(a.x-b.x)*(a.x-b.x));
double ac=sqrt((a.y-c.y)*(a.y-c.y)+(a.x-c.x)*(a.x-c.x));
double bc=sqrt((b.y-c.y)*(b.y-c.y)+(b.x-c.x)*(b.x-c.x));
double p=(ab+ac+bc)/2;
return sqrt(p*(p-ab)*(p-ac)*(p-bc));
}
int main(){
int x, y;
cin >> x >> y;
point a(x, y);
cin >> x >> y;
point b(x, y);
cin >> x >> y;
point c(x, y);
triangle T(a, b, c);
cout << fixed << setprecision(2) << getArea(T) << endl;
return 0;
}
noob74 直线与圆交点间距
#include <bits/stdc++.h>
using namespace std;
struct point{
double x,y;
point(double A,double B){
x=A,y=B;
}
point() = default;
};
struct line{
point point_A,point_B;
line(point A,point B){
point_A = A,point_B = B;
}
line() = default;
};
struct Circle{
point O;
int r;
Circle(point A,int B){
O=A,r=B;
}
Circle() = default;
};
double getDistance(const Circle& circle, const line& l)
{
// 请在这里实现你的代码
point p=circle.O;
double r=circle.r;
point a=l.point_A;
point b=l.point_B;
double A=b.y-a.y;
double B=-(b.x-a.x);
double C=b.x*a.y-b.y*a.x;
double d=abs(A*p.x+B*p.y+C)/sqrt(A*A+B*B);
return 2*sqrt(r*r-d*d);
}
int main() {
double ox, oy, r;
double x1, y1, x2, y2;
cin >> ox >> oy >> r;
cin >> x1 >> y1 >> x2 >> y2;
point center(ox, oy);
Circle circle(center, (int)r);
point p1(x1, y1);
point p2(x2, y2);
line l(p1, p2);
double result = getDistance(circle, l);
cout << fixed << setprecision(6) << result << endl;
return 0;
}
noob75 两直线交点
#include<bits/stdc++.h>
using namespace std;
struct point{
double x,y;
point(double A,double B){
x=A,y=B;
}
point() = default;
};
struct line{
point point_A,point_B;
line(point A,point B){
point_A = A,point_B = B;
}
line() = default;
};
point findMeetingPoint(line line_A,line line_B){
// TODO:求直线 line_A 与 line_B 的交点
point a=line_A.point_A;
point b=line_A.point_B;
point c=line_B.point_A;
point d=line_B.point_B;
double D=(a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x);
if(D==0) return {-1,-1};
else{
double x=(a.x*b.y-a.y*b.x)*(c.x-d.x)-(a.x-b.x)*(c.x*d.y-c.y*d.x);
x/=D;
double y=(a.x*b.y-a.y*b.x)*(c.y-d.y)-(a.y-b.y)*(c.x*d.y-c.y*d.x);
y/=D;
return {x,y};
}
}
int main(){
point A,B,C,D;
cin>>A.x>>A.y>>B.x>>B.y>>C.x>>C.y>>D.x>>D.y;
line AB=line(A,B);
line CD=line(C,D);
point ans = findMeetingPoint(AB,CD);
printf("%.6lf %.6lf",ans.x,ans.y);
return 0;
}
noob76 【模板】序列操作
#include<bits/stdc++.h>
using namespace std;
vector<int>ve;
int main() {
int n; cin>>n;
while(n--)
{
int op; cin>>op;
if(op==1){
int x; cin>>x;
ve.push_back(x);
}else if(op==2){
ve.pop_back();
}else if(op==3)
{
int x; cin>>x;
cout<<ve[x]<<endl;
}else if(op==4){
int id,x; cin>>id>>x;
ve.insert(ve.begin()+id+1,x);
}else if(op==5){
sort(ve.begin(),ve.end());
}else if(op==6){
sort(ve.begin(),ve.end());
reverse(ve.begin(),ve.end());
}
else if(op==7) cout<<ve.size()<<endl;
else{
for(int i=0;i<ve.size();i++) cout<<ve[i]<<" ";
cout<<endl;
}
}
}
// 64 位输出请用 printf("%lld")
noob77 求峰谷点数
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 求序列a中的峰、谷点的个数
* @param a int整型vector 序列a
* @return int整型
*/
int countPeakPoint(vector<int>& a) {
// write code here
int n=a.size()-1,cnt=0;
for(int i=0;i<=n;i++)
{
if(i-1>=0&&i+1<=n)
{
if(a[i]>a[i-1] && a[i]>a[i+1]){
cnt++;
}
if(a[i]<a[i+1]&&a[i]<a[i-1]) cnt++;
}
}
return cnt;
}
};
noob78 向量点乘
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 计算两个三维向量的点乘结果
* @param vector1 int整型vector 第一个向量
* @param vector2 int整型vector 第二个向量
* @return int整型
*/
int dotTime(vector<int>& vector1, vector<int>& vector2) {
int sum=0;
for(int i=0;i<vector1.size();i++) sum+=vector1[i]*vector2[i];
return sum;
}
};
noob79 向量叉乘
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 计算出这两个向量叉乘的结果
* @param vector1 int整型vector
* @param vector2 int整型vector
* @return int整型vector
*/
vector<int> crossTimes(const vector<int>& a, const vector<int>& b) {
return {
a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0]
};
}
};
noob80 旺仔哥哥挤地铁
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 计算旺仔哥哥在地铁上的最长停留时间
* @param t int整型vector 序列 t,表示地铁在相邻两站之间的用时
* @param s int整型vector 序列 s,表示地铁在每一站的停靠时间
* @param x int整型 旺仔哥哥想从第 x 站出发
* @param y int整型 旺仔哥哥想坐到第 y 站
* @return int整型
*/
int countLongestSubwayTime(vector<int>& t, vector<int>& s, int x, int y) {
int sum=0;
for(int i=x-1;i<=y-1;i++){
sum+=s[i];
}
for(int i=x-1;i<y-1;i++) sum+=t[i];
return sum;
// write code here
}
};
noob81 逗号整合器
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 整理出一个将序列中的数字以逗号隔开从而得到的字符串
* @param a int整型vector 需要整理的序列 a
* @return string字符串
*/
string commaTransformer(vector<int>& a) {
// write code here
string ans;
for(int i=0;i<a.size();i++) ans=ans+to_string(a[i])+",";
ans.erase(ans.end()-1,ans.end());
return ans;
}
};
noob82 旺仔哥哥转圈圈
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 计算出旺仔哥哥最后会站在哪位小朋友旁边
* @param a int整型vector 第 i 个小朋友的数字是 a_i
* @param m int整型 表示旺仔哥哥的移动次数
* @return int整型
*/
int stopAtWho(vector<int>& a, int m) {
// write code here
int ans=0,n=a.size();
for(int i=0;i<a.size();i++) a[i]=a[i]%n;
while(m--)
{
//cout<<ans<<endl;
ans=(ans-a[ans]+n)%n;
}
return ans+1;
}
};
noob83 【模板】栈的操作
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int n; cin>>n;
stack<int>st;
while(n--)
{
string op; cin>>op;
if(op=="push"){
int x; cin>>x;
st.push(x);
}else if(op=="pop"){
if(st.size()) st.pop();
else puts("Empty");
}else if(op=="query"){
if(st.size()) cout<<st.top()<<endl;
else puts("Empty");
}else cout<<st.size()<<endl;
}
return 0;
}
noob84 括号配对问题
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
string s; cin>>s;
stack<char>st;
for(int i=0;i<s.size();i++)
{
if(s[i]=='(' || s[i]==')' || s[i]=='[' || s[i]==']')
{
if(s[i]=='(') st.push(s[i]);
else if(s[i]==')' && st.size() && st.top() =='(' ) st.pop();
else if(s[i]=='[') st.push(s[i]);
else if(s[i]==']'&& st.size() && st.top()=='[') st.pop();
else st.push(s[i]);
}
}
if(st.size()) cout<<"false";
else cout<<"true";
return 0;
}
noob85 好串
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
string s; cin>>s;
int a=0,b=0,flag=1;
for(int i=0;i<s.size();i++)
{
if(s[i]=='a') a++;
else if(s[i]=='b') b++;
else flag=0;
if(b>a) flag=0;
}
if(a!=b || a==0 || b==0) flag=0;
if(flag) puts("Good");
else puts("Bad");
return 0;
}
noob86 吐泡泡
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int t; cin>>t;
while(t--)
{
string s; cin>>s;
stack<char>st;
for(int i=0;i<s.size();i++)
{
if(st.size())
{
if(st.top()=='O' && s[i]=='O') st.pop();
else if(st.top()=='o' && s[i]=='o')
{
st.pop(); st.push('O');
while(st.size()>=2)
{
char s1=st.top(); st.pop();
char s2=st.top(); st.pop();
if(s1==s2 && s1=='O'){
;
}else {
st.push(s2);
st.push(s1);
break;
}
}
}else st.push(s[i]);
}else st.push(s[i]);
}
string ans;
while(st.size()) ans=st.top()+ans,st.pop();
cout<<ans<<endl;
}
return 0;
}
noob87 有效括号序列
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return bool布尔型
*/
bool isValid(string s) {
// write code here
stack<char>st;
for(int i=0;i<s.size();i++)
{
if(st.size()){
if(st.top()=='(' && s[i]==')') st.pop();
else if(st.top()=='[' && s[i]==']') st.pop();
else if(st.top()=='{' && s[i]=='}') st.pop();
else st.push(s[i]);
}else st.push(s[i]);
}
return st.size()==0;
}
};
noob88 牛牛与后缀表达式
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 给定一个后缀表达式,返回它的结果
* @param str string字符串
* @return long长整型
*/
long long legalExp(string s) {
// write code here
stack<long long int>st;
long long int ans=0;
for(int i=0;i<s.size();i++)
{
if(s[i]>='0' && s[i]<='9')
{
ans=ans*10+s[i]-'0';
if(s[i+1]=='#') st.push(ans),ans=0;
}
else if(s[i]=='+' || s[i]=='-' || s[i]=='*')
{
long long int temp1=st.top(); st.pop();
long long int temp2=st.top(); st.pop();
if(s[i]=='+') st.push(temp1+temp2);
if(s[i]=='-') st.push(temp2-temp1);
if(s[i]=='*') st.push(temp1*temp2);
}
}
return st.top();
}
};
noob89 表达式求值
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
class Solution {
public:
int solve(string s) {
return calculate_stack(s);
}
private:
LL f(LL a,LL b,char op)
{
if(op=='+') return a+b;
if(op=='-') return a-b;
if(op=='*') return a*b;
return 0ll;
}
int calculate_stack(const string &s) {
stack<LL>nums;
stack<char>ops;
unordered_map<char,int>mp; mp['-']=1,mp['+']=1,mp['*']=2;
int n=s.size();
/*
for(int i=0;i<n;)
{
if(s[i]==' '){
i++; continue;
}
if(isdigit(s[i]))
{
LL ans=0;
while( i<n && isdigit(s[i]) )
{
ans=ans*10+s[i]-'0';
i++;
}
nums.push(ans);
continue;
}
if(s[i]=='(') ops.push(s[i]);
else if(s[i]==')')
{
while(ops.size() && ops.top()!='('){
LL a=nums.top(); nums.pop();
LL b=nums.top(); nums.pop();
nums.push(f(a,b,ops.top()));
ops.pop();
}
if( !ops.empty() ) ops.pop();
}else
{
while(!ops.empty() && ops.top()!='(' && mp[ops.top()] >= mp[s[i]] ){
LL a=nums.top(); nums.pop();
LL b=nums.top(); nums.pop();
nums.push(f(a,b,ops.top()));
ops.pop();
}
ops.push(s[i]);
}
i++;
}*/
for (int i = 0; i < n; )
{
char ch = s[i];
if (ch == ' ') {
i++;
continue;
}
if (isdigit(ch)) {
int num = 0;
while (i < n && isdigit(s[i])) {
num = num * 10 + (s[i]-'0');
i++;
}
nums.push(num);
continue;
}
if (ch == '(') {
ops.push(ch);
} else if (ch == ')') {
while (!ops.empty() && ops.top() != '(') {
int b = nums.top(); nums.pop();
int a = nums.top(); nums.pop();
nums.push(f(a,b,ops.top()));
ops.pop();
}
if (!ops.empty()) ops.pop(); // 弹出'('
} else { // 运算符
while (!ops.empty() && ops.top() != '(' && mp[ops.top()] >= mp[ch]) {
int b = nums.top(); nums.pop();
int a = nums.top(); nums.pop();
nums.push(f(a,b,ops.top()));
ops.pop();
}
ops.push(ch);
}
i++;
}
while(!ops.empty())
{
LL a=nums.top(); nums.pop();
LL b=nums.top(); nums.pop();
nums.push(f(b,a,ops.top()));
ops.pop();
}
return nums.top();
}
};
noob90 栈和排序
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N],b[N],n;
int main(void)
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=n;i>=1;i--) b[i]=max(b[i+1],a[i]);
stack<int>st; st.push(a[1]);
for(int i=2;i<=n;i++){
if(st.top()>b[i]) cout<<st.top()<<" ",st.pop();
st.push(a[i]);
}
while(st.size()) cout<<st.top()<<" ",st.pop();
return 0;
}
noob91 自动管理停车场桩位系统
v1
class Solution {
public:
stack<int>st;
map<int,int>mp;
void push(int value)
{
st.push(value);
mp[value]++;
}
void pop() {
mp[st.top()]--;
st.pop();
}
int top()
{
return st.top();
}
int min()
{
for(auto i=mp.begin();i!=mp.end();i++)
{
if(i->second>0)
return i->first;
}
return 0;
}
};
#include <stack>
using namespace std;
class Solution {
public:
stack<int> st; // 主栈:停车场车辆
stack<int> minSt; // 辅助栈:记录最小值
void push(int value) {
st.push(value);
if (minSt.empty() || value <= minSt.top()) {
minSt.push(value);
}
}
void pop() {
if (st.empty()) return;
int x = st.top();
st.pop();
if (!minSt.empty() && x == minSt.top()) {
minSt.pop();
}
}
int top() {
return st.empty() ? -1 : st.top();
}
int min() {
return minSt.empty() ? -1 : minSt.top();
}
};
noob92 验证栈序列
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N],b[N],n,t;
int main(void)
{
cin>>t;
while(t--)
{
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++) cin>>b[i];
stack<int>st;
int id=0;
for(int i=0;i<n;i++){
st.push(a[i]);
while(st.size() && b[id]==st.top()) id++,st.pop();
}
if(id==n) cout<<"Yes"<<'\n';
else cout<<"No"<<'\n';
}
return 0;
}
noob93 【模板】队列操作
#include<bits/stdc++.h>
#include <ostream>
using namespace std;
queue<int>q;
int main(void)
{
int t; cin>>t;
while(t--){
int op; cin>>op;
if(op==1){
int x; cin>>x;
q.push(x);
}else if(op==2){
if(q.size()) q.pop();
else cout<<"ERR_CANNOT_POP"<<'\n';
}else if(op==3){
if(q.size()) cout<<q.front()<<endl;
else cout<<"ERR_CANNOT_QUERY"<<endl;
}else cout<<q.size()<<endl;
}
return 0;
}
noob94 无法吃午餐的学生数量
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param students int整型vector
* @param sandwiches int整型vector
* @return int整型
*/
int countStudents(vector<int>& students, vector<int>& sandwiches) {
// write code here
int n=students.size();
queue<int>q1,q2;
for(int i=0;i<n;i++){
q1.push(students[i]);
q2.push(sandwiches[i]);
}
int ans=0;
for(int i=0;i<5*n;i++){
if(q1.size()==0) return 0;
if(q1.front()==q2.front()) q1.pop(),q2.pop(),ans++;
else q1.push(q1.front()),q1.pop();
}
return n-ans;
}
};
noob95 队列消数
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param tickets int整型vector
* @param k int整型
* @return int整型
*/
int timeRequiredToBuy(vector<int>& tickets, int k) {
// write code here
queue<int>q;
int n=tickets.size();
for(int i=0;i<n;i++) q.push(i);
int ans=0;
while(1)
{
ans++;
if(q.front()==k && tickets[q.front()]==1 ) return ans;
if(tickets[q.front()]==1) q.pop();
else q.push(q.front()),tickets[q.front()]--,q.pop();
}
return ans;
}
};
noob96 用两个栈实现队列
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
if(stack2.empty())
{
while(stack1.size())
{
stack2.push(stack1.top());
stack1.pop();
}
}
int temp=stack2.top(); stack2.pop();
return temp;
}
private:
stack<int> stack1;
stack<int> stack2;
};
noob97 参议院投票
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 求出最终获胜帮派的名称
* @param s string字符串
* @return string字符串
*/
string predictVictory(string s) {
// write code here
queue<int>d,r;
int n=s.size();
for(int i=0;i<s.size();i++){
if(s[i]=='D') d.push(i);
else r.push(i);
}
while(d.size() && r.size()){
int D=d.front(); d.pop();
int R=r.front(); r.pop();
if(D<R) d.push(D+n);
else r.push(R+n);
}
if(d.size()) return "Dark";
else return "Red";
}
};
noob98 机器翻译
#include<bits/stdc++.h>
using namespace std;
int n,m,flag,ans;
map<int,int>mp;
queue<int>q;
int main(void)
{
cin>>n>>m;
for(int i=0;i<m;i++)
{
int x; cin>>x;
if(mp[x]==1) continue;
else
{
if(flag==n)
{
mp[q.front()]--,q.pop();
q.push(x),mp[x]++;
}else{
flag++;
mp[x]++,q.push(x);
}
ans++;
}
}
cout<<ans;
return 0;
}