PAT乙级部分

1001 害死人不偿命的(3n+1)猜想 (15 分)

#include<stdio.h>

int main()
{
	int n;
	int i=0;
	scanf("%d",&n);
	while(n!=1)
	{
		if(n%2==0)
		{
			n=n/2;
			i++;
		}
		else
		{
			n=(3*n+1)/2;
			i++;
		}
	}
	printf("%d",i);
	return 0;
}

1006 换个格式输出整数 (15 分)

#include<stdio.h>

int main()
{
	int n,one,ten,hunderd;
	scanf("%d",&n);
	one=n%10;
	ten=((n-one)%100)/10;
	hunderd=(n-one-ten*10)/100;
	for(int i=0;i<hunderd;i++)
	{
		printf("B");
	}
	for(int i=0;i<ten;i++)
	{
		printf("S");
	}
	for(int i=1;i<=one;i++)
	{
		printf("%d",i);
	}
	return 0;
}

1011 A+B 和 C (15 分)

#include<stdio.h>

int main()
{
	int num;
	long a,b,c;
	scanf("%d",&num);
	for(int i=1;i<=num;i++)
	{
		scanf("%ld %ld %ld",&a,&b,&c);
		if(a+b>c)
		{
			printf("Case #%d: true\n",i);
		}
		else
		{
			printf("Case #%d: false\n",i);
		}
	}
	return 0;
}

1016 部分A+B (15 分)

参考https://blog.csdn.net/qq_45800977/article/details/106318498

#include<stdio.h>

int main()
{
	long a,b,da,db;
	long pa=0;
	long pb=0;
	int i;
	scanf("%ld %ld %ld %ld",&a,&da,&b,&db);
	i=1;
	if(da==0)
	{
		pa=0;
	}
	else if(a<10)
	{
		pa=a;
	}
	else
	{
		while(a>=i*da)
		{
			if((a-da*i)%(i*10)<i)
			{
				pa=pa*10+da;
			}
			i=i*10;
		}
	}
	i=1;
	if(db==0)
	{
		pb=0;
	}
	else if(b<10)
	{
		pb=b;
	}
	else
	{
		while(b>=i*db)
		{
			if((b-db*i)%(i*10)<i)
			{
				pb=pb*10+db;
			}
			i=i*10;
		}
	}
	printf("%ld",pa+pb);
	return 0;
}

1021 个位数统计 (15 分)

#include<stdio.h>

int main()
{
	int ones[10]={0};
	char num[1000];
    int i=0;
    int n;
	scanf("%s",num);
	while(num[i]!='\0')
	{
        n=num[i]-'0';
        ones[n]=ones[n]+1;
        i++;
	}
	for(int j=0;j<10;j++)
	{
        if(ones[j]!=0)
        {
            printf("%d:%d\n",j,ones[j]);
        }
	}
	return 0;
}

1026 程序运行时间 (15 分)

#include<stdio.h>

int main()
{
	const int CLK_TCK=100;
	long start,end,times,hours,minutes,seconds;
	scanf("%ld %ld",&start,&end);
	times=(end-start)/CLK_TCK;
	if ((end - start) % CLK_TCK >= 50)
	{
		times=times+1;
	}
	seconds=times%60;
	minutes=((times-seconds)/60)%60;
	hours=(times-60*minutes-seconds)/3600;
	printf("%02ld:%02ld:%02ld",hours,minutes,seconds);
	return 0;
}

1031 查验身份证 (15 分)

#include<stdio.h>

int main()
{
	char idcard[18];
	int num;
	int check=0;
	int weight[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
	char check_bit[11]={'1','0','X','9','8','7','6','5','4','3','2'};
	scanf("%d",&num);
	int k=0;
	for(int i=0;i<num;i++)
	{
		scanf("%s",idcard);
		for(int j=0;j<17;j++)
		{
			check=(idcard[j]-'0')*weight[j]+check;
		}
        if(idcard[17]!=check_bit[check%11])
		{
            printf("%s\n",idcard);
            k++;
		}
		check=0;
	}
    if(k==0)
    {
        printf("All passed");
    }
	return 0;
}

1036 跟奥巴马一起编程 (15 分)

#include<stdio.h>

int main()
{
	int row,line;
	char sigh;
	scanf("%d %c",&row,&sigh);
	if(row%2==0)
	{
		line=row/2;
	}
	else
	{
		line=row/2+1;
	}

	for(int i=0;i<line;i++)
	{
		if(i==0||i==line-1)
		{
			for(int j=0;j<row;j++)
			{
				printf("%c",sigh);
			}
		}
		else
		{
			for(int j=0;j<row;j++)
			{
				if(j==0||j==row-1)
				{
					printf("%c",sigh);
				}
				else
				{
					printf(" ");
				}
			}
		}
		printf("\n");
	}
	return 0;
}

1041 考试座位号 (15 分)

#include<stdio.h>

int main()
{
	typedef struct{
		long long id;
		int test_chair;
		int real_chair;
	} students;
	students student[1000];
	int num;
	int search;
	int search_chair;
	int n=0;
	scanf("%d",&num);
	for(int i=0;i<num;i++)
	{
		scanf("%lld %d %d ",&student[i].id,&student[i].test_chair,&student[i].real_chair);
	}
	scanf("%d",&search);
	for(int i=0;i<search;i++)
	{
        scanf("%d",&search_chair);

		for(int j=0;j<num;j++)
		{
			if(student[j].test_chair==search_chair)
			{
				printf("%lld %d\n",student[j].id,student[j].real_chair);
				break;
			}
		}
	}
	return 0;
}

1046 划拳 (15 分)

#include<stdio.h>

int main()
{
	int round,people1_say,people1_do,people2_say,people2_do,number;
	int people1=0;
	int people2=0;
	scanf("%d",&round);
	while(round!=0)
	{	round--;
		scanf("%d %d %d %d",&people1_say,&people1_do,&people2_say,&people2_do);
		number=people1_say+people2_say;
		if(number==people1_do&&number==people2_do)
		{
			continue;
		}
		if(number==people1_do)
		{
			people2++;
		}
		if(number==people2_do)
		{
			people1++;
		}
	}
	printf("%d %d",people1,people2);
	return 0;
}

1056 组合数的和 (15 分)

#include<stdio.h>

int main()
{
	int numbers[10];
	int n;
	int total=0;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&numbers[i]);
	}
	for(int i=0;i<n;i++)
	{
		total=total+numbers[i]*10*(n-1)+numbers[i]*(n-1);
	}
	printf("%d",total);
	return 0;
}

1051 复数乘法 (15 分)

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

int main()
{
	double R1,P1,R2,P2;
	double realpart1,imapart1,realpart2,imapart2,realpart,imapart;
	scanf("%lf %lf %lf %lf",&R1,&P1,&R2,&P2);
	realpart1=R1*cos(P1);
	imapart1=R1*sin(P1);
	realpart2=R2*cos(P2);
	imapart2=R2*sin(P2);
	realpart=realpart1*realpart2-imapart1*imapart2;
	imapart=imapart2*realpart1+imapart1*realpart2;
    if (realpart + 0.005 >= 0 && realpart < 0)
    {
        printf("0.00");
    }
    else
    {
        printf("%.2f", realpart);
    }
    if(imapart >= 0)
    {
        printf("+%.2fi", imapart);
    }
    else if (imapart + 0.005 >= 0 && imapart < 0)
    {
        printf("+0.00i");
    }
    else
    {
        printf("%.2fi", imapart);
    }
	return 0;
}
posted @ 2021-06-01 15:15  启林O_o  阅读(151)  评论(0编辑  收藏  举报