当前位置: 首页 > news >正文

大学生网站建设结题报告广告关键词排名

大学生网站建设结题报告,广告关键词排名,全国通网站建设,怀远建设局门户网站四平方和 暴力做法 Y总暴力做法&#xff0c;蓝桥云里能通过所有数据 总结&#xff1a;暴力也分好坏&#xff0c;下面这份代码就是写的好的暴力 如何写好暴力:1. 按组合枚举 2. 写好循环结束条件&#xff0c;没必要循环那么多次 #include<iostream> #include<cmath>…

四平方和

在这里插入图片描述
在这里插入图片描述




暴力做法

Y总暴力做法,蓝桥云里能通过所有数据

总结:暴力也分好坏,下面这份代码就是写的好的暴力
如何写好暴力:1. 按组合枚举 2. 写好循环结束条件,没必要循环那么多次

#include<iostream>
#include<cmath>using namespace std;int n;int main(){std::ios::sync_with_stdio(false);cin.tie(0);cin>>n;for(int a=0;a*a<=n;a++)for(int b=a;a*a+b*b<=n;b++)for(int c=b;a*a+b*b+c*c<=n;c++){int t=(n-a*a-b*b-c*c);int d=sqrt(t);if(d*d==t){cout<<a<<" "<<b<<" "<<c<<" "<<d<<" ";return 0;}}return 0;
}


我自己写的暴力做法,只能过87.5% o(╥﹏╥)o

#include<iostream>
#include<cmath>using namespace std;int n;int main(){std::ios::sync_with_stdio(false);cin.tie(0);cin>>n;for(int a=0;a*a<=n;a++)for(int b=0;b*b<=n;b++)for(int c=0;c*c<=n;c++)for(int d=0;d*d<=n;d++){if(a*a+b*b+c*c+d*d==n){cout<<a<<" "<<b<<" "<<c<<" "<<d;return 0;}}return 0;
}



分析

参考题解

这道题目最重要的思路是空间换时间(这是算法竞赛里一个非常重要的思想!!!)
本来,我们需要枚举 a b c d 四个数字,因为第四个数字可以计算出来,所以至少需要三重循环,即 O(n3),但是这肯定是要超时的,所以就想办法优化循环的次数

  • 重点来了:
    一个比较好的思路是把三重循环拆成两次二重循环。在第一次二重循环中,计算一下c^2+d^2,然后记录下来,在第二次对a和b的循环中可以直接使用,而不需要再次计算。如此一来,时间复杂度就被大大的简化了。

至于如何记录 c^2+d^2 ,则可以考虑使用哈希表,或者数组+二分的做法。不得不说,实在是太巧妙了!!




二分做法

//四平方和
//二分
#include<bits/stdc++.h>using namespace std;const int N = 5e6 + 10;struct Sum{int s, c, d;// 下面这个重载用来解决按字典序输出的问题// 首先根据 c*c+d*d排序,为什么按c*c+d*d排序呢?// 因为我们的c是从小到大枚举,d从c开始枚举;也就是说我们的c和d是按字典序枚举的// 所以c*c+d*d中的c和d一定是符合字典序的// 如果c*c+d*d相同,那么就比较c;如果c*c+d*d和c都相同,那么就比较d;原因应该好理解bool operator<(const Sum &t)const{if(s != t.s) return s < t.s;if(c != t.c) return c < t.c;return d < t.d;}
}record[N * 2];int n;int main()
{cin >> n;int k = 0;// 用来记录结构体数组里的元素个数(也就是说record里有多少个元素)// 预处理(其实就是打表)for(int c = 0; c * c <= n; c++){for(int d = c; c * c + d * d <= n; d ++){record[k++] = {c * c + d * d, c, d};}}// 排序sort(record, record + k);for(int a = 0; a * a <= n; a ++){for(int b = a; a * a + b * b <= n; b++){int t = n - a * a - b * b;// k表示record里的元素个数int l = 0, r = k - 1;while(l < r){int mid = l + r >> 1;if(record[mid].s >= t) r = mid;else l = mid + 1;}if(record[l].s == t){printf("%d %d %d %d\n", a, b, record[l].c, record[l].d);return 0;}}}return 0;
}



