[关闭]
@Moritz 2019-03-26T04:21:10.000000Z 字数 2957 阅读 400

第九届蓝桥杯C/C++A组省赛

蓝桥杯 C++ 算法 竞赛 所有文稿


4.第几个幸运数

到x星球旅行的游客都被发给一个整数,作为游客编号。
x星的国王有个怪癖,他只喜欢数字3,5和7。
国王规定,游客的编号如果只含有因子:3,5,7,就可以获得一份奖品。

我们来看前10个幸运数字是:
3 5 7 9 15 21 25 27 35 45
因而第11个幸运数字是:49

小明领到了一个幸运数字 59084709587505,他去领奖的时候,人家要求他准确地说出这是第几个幸运数字,否则领不到奖品。

请你帮小明计算一下,59084709587505是第几个幸运数字。

需要提交的是一个整数,请不要填写任何多余内容。

  1. #include <iostream>
  2. #include <queue>
  3. #include <set>
  4. using namespace std;
  5. int main(){
  6. int yin[3]= {3,5,7};
  7. long long t=59084709587505,n=0;
  8. priority_queue<long long ,vector<long long >,greater<long long >> pq;
  9. set<long long > num;
  10. for(int i=0;i<3;i++){
  11. num.insert(yin[i]);
  12. pq.push(yin[i]);
  13. }
  14. while(pq.top()<=t) {
  15. long long x=pq.top();
  16. cout<<n<<" "<<x<<endl;
  17. pq.pop();
  18. n++;
  19. for(int i=0; i<3; i++){
  20. if (!num.count(x*yin[i])){
  21. pq.push(x*yin[i]);
  22. num.insert(x*yin[i]);
  23. }
  24. }
  25. }
  26. cout<<n<<endl;
  27. return 0;
  28. }

注意点:


全球变暖

两次dfs

  1. /*16'34-17:02*/
  2. #include <iostream>
  3. #include <cmath>
  4. #include <string.h>
  5. using namespace std;
  6. const int maxn=1010;
  7. char island[maxn][maxn];
  8. bool ma[maxn][maxn],m2[maxn][maxn]={false};
  9. int n;
  10. void dfs(int y,int x){
  11. for(int c=-1;c<=1;c++){
  12. for(int r=-1;r<=1;r++){
  13. if (r*c==0&&y+r>=0&&y+r<n&&x+c>=0&&x+c<n){
  14. if (island[y+r][x+c]=='#'&&!ma[y+r][x+c]){
  15. ma[y+r][x+c]=true;
  16. dfs(y+r,x+c);
  17. }
  18. }
  19. }
  20. }
  21. return;
  22. }
  23. int main(){
  24. cin>>n;
  25. memset(ma,false,sizeof(ma));
  26. for(int i=0;i<n;i++){
  27. for(int j=0;j<n;j++){
  28. cin>>island[i][j];
  29. }
  30. }
  31. int cnt=0;
  32. for(int i=0;i<n;i++){
  33. for(int j=0;j<n;j++){
  34. if (island[i][j]=='#'&&!ma[i][j]){
  35. cnt++;
  36. ma[i][j]=true;
  37. dfs(i,j);
  38. }
  39. }
  40. }
  41. for(int i=0;i<n;i++){
  42. for(int j=0;j<n;j++){
  43. if (!ma[i][j]){
  44. for(int c=-1;c<=1;c++){
  45. for(int r=-1;r<=1;r++){
  46. if (r*c==0&&i+r>=0&&i+r<n&&j+c>=0&&j+c<n){
  47. island[i+r][j+c]='.';
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. cnt=0;
  55. memset(ma,false,sizeof(ma));
  56. for(int i=0;i<n;i++){
  57. for(int j=0;j<n;j++){
  58. if (island[i][j]=='#'&&!ma[i][j]){
  59. cnt++;
  60. ma[i][j]=true;
  61. dfs(i,j);
  62. }
  63. }
  64. }
  65. cout<<cnt;
  66. return 0;
  67. }

10.付账问题

  1. /*16:13 start*/
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. const int maxn=50000+10;
  8. int a[maxn];
  9. double b[maxn];
  10. double bzc(int n){
  11. double tot_b=0.0,s0=0.0;
  12. for(int i=1;i<=n;i++){
  13. tot_b+=b[i];
  14. }
  15. tot_b=tot_b/(n*1.0);
  16. cout<<endl<<"tot_b="<<tot_b<<endl;
  17. for(int i=1;i<=n;i++){
  18. s0=s0+(b[i]-tot_b)*1.0*(b[i]-tot_b);
  19. cout<<"i="<<i<<" "<<(b[i]-tot_b)<<endl;
  20. }
  21. cout<<"s0="<<s0<<endl;
  22. return sqrt(s0/(1.0*n));
  23. }
  24. int main(){
  25. int n;
  26. double s;
  27. cin>>n>>s;
  28. for(int i=1;i<=n;i++) cin>>a[i];
  29. sort(a,a+n);
  30. double aver=s*1.0/n;
  31. int i=1;
  32. while(s>0){
  33. if (a[i]<aver) b[i]=a[i]*1.0;
  34. else b[i]=aver;
  35. s-=b[i];
  36. if (n>i){
  37. aver=s*1.0/(n-i);
  38. i++;}
  39. }
  40. for(int i=1;i<=n;i++) cout<<a[i]<<" ";
  41. cout<<endl;
  42. for(int i=1;i<=n;i++) cout<<b[i]<<" ";
  43. cout<<endl<<bzc(n)<<endl;
  44. double tot_b=0.0,s0=0.0;
  45. for(int i=1;i<=n;i++){
  46. tot_b+=b[i];
  47. }
  48. tot_b=tot_b/(n*1.0);
  49. cout<<endl<<"tot_b="<<tot_b<<endl;
  50. for(int i=1;i<=n;i++){
  51. s0=s0+(b[i]-tot_b)*1.0*(b[i]-tot_b);
  52. cout<<"i="<<i<<" "<<(b[i]-tot_b)<<endl;
  53. }
  54. cout<<"s0="<<sqrt(s0/(1.0*n))<<endl;
  55. system("pause");
  56. return 0;
  57. }

出现了很神奇的输出结果

  1. 5 2333
  2. 666 666 666 666 666
  3. 666 666 666 666 666
  4. 466.6 466.6 466.6 466.6 466.6
  5. tot_b=466.6
  6. i=1 0
  7. i=2 0
  8. i=3 5.68434e-014
  9. i=4 0
  10. i=5 0
  11. s0=3.23117e-027
  12. 2.54211e-014
  13. tot_b=466.6
  14. i=1 0
  15. i=2 0
  16. i=3 5.68434e-014
  17. i=4 0
  18. i=5 0
  19. s0=2.54211e-014
  20. 请按任意键继续. . .

i=5时的5.68434e-014是怎么来的???

  1. printf( "%.4lf", sqrt(r/n));

2019.3.23

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注