0
点赞
收藏
分享

微信扫一扫

【入门3】循环结构 (今天刷洛谷了嘛)

大柚子top 2022-02-16 阅读 71

P5718

【深基4.例2】找最小值 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
int main()
{
	ios;
	int s[109];
	int n;
	cin>>n;
	for(int i = 0;i<n;i++)
	cin>>s[i];
	cout<<*min_element(s,s+n)<<"\n";
}

P5719

【深基4.例3】分类平均 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
int main()
{
	ios;
	int n,k;
	cin>>n>>k;
	int a = 0,b = 0;
	int na = 0,nb = 0;
	for(int i = 1;i<=n;i++)
	{
		if(i%k==0)
		{
			a+=i;
			na++;
		}
		else
		{
			b+=i;
			nb++;
		}
	}
	printf("%.1lf %.1lf\n",1.0*a/na,1.0*b/nb);
}

P5720

【深基4.例4】一尺之棰 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
int main()
{
	ios;
	int n;
	cin>>n;
	int cnt = 0;
	while(n)
	{
		n>>=1;
		cnt++;
	}
	cout<<cnt<<"\n";
}

P5721

【深基4.例6】数字直角三角形 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
int main()
{
	ios;
	int n;
	cin>>n;
	int q = 1;
	for(int i = n;i>=1;i--)
	{
		for(int j = 1;j<=i;j++)
		{
			if(q<10)
			cout<<0<<q;
			else
			cout<<q;
			q++;
		}
		cout<<"\n";
	}
}

P1009

[NOIP1998 普及组] 阶乘之和 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long
#define maxn 2000
using namespace std;
int main() 
{
	int fact[maxn + 2];
	int add[maxn + 2];
	int n;
	memset(fact, 0, sizeof(fact));
	memset(add, 0, sizeof(add));
	scanf("%d", &n);
	fact[0] = 1;
	int product=1,sum,carrymul=0,carryplu=0;
	int j;
	int l=1;
	for (int i = 1; i <= n; i++) 
	{
		for (j = 0; j < maxn; j++)
		 {
			product = fact[j] * i+carrymul;
			fact[j] = product % 10;
			carrymul = product / 10;
			sum = add[j] + fact[j] + carryplu;
			add[j] = sum % 10;
			carryplu = sum / 10;
			if (fact[j + 1] == 0 && carrymul == 0
				&& add[j + 1] == 0 && carryplu == 0
				&& j + 1 >= l) 
				{
					break;
				}
		}
		l = j;
	}
	int frontzero = 1;
	for (int i = maxn - 1; i >= 0; i--) 
	{
		if (add[i] != 0)
	    {
			frontzero = 0;
		}
		if (!frontzero) 
		{
			printf("%d", add[i]);
		}
	}
}

P1980

[NOIP2013 普及组] 计数问题 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
int main()
{
	ios;
	int n,x;
	cin>>n>>x;
	int ans = 0;
	for(int i = 1;i<=n;i++)
	{
		int k = i;
		while(k)
		{
			if(k%10==x)
			ans++;
			k/=10;
		}
	}
	cout<<ans<<"\n";
}

P1035

[NOIP2002 普及组] 级数求和 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
int main()
{
	ios;
	double k;
	cin>>k;
	double ans =  1;
	int q = 2;
	while(1)
	{
		ans+=1.0/q;
		if(ans>k)
		break;
		q++;
	}
	cout<<q<<"\n";
}

P2669

[NOIP2015 普及组] 金币 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
int main()
{
	ios;
	int n;
	cin>>n;
	int k = 1;
	int d = 1;
	int ans = 0;
	while(n)
	{
		if(n>=d)
		{
			ans+=d*k;
			n-=d;
			d++;
			k++;
		}
		else
		{
			ans+=n*k;
			break;
		}
	} 
	cout<<ans<<"\n";
}

P5722

【深基4.例11】数列求和 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
int main()
{
	ios;
	int n;
	cin>>n;
	int ans = 0;
	for(int i = 1;i<=n;i++)
	{
		ans+=i;
	}
	cout<<ans<<"\n";
}

P5723

【深基4.例13】质数口袋 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
int main()
{
	ios;
	int n;
	cin>>n;
	int ans = 0;
	int cnt = 0;
	for(int i = 2;;i++)
	{
		
		bool flag = 1;
		for(int j = 2;j*j<=i;j++)
		{
			if(i%j==0)
			{
				flag = 0;
				break;
			}
		}
		if(flag)
		{
			if(ans+i>n)
			break;
			ans+=i;cnt++;
			cout<<i<<"\n";
		}
	}	
	cout<<cnt<<"\n";
}

P1217

[USACO1.5]回文质数 Prime Palindromes 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 9999899;
bool prm[N];
int prime[N];
void pme()
{
	prm[1] = 1;
	prm[0] = 1;
	ll num = 0;
	for(int i = 2;i<=N;i++)
	{
		if(!prm[i]) prime[++num] = i;
		for(int j = 1;j<=num;j++)
		{
			if(i * prime[j] > N) break;
			prm[prime[j]*i] = 1;
			if(i%prime[j]==0)
			break;
		}
	}
}
bool func(int x)
{

    int y=x,num=0;//int y=x,防止x被改变
    while (y!=0)
    {
        num=num*10+y%10;//上一次数字的记录进位再加上下一位数
        y/=10;
    } 
    if (num==x) return 1;
    else return 0;
}
int main()
{
	ios;
	int l,r;
	scanf("%d %d",&l,&r);
	pme();
	for(int i=l;i<=r;i++)
    {
    	if(i>9989899)
    	break;
    	if(i&1)
        if(func(i)&&!prm[i]) printf("%d\n",i);
    }
}