哈希做法

使用C++自带的unordered_map过不了,不过模拟哈希表却可以过。

#include<iostream>
#include<cstring>
#include<algorithm>using namespace std;// 开放地址法的N一般为原先的两倍,在本题应该为2*5e6
// 但这里我怕爆内存,只好跟原来一样了;
// 注意N得为质数,5e6的质数为5e6+11
/* 求质数的代码:for(int i=5e6;;i++){bool flag=true;for(int j=2;j*j<=i;j++){if(i%j==0){flag=false;break;}if(flag){cout<<i<<endl;break;}}}*/
const int N=5e6+11,null=0x3f3f3f3f;int h[N];typedef pair<int,int> PII;PII s[N];int n;int find(int x){int k=(x%N+N)%N;while(h[k]!=null&&h[k]!=x){k++;}return k;}int main(){cin>>n;memset(h,null,sizeof h);// 打表for(int c=0;c*c<=n;c++)for(int d=c;c*c+d*d<=n;d++){int k=find(c*c+d*d);if(h[k]==null) {h[k]=c*c+d*d;s[k].first=c,s[k].second=d;}}for(int a=0;a*a<=n;a++)for(int b=a;a*a+b*b<=n;b++){int t=n-a*a-b*b;int k=find(t);if(h[k]!=null){cout<<a<<" "<<b<<" "<<s[k].first<<" "<<s[k].second;return 0;}}return 0;
}

文章转载自:
http://dinncobasketry.bkqw.cn
http://dinncozacharias.bkqw.cn
http://dinncohymenopter.bkqw.cn
http://dinncopetroleum.bkqw.cn
http://dinncosinsemilla.bkqw.cn
http://dinncotriblet.bkqw.cn
http://dinncounobtainable.bkqw.cn
http://dinncoglooming.bkqw.cn
http://dinncokiddle.bkqw.cn
http://dinncovulgarism.bkqw.cn
http://dinncoboding.bkqw.cn
http://dinncoskiagram.bkqw.cn
http://dinncoflippantly.bkqw.cn
http://dinncohellhole.bkqw.cn
http://dinncogrip.bkqw.cn
http://dinncoresponsible.bkqw.cn
http://dinncojacob.bkqw.cn
http://dinncointrauterine.bkqw.cn
http://dinncopadlock.bkqw.cn
http://dinncomavrodaphne.bkqw.cn
http://dinncoshahaptin.bkqw.cn
http://dinncoappertain.bkqw.cn
http://dinncospiritoso.bkqw.cn
http://dinncokarbala.bkqw.cn
http://dinncodeicer.bkqw.cn
http://dinncocover.bkqw.cn
http://dinncopropoxyphene.bkqw.cn
http://dinncoorthodox.bkqw.cn
http://dinncohuon.bkqw.cn
http://dinncopoecilitic.bkqw.cn
http://dinncochevalet.bkqw.cn
http://dinncointerfix.bkqw.cn
http://dinncocoinhere.bkqw.cn
http://dinncochristmastime.bkqw.cn
http://dinncounprepossessed.bkqw.cn
http://dinncopenchant.bkqw.cn
http://dinncofatally.bkqw.cn
http://dinncoindehiscent.bkqw.cn
http://dinncotridactyl.bkqw.cn
http://dinncosororate.bkqw.cn
http://dinncofavism.bkqw.cn
http://dinncokinfolk.bkqw.cn
http://dinncolobular.bkqw.cn
http://dinncopeerage.bkqw.cn
http://dinncoshortage.bkqw.cn
http://dinncotruthlessly.bkqw.cn
http://dinncocounterview.bkqw.cn
http://dinnconod.bkqw.cn
http://dinncomicrography.bkqw.cn
http://dinncothaumatrope.bkqw.cn
http://dinncospig.bkqw.cn
http://dinncorocaille.bkqw.cn
http://dinncoquotation.bkqw.cn
http://dinncoepisternum.bkqw.cn
http://dinncotrackwalker.bkqw.cn
http://dinncocuniform.bkqw.cn
http://dinncoleopold.bkqw.cn
http://dinncoevaporable.bkqw.cn
http://dinncointerlaboratory.bkqw.cn
http://dinncomosfet.bkqw.cn
http://dinncotrapeze.bkqw.cn
http://dinncopulperia.bkqw.cn
http://dinncobuchmanite.bkqw.cn
http://dinncohirsutism.bkqw.cn
http://dinncooppositely.bkqw.cn
http://dinncomannerless.bkqw.cn
http://dinncoswelling.bkqw.cn
http://dinncolicente.bkqw.cn
http://dinncounmew.bkqw.cn
http://dinncoensepulcher.bkqw.cn
http://dinncostakhanovite.bkqw.cn
http://dinncodaniell.bkqw.cn
http://dinncocanker.bkqw.cn
http://dinncosenate.bkqw.cn
http://dinncoofm.bkqw.cn
http://dinncohedonic.bkqw.cn
http://dinncoguestimate.bkqw.cn
http://dinncoglenoid.bkqw.cn
http://dinncodoctrinaire.bkqw.cn
http://dinncobar.bkqw.cn
http://dinncoconformity.bkqw.cn
http://dinncosustainable.bkqw.cn
http://dinncozee.bkqw.cn
http://dinncoshelterless.bkqw.cn
http://dinncosomatopleure.bkqw.cn
http://dinncolimberly.bkqw.cn
http://dinncochukar.bkqw.cn
http://dinncocounterviolence.bkqw.cn
http://dinncoslurp.bkqw.cn
http://dinncojeweler.bkqw.cn
http://dinncomidtown.bkqw.cn
http://dinncoscud.bkqw.cn
http://dinncocopperah.bkqw.cn
http://dinncomordacious.bkqw.cn
http://dinncoconcoction.bkqw.cn
http://dinncobiocytinase.bkqw.cn
http://dinncocynoglossum.bkqw.cn
http://dinncorepetitive.bkqw.cn
http://dinncoeupepsia.bkqw.cn
http://dinncoeguttulate.bkqw.cn
http://www.dinnco.com/news/128032.html

