
Lv.1
签名:清理黑历史
在 计数排序(c++教学) 中回复
#include<iostream>
using namespace std;
const int MAXN=10001;
int a[MAXN],n;
int f[MAXN];
void counting(){
for(int i=0;i<n;i++){
f[a[i]]++;
}
for(int i=0;i<MAXN;i++)
for(int j=0;j<f[i];j++)
cout<<i;
}
int main(){
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
counting();
return 0;
}2023-08-01T11:37:29 点赞:0
在 计数排序(c++教学) 中回复
回顾:
插入、选择排序:https://shequ.codemao.cn/community/538893
冒泡排序:https://shequ.codemao.cn/community/538831
整数的表示:https://shequ.codemao.cn/community/537861
数据的结构与算法概念、算法复杂度:https://shequ.codemao.cn/community/537721
中缀转后缀思路:https://shequ.codemao.cn/community/537172
后缀表达式+后缀表达式求值思路:https://shequ.codemao.cn/community/537153
2023-08-02T20:38:40 点赞:0
在 求最大公约数C算法实现 中回复
其实求最大公因数的算法还可以再优化一下
辗转相除法缺点就是取模运算耗时长
可以试试更相减损法与移位结合
时间复杂度为O(log(max(a,b)))
比辗转相除法快,究其原因是:避免了取模运算
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
if(a==b)return a;
if((a&1)==0&&(b&1)==0){
return gcd(a>>1,b>>1)<<1;
}
else if((a&1)==0&&(b&1)!=0){
return gcd(a>>1,b);
}
else if((a&1)!=0&&(b&1)==0){
return gcd(a,b>>1);
}
else{
int b=max(a,b);
int s=min(a,b);
return gcd(b-s,s);
}
}
int main(){
cout<<gcd(74,111)<<endl;
cout<<gcd(114,514)<<endl;
cout<<gcd(314,628)<<endl;
cout<<gcd(81,123)<<endl;
return 0;
}2023-08-05T19:25:03 点赞:0
在 【神岛JS萌新教程】BoxJS 从入门到实战 第十四期——神奇的选择器! 中回复
\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/\可乐/
2023-08-29T09:04:38 点赞:0
在 怎么用c++整人 中回复
#include <iostream>
#include<queue>
#include<vector>
using namespace std;
int a[1005][1005];
int vis[1005][1005];
int dis[1005][1005];///维护到出发点的最短距离
const int MAXN=4;
int fx[MAXN] = {1,-1,0,0};
int fy[MAXN] = {0,0,-1,1};
struct node{
int x, y;
};
int main(){
int n;
cin >> n;
memset(a,-1,sizeof a);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin >> a[i][j];
}
}
///bfs
queue <node> q;
node z;
z.x=1;z.y=1;
q.push(z);///压入起点
vis[1][1] = 1;
while(!q.empty()){
node now = q.front();
q.pop();
///访问我下一步能够到达的结点
for(int i = 0; i < MAXN; i++){
int tox = now.x + fx[i];
int toy = now.y + fy[i];
if(vis[tox][toy] == 0 && a[tox][toy] == 0){
z.x=tox;z.y=toy;
q.push(z);
vis[tox][toy] = 1;
dis[tox][toy] = dis[now.x][now.y] + 1;
}
}
}
cout << dis[n][n]+1;
}//不如写个bfs让他们伤心、难过、自尊心没了2023-08-29T17:33:00 点赞:0