PAT : 中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题答案(自测-1 ~ 自测-5)(纯C编写)

题目地址

自测-1

#include
int main(int argc,char** argv)
{
	int sum;
	char ch;
	scanf("%d %c",&sum,&ch);
	int i=1,msum=1,fk=sum-msum;
	while(fk>=(i+2)*2)
	{
		i+=2;
		msum+=2*i;
		fk=sum-msum;
	}
	int ins=i;
	while(ins>1)
	{
		int ind=ins,inf=(i-ins)/2;
		while(inf--)
			putchar(' ');
		while(ind--)
			putchar(ch);
		putchar('\n');
		ins-=2;
	}
	int inf=(i-1)/2;
	while(inf--)
		putchar(' ');
	putchar(ch);
	putchar('\n');
	ins=3;
	while(ins<=i)
	{
		int ind=ins,inf=(i-ins)/2;
		while(inf--)
			putchar(' ');
		while(ind--)
			putchar(ch);
		putchar('\n');
		ins+=2;
	}
	printf("%d\n",fk);
	return 0;
}

>>先计算出最大可以容下多少个符号和最大值时第一行符号个数,后循环输出空格和符号即可。
自测-2

#include
#include
int book[100005];
int isprime(int m)
{
	int i;
	if(m<2)
		return 0;
	for(i=2;i<=sqrt(m);i++)
		if(!(m%i))
			return 0;
	return 1;
}
int main(int argc,char** argv)
{
	int N;
	scanf("%d",&N);
	int i,sum=0;
	for(i=2;i<=N;i++)
		book[i]=isprime(i);
	for(i=4;i<=N;i++)
		if(book[i]&&book[i]-book[i-2]==0)
			sum++;
	printf("%d\n",sum);
  	return 0;
}

>>标记2~N所有的素数,检查计数相差为2的素数。
自测-3

#include
int main(int argc,char** argv)
{
	int m,n;
	scanf("%d%d",&m,&n);
	int i,dig[105];
	for(i=0;i

>>每次递增都取余运算,计算初始位置 m-n 时可能为负数,须加 m 直到是正数为止。
自测-4

#include
#include
char str[25];
int dig[10];
int main(int argc,char** argv)
{
	scanf("%s",str);
	int i,check=0;
	for(i=0;i=0;i--)
	{
		int fk=num;
		num=(str[i]-'0')*2;
		str[i]=(num+fk)%10+'0';
		num/=10;
	}
	if(num)
		printf("No\n%d",num);
	else
	{
		for(i=0;i

>>1.数组存储后先逐位记录用到的数字和个数;

    2.每一位乘二后进位;

    3.如果计算后存在进位不为 0 ,直接输出 No 并输出当前进位;

    4.否则检查当前数组是否和最初用到的数字相同、个数一样,一样输出 Yes ,不一样输出 No;

    5.输出当前数组。
自测-5

#include
#include
char str[55][5]={"x","S1","S2","S3","S4","S5","S6","S7","S8","S9","S10","S11","S12","S13",\
"H1","H2","H3","H4","H5","H6","H7","H8","H9","H10","H11","H12","H13",\
"C1","C2","C3","C4","C5","C6","C7","C8","C9","C10","C11","C12","C13",\
"D1","D2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13",\
"J1","J2"};
int dig[55];
int fkkk[55];
int main(int argc,char** argv)
{
	int k;
	scanf("%d",&k);
	int i,j;
	for(i=1;i<=54;i++)
		scanf("%d",&dig[i]);
	for(i=1;i<=54;i++)
	{
		int sp=dig[i];
		for(j=2;j<=k;j++)
			sp=dig[sp];
		fkkk[sp]=i;
	}
	for(i=1;i<=54;i++)
		printf("%s%c",str[fkkk[i]],i==54?'\n':' ');
  	return 0;
}

>>计算洗牌 k 次后的各牌顺序,fkkk[ i ] 数组存储洗牌完成后的第 i 张牌是原来的第几张牌。

    这个过程有点像用数组实现链表。

END

你可能感兴趣的:(PAT)