P1423

小玉在游泳 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
int main()
{
	double n;
	cin>>n;
	double q = 2;
	int cnt = 0;
	while(n>0)
	{
		n-=q;
		q*=0.98;
		cnt++;
	}
	cout<<cnt<<"\n";
}

P1307

[NOIP2011 普及组] 数字反转 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
char s[100];
int main()
{
	cin>>s;
	int n = strlen(s);
	if(s[0]=='0'&&n==1)
	cout<<0<<"\n";
	if(s[0]=='-')
	{
		reverse(s,s+n);
		cout<<'-';
		for(int i = 0;i<n-1;i++)
		{
			if(i==0&&s[i]=='0')
			while(s[i]=='0')
			i++;
			cout<<s[i];
		}
	}
	else
	{
		reverse(s,s+n);
		for(int i = 0;i<n;i++)
		{
			if(i==0&&s[i]=='0')
			while(s[i]=='0')
			i++;
			cout<<s[i];
		}
	}
}

P1720

月落乌啼算钱(斐波那契数列) 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
int main()
{
	const double pp = sqrt(5);
	int n;
	cin>>n;
	double ans = (pow((1+pp)/2,n)-pow((1-pp)/2,n))/pp;
	printf("%.2lf\n",ans);
}

P5724

【深基4.习5】求极差 / 最大跨度值 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e6;
int main()
{
	ios;
	int s[109];
	int n;
	cin>>n;
	for(int i = 0;i<n;i++)
	cin>>s[i];
	sort(s,s+n);
	cout<<s[n-1]-s[0]<<"\n"; 
}

P1420

最长连号 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e4+9;
int s[N];
int main()
{
	ios;
	int n;
	cin>>n;
	for(int i = 1;i<=n;i++)
	cin>>s[i];
	int cnt = 1;
	int ans = 1;
	for(int i = 2;i<=n;i++)
	{
		if(s[i]-1==s[i-1])
		cnt++;
		else
		cnt = 1;
		ans = max(ans,cnt);
	}
	cout<<ans<<"\n";
}

P1075

[NOIP2012 普及组] 质因数分解 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e8+1;
bool su[N];
int prime[N];
int num = 0;
void pm()
{
	su[1] = 1;
	for(int i = 2;i<=N;i++)
	{
		if(!su[i]) prime[++num] = i;
		for(int j = 1;j<=num;j++)
		{
			if(i*prime[j]>N) break;
			su[i*prime[j]] = 1;
			if(i%prime[j]==0) break;
		}
	}
}
int main()
{
	ios;
	int n;
	cin>>n;
	int ans = 1;
	pm();
	for(int i = 1;i<=num;i++)
	{
		if(n%prime[i]==0)
		{
			ans = prime[i];break;
		}
		
	}
	cout<<n/ans<<"\n";
}

P5725

【深基4.习8】求三角形 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e8+1;
int main()
{
	ios;
	int n;
	cin>>n;
	int q = 1;
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=n;j++)
		{
			if(q<10)
			cout<<0<<q;
			else
			cout<<q;
			q++;
		}
		cout<<"\n";
	}
	cout<<"\n";
	q = 1;
	for(int i = 1;i<=n;i++)
	{
		for(int j = n*2-i*2;j>=1;j--)
		cout<<' ';
		for(int k = 1;k<=i;k++)
		{
			if(q<10)
			cout<<0<<q;
			else
			cout<<q;
			q++;
		}
		cout<<"\n";
	}
}

P5726

【深基4.习9】打分 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e8+1;
int main()
{
	ios;
	int n;
	cin>>n;
	int s[1009];
	for(int i = 1;i<=n;i++)
	cin>>s[i];
	sort(s+1,s+1+n);
	int ans = 0;
	for(int i = 2;i<n;i++)
	ans+=s[i];
	printf("%.2lf\n",1.0*ans/(n-2));
}

P4956

[COCI2017-2018#6] Davor 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e8+1;
int main()
{
	ios;
	int n;
	cin>>n;
	int x = 0,k = 0;
	while(n)
	{
		n-=21*52;
		k++;
		if(n%(52*7)==0)
		{
			x = n/(52*7);
			if(x<=100)
			break;
		}
	}
	cout<<x<<"\n"<<k<<"\n";
}

P1089

[NOIP2004 提高组] 津津的储蓄计划 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath> 
#include<vector>
#include <iomanip>
using namespace std;
typedef long long  ll;
const int inf = 0x3f3f3f3f;
#define ios ios::sync_with_stdio(false);cin.tie(0), cout.tie(0)
const int N = 1e8+1;
int main()
{
	ios;
	int flag = 0;
	int cq = 0;
	int sy = 0;
	for(int i = 1;i<=12;i++)
	{
		int n;
		cin>>n;
		n-=sy;
		if(n>300&&!flag)
		{
			flag = i;
		 } 
		sy = 300-n;
		cq += sy/100*100;
		sy = sy%100;
	}
	if(flag)
	{
		cout<<'-'<<flag<<"\n";
	}
	else
	cout<<cq*1.2+sy<<"\n";
}

想要加入挑战或代码交流,请私信作者(看到必回复!!!)

滑稽保命(:

举报

相关推荐

0 条评论