PTA循环

目录

1.小于m的最大的10个素数

2.爬动的蠕虫

3.百钱买百鸡

4.情侣身高差

5.统计素数并求和

6.找出最小值

7.求阶乘序列前N项和


1.小于m的最大的10个素数

#include<stdio.h>
#include<math.h>
int main()
{
  int n,i,j,count=0;
  scanf("%d",&n);
  for(i=n-1;i>=2;i--)
  {
    for(j=2;j<=sqrt(i);j++)
    {
      if(i%j==0) break;
    }
    if(j>sqrt(i)&&count<10)
    {
      count++;
      printf("%6d",i);
    }
  }
}
#include<stdio.h>
#include<math.h>
int main()
{
  int n,i,j,count=0;
  scanf("%d",&n);
  for(i=n-1;i>=2;i--)
  {
    for(j=2;j<=sqrt(i);j++)
    {
      if(i%j==0) break;
    }
    if(j>sqrt(i)&&count<10)
    {
      count++;
      printf("%6d",i);
    }
  }
}

2.爬动的蠕虫

#include <stdio.h>
int main()
{
	int N,U,D,L;
	scanf("%d %d %d",&N,&U,&D);
	int time;
	time=L=0;
	while(L<N){
 
		
		time++;
		L=L+U;
		if(L>=N)
			break;
		L=L-D;
		time++;
	}
	printf("%d",time);
	return 0;
} 

3.百钱买百鸡

#include <stdio.h>
#define TRUE 1
#define FALSE 0
 
int main(void)
{
	int x, y, z, n, NoAnswer;
 
	scanf("%d", &n);
	NoAnswer = TRUE;
	for(x = 0; x <= n/5; x++)  //穷举公鸡个数
		for(y = 0; y <= n/3; y++) //穷举母鸡个数
		{
			z = n - x - y;  //其余是鸡仔
			if(15 * x + 9 * y + z == n *3 )  //注意避免浮点运算
			{
				printf("%4d%4d%4d\n", x, y, z);
				NoAnswer = FALSE;
			}
		}
	if(NoAnswer == TRUE)
		printf("No Answer\n");
	return 0;
}

4.情侣身高差

解析:%c:空格 和 转义字符(比如'\n') 都作为有效字符,都会被c接收。有两种方法避免让c接收换行符

1、scanf("%c\n", &c);  '\n' 表示忽略所有的空白字符(包括回车,空格,tab )。所以想要结束输入,输入任意一个非空白字符即可,但是该字符仍然会留在缓冲区中。

2、getchar(); 来接收换行符
 

#include <stdio.h>
int main () 
{
	int n;
	char c;
	float h;
	//方法一 
	//scanf("%d\n", &n); 	//'\n'表示忽略所有的空白字符,输入非空白字符结束输入 ,不加'\n'会使c接收换行符 
	//方法二 
	scanf("%d", &n);
	getchar(); //接收换行符 
	while ( n-- ) 
    {
		//scanf("%c %f\n", &c, &h);
		scanf("%c %f", &c, &h);
		getchar();
		if ( c == 'F' ) 
			printf("%.2f\n", h * 1.09);
		else
			printf("%.2f\n", h / 1.09);
	} 
 	return 0;
}

5.统计素数并求和

#include <stdio.h> 
int main()
{
	int count = 0, sum = 0;
	int m, n;
	int i, j;
	scanf("%d %d", &m, &n);
	for (i = m; i <= n; i++) 
	{
		for (j = 2; j <= i; j++) 
		{
			if (i % j == 0) break; 
        }
		if (j == i)  
		{
			sum += i;
			count += 1;
		}
	}
	printf("%d %d", count, sum);
	return 0;
}

6.找出最小值

#include <stdio.h>  
#include <math.h>
int main(){  
	int n,min,num;
	int i;
	int cnt=1;
	scanf("%d",&n);
	for(i=1;i<=n;i++,cnt++)
    {
		
		scanf("%d",&num);
		if(cnt==1)
        {
			min=num;
		}
		if(num<min){
			min=num;
		}
	}
	printf("min = %d",min);
    return 0;  
 } 

7.求阶乘序列前N项和

#include<stdio.h>
double fact(int n);
 
int main()
{
  int  n;
  scanf("%d",&n);
  double sum=0;
  
  for(int i=1;i<=n;i++)
	sum+=fact(i);
	
  printf("%.0lf",sum);
  
  return 0;
}
 
double fact(int n)
{
  double result=1;
  for(int i=1;i<=n;i++)
    result*=i;
  return result;
}
 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值