U190849 最简分式

#include<bits/stdc++.h>
using namespace std; // gcd(a,b) = gcd(b, a%b): a,b的最大公约数 = b,a%b 的最大公约数
int gcd(int a,int b) {
// return b ? gcd(b,a%b) : a;
if(b==0) return a;
return gcd(b,a%b);
}
int main() {
int t,a,b,c,d; cin>>t;
while(t--) {
cin>>a>>b>>c>>d;
int e = a*d+b*c;
int f = b*d;
int temp = gcd(e,f);
cout<<e/temp<<" "<<f/temp<<endl;
}
return 0;
}

P5734 【深基6.例6】文字处理软件

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+10,INF=0x3f3f3f3f;
// s.append(str); 在 s 后追加一个 str
// s.substr(a,b); 截取 s 的第 a 个字符后连续 b 个字符
// s.insert(a, str); 在 s 的第 a 个字符后插入字符串 str
// s.find(str) 查询 str 第一次出现的下标,如果没有出现则返回 -1
int main(){
string s,str; int t,op,a,b; cin>>t>>s;
while(t--){
cin>>op;
if(op==1){
cin>>str; s.append(str);
cout<<s<<endl;
}else if(op==2){
cin>>a>>b; s=s.substr(a,b);
cout<<s<<endl;
}else if(op==3){
cin>>a>>str; s.insert(a, str);
cout<<s<<endl;
}else if(op==4){
cin>>str;
cout<<(int)s.find(str)<<endl;
}
}
return 0;
}

P1104 生日

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=110,INF=0x3f3f3f3f;
int n;
struct T {
string name;
int y,m,d,id;
}t[N]; bool cmp(T a, T b){
if(a.y!=b.y) return a.y < b.y;
if(a.m!=b.m) return a.m < b.m;
if(a.d!=b.d) return a.d < b.d;
return a.id > b.id;
} int main(){
cin>>n;
for(int i=1; i<=n; i++){
cin>>t[i].name>>t[i].y>>t[i].m>>t[i].d;
t[i].id = i;
}
sort(t+1, t+n+1, cmp);// sort(首地址,末地址后一位,比较方式);
for(int i=1; i<=n; i++){
cout<<t[i].name<<endl;
}
return 0;
}