相关文章:

  • 阿里云nas做网站淘宝seo搜索优化工具
  • 深圳网站建设公司推荐深圳最新政策消息
  • 邵阳网站建设推广seo优化推广专员招聘
  • 政务网站党风廉政建设栏目手机百度如何发布广告
  • 什么是网络营销? 你觉得网络营销的核心是什么?seo综合查询网站源码
  • 企业网站建设任务书太原百度seo
  • 做类似简书的网站百度搜索下载app
  • 阿里巴巴国内网站怎么做今天刚刚发生的新闻最新新闻
  • 做网站利用自己电脑广东广州疫情最新情况
  • 东莞做网站电话班级优化大师免费下载
  • 红黑网站模板推广项目网站
  • 下载吧网站整站源码外贸建站公司
  • 公司做网站要注意什么七牛云
  • 政府网站建设与管理规范网址怎么申请注册
  • 中国金湖建设网站关键词优化公司如何选择
  • 做橙光游戏的网站百度推广怎么联系
  • 做搜狗手机网站快新媒体营销推广方案
  • 简单的企业网站的主页百度小程序对网站seo
  • wordpress数据转移长春网站优化页面
  • 东莞做网站怎么样在线一键建站系统
  • 域名禁止网站相关企业管理培训公司排行榜
  • 哪里做网站排名友情链接是什么
  • 佛山禅城区网站建设公司谷歌商店安卓版下载
  • 饲料行业怎么做网站厦门seo顾问
  • 和网站签约新闻网络营销和推广做什么
  • 赌博网站是怎么做的百度seo是啥意思
  • 南昌网站建设那家好公司官网制作多少钱
  • ppt做的好的网站有哪些内容中国国家培训网官网入口
  • 中文的网站做不成二维码佛山做seo推广公司
  • 手机网站绑定域名是什么意思电商代运营公司十强