Open judge1.5答案

本文提供了一系列使用C语言解决实际问题的编程案例,包括求平均数、最高分统计、银行利息计算等,通过这些实例帮助读者理解和掌握C语言的基本语法及应用。

01:求平均年龄

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int n,count=0,ages=0,middle=0;
	float average;
	scanf("%d",&n);
	while(count<n)
	{
		scanf("%d",&middle);
		ages+=middle;
		count++;
	}
	average=ages*1.0/n;
	printf("%.2f",average);
	return 0;
	
}

02:财务管理

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	float money=0,average;
	int count=1;
	while(count<=12)
	{
		scanf("%f",&average);
		money+=average;
		count++;
	}
	average=money/12;
	printf("$%.2f",average);
	
}

03:均值

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	float num=0,middle,average;
	int count=1,n;
	scanf("%d",&n);
	while(count<=n)
	{
		scanf("%f",&middle);
		num+=middle;
		count++;
	}
	average=num/n;
	printf("%.4f",average);
	
}

04:求整数的和与均值

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int sum=0,count=1,n,middle;
	//float average;
	scanf("%d\n",&n);
	while(count<=n)
	{
		scanf("%d",&middle);
		sum+=middle;
		count++;
	}
	printf("%d %.5f",sum,(sum*1.0)/n);
	
	return 0;
}

05:最高的分数

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int count=1,middle,max=0,n;
	scanf("%d",&n);
	while(count<=n)
	{
		scanf("%d",&middle);
		if(middle>=max)
		{
			max=middle;	
		}
		count++;
	}
	printf("%d",max);
	return 0;
}

06:整数序列的元素最大跨度值

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int count=1,m,max=0,min=1000,middle;
	scanf("%d",&m);
    while(count<=m)
	{
		scanf("%d",&middle);
		if(max<=middle)
		{
			max=middle;
		}
		if(min>=middle)
		{
			min=middle;
		}
		count++;
	}
	printf("%d",max-min);
	return 0;
}

07:奥运奖牌计数

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int a=0,b=0,c=0,n,count=1;
	int d,e,f;
	scanf("%d",&n);
	while(count<=n)
	{
		scanf("%d %d %d",&d,&e,&f);
		a+=d;
		b+=e;
		c+=f;
		count++;
	}
	d=a+b+c;
	printf("%d %d %d %d",a,b,c,d);
	return 0;
}

08:多边形内角和

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int n,count,angle,middle,sum=0;
	scanf("%d",&n);
	for(count=1;count<n;count++)
	{
		scanf("%d",&middle);
		sum+=middle;
	}
	angle=(n-2)*180-sum;
	printf("%d",angle);
	return 0;
}

09:奇数求和

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int m,n,sum=0;
	scanf("%d %d",&m,&n);
	if(m%2==1)
	{
		if(n%2==1)
		{
			while(m<=n)
			{
				sum+=m;
				m=m+2;
			}
		}
		else
		{
		
			while(m<n)
			{
				sum+=m;
				m=m+2;
			}
		}
	}
	else
	{
		m++;
	    if(n%2==1)
		{
			while(m<=n)
			{
				sum+=m;
				m=m+2;
			}
		}
		else
		{
	
			while(m<n)
			{
				sum+=m;
				m=m+2;
			}
		}
	}
	printf("%d",sum);
	return 0;
}

10:满足条件的数累加

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int m,n,sum=0,a;
	scanf("%d %d",&m,&n);
	if(m%17==0)
	{
		if(n%17==0)
		{
			while(m<=n)
			{
				sum+=m;
				m=m+17;
			}
		}
		else
		{
			while(m<n)
			{
				sum+=m;
				m+=17;
			}
		}
	}
	else
	{
		a=m%17;
		m=m-a;
		m=m+17;
		if(n%17==0)
		{
			while(m<=n)
			{
				sum=m+sum;
				m=m+17;
			}
		}
		else
		{
			while(m<n)
			{
				sum+=m;
				m+=17;
			}
		}
	}
	printf("%d",sum); 
	return 0;
}

11:整数的个数

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int k,middle,count=0,c1=0,c2=0,c3=0,m;
	scanf("%d",&k);
	while(count<k)
	{
		scanf("%d",&middle);
		count++;
		if(middle==1)
		{
			c1++;
		}
		else if(middle==5)
		{
			c2++;
		}
		else if(middle==10)
		{
			c3++;
		}
		else
		m++;
	}
	printf("%d\n%d\n%d",c1,c2,c3);
	return 0;
}

