【女生节】圆周率小数点后37373737位中,“37”出现的次数是?

举报
小哈里 发表于 2022/05/10 23:05:39 2022/05/10
【摘要】 problem 每年的3月7日是女生节。那么,圆周率小数点后37373737位中,“37”出现的次数是?*(提示:答案是一个六位数) solution1 最开始的想法当然是上网查洛, 1、能算n位:...

problem

每年的3月7日是女生节。那么,圆周率小数点后37373737位中,“37”出现的次数是?*(提示:答案是一个六位数)

solution1

最开始的想法当然是上网查洛,
1、能算n位:https://www.haomeili.net/YuanZhouLv
2、能算100万位:https://uutool.cn/pi/
3、能算1000万位:http://pai.babihu.com/pi/100.html

solution2

然后当然是写代码算洛,但是我还是小看了位数
这是3000万位,内存直接崩了。

#include<iostream>
#include<string>
using namespace std;

int main(){
	freopen("out2.txt","w",stdout);
	string s, t;
	while(cin>>t){
		s += t;
	}
	cout<<s;
	return 0;
}

/*
int main(){
	string a, b;
	getline(cin,a);
	getline(cin,b);
	if(a==b)cout<<"ok";
	else cout<<"fail";
	return 0;
}
*/

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;

int fun2(const std::string& str, const std::string& sub)
{
	int num = 0;
	size_t len = sub.length();
	if (len == 0)len=1;//应付空子串调用
	for (size_t i=0; (i=str.find(sub,i)) != std::string::npos; num++, i+=len);
	return num;
}
int fun1(const std::string& str, const std::string& sub)
{
	int num = 0;
	for (size_t i = 0; (i = str.find(sub, i)) != std::string::npos; num++, i++);
	return num;
}

const int maxn = 1e8+10;
int b,x,n,c,i,j,d,l,a[maxn];
int main(){
	//freopen("out.txt","w",stdout);
	float s;
	
	//cout<<"Input n:"<<"\n";
	cin>>x;
	for(s=0,n=1;n<=maxn;n++)//累加确定项数.
	{
		s=s+log10((2*n+1)/n);
		if(s>x+1)
		break;
	}
	for(i=0;i<=x+5;i++)a[i]=0;
	for(c=1,j=n;j>=1;j--)//按公式分布计算。
	{
		d=2*j+1;
		for(i=0;i<=x+4;i++)//各位实施除2j+1.
		{
			 a[i]=c/d;
			 c=(c%d)*10+a[i+1];
		}
		a[x+5]=c/d;
		for(b=0,i=x+5;i>=0;i--)//各位实施乘j
		{
			a[i]=a[i]*j+b;
			b=a[i]/10;
			a[i]=a[i]%10;
		}
		a[0]=a[0]+1;
		c=a[0];//整数加1.
	}
	for(b=0,i=x+5;i>=0;i--)//按公式各位乘2
	{
		a[i]=a[i]*2+b;
		b=a[i]/10;
		a[i]=a[i]%10;
	}
	//cout<<endl<<"PI="<<a[0]<<".\n";//诸位输出计算结果。
	int ans = 0;
	string st;
	for(int i = 1; i <= x; i++){
		st += a[i]+'0';
	}
	//cout<<st<<'\n';
	cout<<fun1(st,"37")<<'\n';
	/*
	for(l=10,i=1;i<=x;i++)
	{
		cout<<a[i];
		l++;
		if(l%10==0)cout<<" ";
		if(l%50==0)cout<<endl;
	}
	*/
	cout<<endl;
	return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

solution3

1、下载1亿位圆周率(网盘文本或网站爬虫)
2、截取ctrl+g跳转37373737位,删除后面的
3、用notepad++查找计数得到位数
在这里插入图片描述

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/104732308

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。