C语言入门练习 - 第二期 判断语句与循环语句(题解)

比赛地址:C语言入门练习 - 第二期 判断语句与循环语句

A、入门——基础语法(for)

#include<stdio.h>
int main(){
	int n,i;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		printf("%d\n",i);
	}
}

B、入门——基础语法(if)

#include<stdio.h>
int main()
{
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	if(a<b&&b<c){
		printf("%d %d %d",a,b,c);
	}
	if(a<c&&c<b){
		printf("%d %d %d",a,c,b);
	}
	if(b<a&&a<c){
		printf("%d %d %d",b,a,c);
	}
	if(b<c&&c<a){
		printf("%d %d %d",b,c,a);
	}
	if(c<a&&a<b){
		printf("%d %d %d",c,a,b);
	}
	if(c<b&&b<a){
		printf("%d %d %d",c,b,a);
	}
	return 0;
}

C、入门——判断奇偶数

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	int t,n,b;
	char a[100];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",a);
		n=strlen(a);
		b=(a[n-1]-48)%2;
		if(b==0)
			printf("even\n");
		else
			printf("odd\n");
	}
	return 0;
}

D、入门——质数判断

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

int isprime(int n){
	if(n < 2)
		return 0;
	for(int i = 2;i <= sqrt(n);i++){
		if(n % i == 0) return 0;
	}
	return 1;
}
int main()
{
	int n;
	while(scanf("%d", &n) != EOF){
		if(isprime(n)){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}

E、倍数

#include<stdio.h>

int main ()
{
    int a,b;
    scanf("%d%d", &a, &b);

    if (a%b==0||b%a==0)
    puts("Sao Multiplos");
    else
    puts("Nao sao Multiplos");
    return 0;
}

G、加薪

#include <stdio.h>

int main()
{
    double salary;
    scanf("%lf", &salary);

    double x;

    if (salary <= 400) x = 0.15;
    else if (salary <= 800) x = 0.12;
    else if (salary <= 1200) x = 0.1;
    else if (salary <= 2000) x = 0.07;
    else x = 0.04;

    printf("Novo salario: %.2lf\n", salary * (1 + x));
    printf("Reajuste ganho: %.2lf\n", salary * x);
    printf("Em percentual: %.0lf %%", x * 100);

    return 0;
}

H、三角形类型

#include<stdio.h>

int max(int x, int y)
{
  	return x > y ? x : y;
}

int min(int x, int y)
{
	return x < y ? x : y;
}

int main()
{
    double a,b,c;
    scanf("%lf%lf%lf", &a, &b, &c);
    double x,y,z;
    x=max(a,max(b,c));
    y=min(a,min(b,c));
    z=(a+b+c)-(x+y);
    //printf("%lf %lf %lf\n",x,y,z);
    if(x>=y+z)
    {
        printf("NAO FORMA TRIANGULO\n");
        return 0;
    }
    if(x*x==y*y+z*z)
        printf("TRIANGULO RETANGULO\n");
    if(x*x>y*y+z*z)
        printf("TRIANGULO OBTUSANGULO\n");
    if(x*x<y*y+z*z)
        printf("TRIANGULO ACUTANGULO\n");
    if(x==y&&y==z)
        printf("TRIANGULO EQUILATERO\n");
    if(x==y&&x!=z||x==z&&z!=y||z==y&&z!=x)
        printf("TRIANGULO ISOSCELES\n");
    return 0;
}

I、实验

#include<stdio.h>


int main()
{
    int n;
    scanf("%d", &n);

    int c = 0, r = 0, f = 0;
    for (int i = 0; i < n; i ++ )
    {
        int k;
        char t;
        scanf("%d %c", &k, &t);  // scanf在读入字符时,不会自动过滤空格、回车、tab
        if (t == 'C') c += k;
        else if (t == 'R') r += k;
        else f += k;
    }

    int s = c + r + f;
    printf("Total: %d animals\n", s);
    printf("Total coneys: %d\n", c);
    printf("Total rats: %d\n", r);
    printf("Total frogs: %d\n", f);
    printf("Percentage of coneys: %.2lf %%\n", (double)c / s * 100);
    printf("Percentage of rats: %.2lf %%\n", (double)r / s * 100);
    printf("Percentage of frogs: %.2lf %%\n", (double)f / s * 100);

    return 0;
}

J、连续整数相加

#include<stdio.h>
int x,y,n;
int main(){
    while(~scanf("%d",&n)){
        if(n<=0) continue;
        if(!x) x=n;
        else{
            y=n;
            break;
        }
    }
    printf("%d",(2*x+y-1)*y/2);
}
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页