12:与指定数字相同的数的个数

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int n,middle,sum=0,count=0,m;
	scanf("%d %d",&n,&m);
	while(count<n)
	{
		scanf("%d",&middle);
		if(m==middle)
		{
			sum++;
		}
		count++;
	}
	printf("%d",sum);
	return 0;
}

13:乘方计算

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	long a,n,count,mult=1;
	scanf("%ld %ld",&a,&n);
	for(count=1;count<=n;count++)
	{
		mult*=a;
	}
	printf("%d",mult);
	return 0;
}

14:人口增长问题

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int x,n,count=0;
	double answer;
	scanf("%d %d",&x,&n);
	answer=x*1.0;
	while(count<n)
	{
		answer*=(1+0.001);
		count++;
	}
	printf("%.4f",answer);
	return 0;
}

15:银行利息

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	long R,M,Y,count=1;
	double m;
	scanf("%ld %ld %ld",&R,&M,&Y);
	m=M*1.0;
	while(count<=Y)
	{
		m=m*(R*0.01+1);
		count++;
	}
	R=(int)m;
	printf("%d",R);
	return 0;
}

16:买房子

17:菲波那契数列

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int n;
	scanf("%d",&n);
	int a[n],i;
	a[0]=1;
	a[1]=1;
	if(n<=2)
	{
		printf("1");
	}
	else
	{
		i=2;
		while(i<n)
		{
		a[i]=a[i-1]+a[i-2];
		i++;
		}
		printf("%d",a[i-1]);
	}

	return 0;
}

18:鸡尾酒疗法

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int a,b,n,i=0,m[n-1][2];
	double x,y[n-1];
	scanf("%d",&n);
	scanf("%d %d",&a,&b);
	x=(b*1.0)/a;
	while(i<n-1)
	{  
		scanf("%d %d",&m[i][0],&m[i][1]);
		y[i]=(m[i][1]*1.0)/m[i][0];
		i++;
	}
	for(i=0;i<n-1;i++)
	{
		if(y[i]-x>0.05)
		{
			printf("better\n");
		}
		else if(x-y[i]>0.05)
		{
			printf("worse\n");
		}
		else
		{
			printf("same\n");
		}
	}
	return 0;
}

19:救援

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int n,i=0;
	double location[n][3],time=0;
	scanf("%d",&n);
	while(i<n)
	{
		scanf("%lf %lf %lf",&location[i][0],&location[i][1],&location[i][2]);
		time+=2*(sqrt(pow(location[i][0],2)+pow(location[i][1],2)))/50+location[i][2]*1.5;
		i++;
	}
	printf("%d",(int)ceil(time));
	return 0;
}

20:球弹跳高度的计算

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	int h,count=1,i=1;
	double sum=0,medium[10];
	scanf("%d",&h);
	medium[0]=h*1.0;
	sum=h*1.0;
	while(count<10)
	{
		medium[i]=medium[i-1]/2;
		sum+=medium[i]*2;
		i++;
		count++;
	}
	printf("%g\n%g",sum,medium[9]/2);
	return 0;
}

21:角谷猜想

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
{
	long n;
	scanf("%ld",&n);
	while(n!=1)
	{
		if(n%2==0)
		{
			n/=2;
			printf("%ld/2=%ld\n",n*2,n);
		}
		else
		{
			n=n*3+1;
			printf("%ld*3+1=%ld\n",(n-1)/3,n);
		}
	}
	printf("End");
	return 0;
}

22:津津的储蓄计划

#include <cmath>
#include <iostream>
#include <iomanip>
#include <cstdio>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv)
{
	int expect,sum=0,m=0,i=1;
	while(i<=12)
	{
		cin>>expect;
		sum=sum+300;
		if((sum-expect)<0)
		{
			printf("-%d",i);
			break;
		}
		if((sum-expect)/100>0)
		{
			m=m+((sum-expect)/100*100);
			sum=sum-((sum-expect)/100*100)-expect;
		}
		else
		{
			sum=sum-expect;
		}
		i++;
	}
	if(i==13)
	{
		//cout<<m<<endl;
		i=(m*1.2)+sum;
		cout<<i;
	}
	return 0;
}

23:药房管理

#include <cmath>
#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv)
{
	int m,n,count=0,i=0,medium;
	cin>>m;
	cin>>n;
	while(i<n)
	{
		cin>>medium;
		if(m>=medium)
		{
			m=m-medium;
		}
		else
		{
			count++;
		}
		i++;
	}
	cout<<count;
	return 0;
}