P4305 [JLOI2011]不重复数字

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+10,INF=0x3f3f3f3f; int main() {
int t,x,n; cin>>t;
while(t--) {
set<int> s; cin>>n;
while(n--) {
cin>>x;
if(s.count(x)==0) cout<<x<<" ";
s.insert(x);
}
cout<<endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+10,INF=0x3f3f3f3f; int main() {
int t,x,n; scanf("%d", &t);
while(t--) {
map<int,int> mp; scanf("%d", &n);
while(n--) {
scanf("%d", &x);
if(mp.count(x)==0) printf("%d ", x);
mp[x] = 1; // x ---> 1
}
printf("\n");
}
return 0;
}

P8218 【深进1.例1】求区间和

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10, INF=0x3f3f3f3f;
int n,q,l,r,a[N],s[N]; int main() {
// 笔记:前缀和 s[i] = a[1]+a[2] +... +a[i]
// s[i] = s[i-1] + a[i]
// 区间和 [l,r] = s[r] - s[l-1]
cin>>n;
for(int i=1; i<=n; i++) cin>>a[i];
for(int i=1; i<=n; i++) s[i]=s[i-1]+a[i];
cin>>q;
while(q--) {
cin>>l>>r;
cout<<s[r] - s[l-1]<<endl;
}
return 0;
}

P3397 地毯

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e3+10,INF=0x3f3f3f3f;
int n,m,v[N][N]; int main(){
int x1,x2,y1,y2;
cin>>n>>m;
for(int i=1; i<=m; i++){
cin>>x1>>y1>>x2>>y2;
for(int x=x1; x<=x2; x++){
for(int y=y1; y<=y2; y++){
v[x][y] ++;
}
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
cout<<v[i][j]<<" ";
} cout<<endl;
}
return 0;
}

P2367 语文成绩

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=5e6+10,INF=0x3f3f3f3f;
int n,p,a[N],b[N];
int main(){
cin>>n>>p; int l,r,d;
for(int i=1; i<=n; i++) cin>>a[i];
while(p--){
cin>>l>>r>>d;
b[l] += d;
b[r+1] -= d;
}
int tmin=2e9;
for(int i=1; i<=n; i++){
b[i] += b[i-1];
if(a[i]+b[i] < tmin) tmin=a[i]+b[i];
}
cout<<tmin;
return 0;
}

CF276C Little Girl and Maximum Sum

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+10,INF=0x3f3f3f3f;
int n,q,a[N],b[N], l,r; int main(){
cin>>n>>q;
for(int i=1; i<=n; i++) cin>>a[i];
while(q--){
cin>>l>>r;
b[l] ++;
b[r+1] --;
}
for(int i=1; i<=n; i++) b[i]+=b[i-1];
sort(a+1, a+1+n);
sort(b+1, b+1+n);
LL s=0; // 2e5 * 2e5
for(int i=1; i<=n; i++) s += 1ll*a[i]*b[i];
cout<<s;
return 0;
}

P1451 求细胞数量

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=110,INF=0x3f3f3f3f;
char s[N][N];
int n,m,ans=0; bool check(int x,int y) {
return x>=0 && x<n &&y>=0 && y<m &&s[x][y]!='0';
}
void dfs(int x,int y) {
s[x][y]='0';
if(check(x+1,y)) dfs(x+1, y);
if(check(x-1,y)) dfs(x-1, y);
if(check(x,y-1)) dfs(x, y-1);
if(check(x,y+1)) dfs(x, y+1);
}
int main() {
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]!='0') {
ans++, dfs(i,j);
}
}
}
cout<<ans;
return 0;
}

P1706 全排列问题

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+10,INF=0x3f3f3f3f;
int n,vis[N],ans[N];
// 坐第 m 个板凳
void dfs(int m) {
if(m > n) {
for(int i=1; i<=n; i++)
cout<<setw(5)<<ans[i];
cout<<endl; return;
}
for(int i=1; i<=n; i++) { // 第 i 个人
if(vis[i]==0) {
vis[i]=1, ans[m]=i; // 第 m 个板凳坐的是 i.
dfs(m+1);
vis[i]=0; // 回溯
}
}
}
int main() {
cin>>n; dfs(1);
return 0;
}

最新文章

  1. Android基础总结(四)
  2. 使用 Redis 实现分布式锁
  3. Oracle SQL函数
  4. java:使用匿名类直接new接口
  5. EventHandlerList z
  6. mongodb基础系列——数据库查询数据返回前台JSP(一)
  7. mac 下安装oh my zsh
  8. ubuntu 下重装mysql若干问题
  9. 在服务器上用Fiddler抓取HTTPS流量
  10. 浅谈SSH框架
  11. 双系统恢复CentOS的MBR
  12. XT535
  13. POJ 3687 Labeling Balls (top 排序)
  14. Synchronized 有几种用法?
  15. MySQLdump之single-transaction详解
  16. Spring AOP 入门实例详解
  17. Android 百度云推送
  18. python解析json数据
  19. EZ 2018 04 21 NOIP2018 模拟赛(九)
  20. 转载《浅析MVC框架中View层的优雅设计及实例》

热门文章

  1. 【进阶篇】Redis实战之Jedis使用技巧详解
  2. Django简介以及基本使用
  3. [常用工具] 基于psutil和GPUtil获取系统状态信息
  4. ArcGIS工具 - 统计要素数量
  5. TCP\ip 地址总结
  6. pytorch 配置详细过程
  7. Google Cloud Platform | 使用 Terraform 的分层防火墙策略自动化
  8. Vue12 监视属性
  9. Docker中apt-get update失败解决方案
  10. imax6开发版_挂载NFS文件系统