24:正常血压

25:求特殊自然数

#include <cmath>
#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char** argv)
{
	int i=1;
	for(i=1;i<=1000;i++)
	{
		if((i%9==i/7/7%7)&&(i/9%9==i/7%7)&&(i/9/9%9==i%7))
		{
			printf("%d\n",i);
			printf("%d%d%d\n",i/7/7%7,i/7%7,i%7);
			printf("%d%d%d",i/9/9%9,i/9%9,i%9);
		}
	}
	return 0;
}

26:统计满足条件的4位数个数

#include <cmath>
#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

bool oPer(int medium);

int main(int argc, char** argv)
{
	int i,n,count=0,medium;
	bool q;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>medium;
		q=oPer(medium);
		if(q==true)
		{
			count++;
		}
	}
	cout<<count;
	return 0;
}

bool oPer(int medium)
{
	bool mm=false;
	int a,b,c,d;
	a=medium/1000;
	b=medium/100-a*10;
	c=medium/10-a*100-b*10;
	d=medium-a*1000-b*100-c*10;
	if((d-a-b-c)>0)
	{
		mm=true;
	}
	return mm;
}

27:级数求和

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

int oPer(int k);

int main()
{
	int n,i,k;
	cin>>k;
	n=oPer(k);
	cout<<n;
	return 0;
}

int oPer(int k)
{
	double Sn=0;
	int i;
	for(i=1;Sn<k&&Sn<=15;i++)
	{
		Sn=Sn+1.0/i;
	}
	return i-1;
}

28:分离整数的各个数位

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

int main()
{
	long n;
	int a;
	cin>>n;
	while(n>=10)
	{
		a=n%10;
		cout<<a<<' ';
		n=n/10;
	}
	cout<<n;
	return 0;
}

29:数字反转

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

long oPer(long n);
int Length(int n);

int main()
{
	long num,medium;
	int i;
	cin>>num;
	while((num%10==0)&&(num!=0))
	{
		num=num/10;
	}
	if(num>0)
	{
		medium=oPer(num);
		cout<<medium;
	}
	else if(num<0)
	{
		medium=oPer(-num);
		cout<<-medium;
	}
	else
	{
		cout<<0;
	}
	return 0;
}

long oPer(long n)
{
	int med,length,i;
	long temp=0;
	length=Length(n);
	for(i=length;i>0;i--)
	{
		med=n%10;
		n/=10;
		temp=temp+med*pow(10,i-1);
	}
	return temp;
}

int Length(int k)
{
	int sum=0;
	while(k!=0)
	{
		k/=10;
		sum++;
	}
	return sum;
}

30:含k个3的数

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

long oPer(long n);
int Length(int n);

int main()
{
	long m;
	int k,count=0,num;
	cin>>m>>k;
	if(m%19==0)
	{
		count++;
	}
	if((num=oPer(m))==k)
	{
		count++;
	}
	if(count==2)
	{
		cout<<"YES";
	}
	else
	{
		cout<<"NO";
	}
	return 0;
}

long oPer(long n)
{
	int length,a,b=0,i;
	length=Length(n);
	for(i=length;i>0;i--)
	{
		a=n%10;
		if(a==3)
		{
			b++;
		}
		n/=10;
	}
	return b;
}

int Length(int k)
{
	int sum=0;
	while(k!=0)
	{
		k/=10;
		sum++;
	}
	return sum;

31:开关灯

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

void oPer(int * ptr1,int n,int i);

void Num(int *ptr2,int n);

int main()
{
	int n,M,i,num;
	cin>>n>>M;//n为灯,M为人
	int a[n]={0};//0为开灯
	for(i=0;i<M;i++)//i是第i个人,这个for是遍历M个人
	{
		oPer(a,n,i);
	}
	Num(a,n);
	return 0;
}

void oPer(int * ptr1,int n,int i)
{
	int j;//j是第j盏灯
	for(j=0;j<n;j++)//这里的for是遍历n盏灯
	{
		if(((j+1)%(i+1)==0)&&j>=i)
		{
			if(*ptr1==1)
			{
				*ptr1=0;
			}
			else
			{
				*ptr1=1;
			}
		}
		ptr1++;
	}
}

void Num(int * ptr2,int n)
{
	int k;
	for(k=0;k<n;k++)
	{
		if(*ptr2==1)
		{
			if(k==0)
			{
				cout<<k+1;
			}
			else
			cout<<','<<k+1;
		}
		ptr2++;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值