数据结构自测题总结

第一题:
在这里插入图片描述
在这里插入图片描述
最终正确版

#include <stdio.h>
int main(){
	int n=2,m,t,n1,m2=1,i,j;
	char ch;
	scanf("%d %c",&t,&ch);
	while(m2<=t){
		m2=4*n-2+m2;//这里一开始是没有累加n的
		//printf("m2=%d n=%d\n",m2,n);
		n++;
	}
	n=n-2;//绝了!这个错我找了好久,
	//printf("%d\n",n);
		for(i=1;i<=n;i++){	
		for(j=1;j<=(2*n-i);j++){
	    	if(j<i){
	     	printf(" ");
	    	}else{
	        printf("%c",ch);
			}
		}
		printf("\n");
	}
	for(i=2;i<=n;i++){
	 for(j=1;j<=(n+i-1);j++){//这个n+i-1找错过
	 	if(j<=n-i){
	 		printf(" ");
		 }else{
		 	printf("%c",ch);
		 }
	 }
	 printf("\n");
   }
   m=t-m2+4*n+2;
   printf("%d",m);
   return 0;
   
}

真的想不到,这道题,是我这5道题里最最最坎坷的一道,做了差不多一个晚上…不过在这道题之前已经一个星期没有学习了。就很手生,而且在开始这套题之前我给了自己很大的心理压力,觉得一定要越快越好,就真急,心理也很着急,根本就不是考试时该有的状态,像这个沙漏题,规律也不难,我太着急了,心态乱了,才整了那么久。
第一和第二个经典大错:

while(m2<=t){
		m2=4*n-2;//1.没有循环累加
		//printf("m2=%d n=%d\n",m2,n);
		n++;
	}
	n=n-1;//2.真的是减一吗?应该是n-2

犯第一个错误实在是因为做题不专注,就只想着规律没想着要累加了。
第二个错误不太容易发现,但我也不是第一次犯了。举例,如果n=5算出来的m2>t,那么就应该是n=4是合适的行数。但当m2>t 时,已经是n=6了,因为n=5被n++了,所以这种循环又带++的问题一定要千万注意,会算多的。光是这个错误,我就找了很久。
第三个大错:
其实是思路问题,我一开始是列表找规律,但我找了,空格的规律,* 的规律,每行输出个数的规律,但其实写代码的时候发现根本不用找 * 的规律,输出空格后全输出*就可以了啊。在此也耽误不少时间
第二道题
在这里插入图片描述
这道题的思路,到最后写出来都蛮简单的,但是最后一个测试点运行超时。因为最关键那个知识点没想出来,明明当时也做了,因为复习不扎实,就给忘了啊。
最终正确版:

#include <stdio.h> 
#include <math.h>
#define max 100000
int main(){
	int i,j=2,n,t=0,flag=1,a[max]={0},count=0;
	
	scanf("%d",&n);
	
	for(i=3;i<=n;i++){
		flag=1;
	   for(j=2;j<=sqrt(i);j++){
	   	if(i%j==0){
	   	flag=0;
	   	break;
		   }
	   }
	   if(flag==1){
	   	a[t]=i;
	   	if((a[t]-a[t-1]==2)&&t!=0){
	   		count++;
		   }
		t++;
	   }
	   
	}
    printf("%d",count);
	return 0;
}

第一版和第二版

#include <stdio.h> 
#include <math.h>
#define max 100000
int main(){
	int i,j=2,n,t=0,flag=1,a[max]={0},count=0;
	
	scanf("%d",&n);
	
	for(i=3;i<=n;i++){
		flag=1;
	   for(j=2;j<=i;j++){//第二版这里改成了i/2还是没有通过测试
	   	if(i%j==0){
	   	flag=0;
	   	break;
		   }
	   }
	   if(flag==1){
	   	a[t]=i;
	   	if((a[t]-a[t-1]==2)&&t!=0){
	   		count++;
		   }
		t++;
	   }
	   
	}
    printf("%d",count);
	return 0;
}

改成sqrt(i)才是最直接的,我的数学已经拉到这个地步了。
第三题:
在这里插入图片描述
差不多一遍过!不愧是我做了一万次的题目。

#include <stdio.h>
#define max 100
int main(){
	int i,j,n,m,a[max]={0},b[max]={0};
	scanf("%d %d",&n,&m);
    m=m%n;
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(i=0;i<n;i++){
		if(i>=(n-m)){
			b[i+m-n]=a[i];
		}else{
			b[i+m]=a[i];
			//printf("b[%]=%d,a[%d]=%d\n",i-m,b[i-m],i,a[i]);
		}
	}
	for(i=0;i<n;i++){
		printf("%d",b[i]);
		if(i<(n-1)){
			printf(" ");
		}
	}
return 0;	
}

第四题:
Have Fun with Numbers (20 分)
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

Input Specification:
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

Output Specification:
For each test case, first print in a line “Yes” if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or “No” if not. Then in the next line, print the doubled number.

Sample Input:
1234567899
结尾无空行
Sample Output:
Yes
2469135798
结尾无空行
(翻译造福世界!)
最终正确版:

#include <stdio.h>
#include <string.h>
#define maxi 24//这个问题还没有解决,我刚开始是22,它可能越界了
int main(){
	int flag=1,flag2=1,i,n,t,t1,t2,t3,count=0,b[10]={0},a[10]={0};
	char str[maxi],str2[maxi],str3[maxi]={"1"};
	
	gets(str);//输入数字字符串 
	n=strlen(str);
	
	for(i=n-1;i>=0;i--){
		t=str[i]-'0';//将字符化成数值 
		a[t]++;//统计单个数字的个数 
		//开始模拟乘法的过程 
		if(t*2<10){//t*2后不需要进位的 
			if(flag==0){//后一位数要进位的 
				str2[i]=2*t+1+'0';
				b[2*t+1]++;
				flag=1;
			}else{//不用进位的 
				str2[i]=2*t+'0';
				b[2*t]++;
				flag=1;
			}
			
		}else{//t*2后不需要进位的 
			t1=(2*t)%10;
			
			if(flag==0){				
				str2[i]=t1+1+'0';
				b[t1+1]++;
				flag=0;
			}else{
				str2[i]=t1+'0';
				b[t1]++;
				flag=0;
			}
			if(i==0){//第一位数乘二后要进位的,第一位补1 
				strcat(str3,str2);
				n=n+1;//长度加一 
				b[1]++;
				flag=2;//特别标记,用来输出 
			}
		}	
	}
	str2[n]='\0';
	str3[n]='\0';
	for(i=0;i<10;i++){
		if(a[i]!=b[i]){
			flag2=0;
		    break;
		}
	}
	if(flag2==1&&flag!=2){
		printf("Yes\n");
    	printf("%s",str2);
	}
	else if(flag2==1&&flag==2){
		printf("Yes\n");
    	printf("%s",str3);
	}
	else if(flag2==0&&flag!=2){
		printf("No\n");
    	printf("%s",str2);
	}else if(flag2==0&&flag==2){
		printf("No\n");
    	printf("%s",str3);
	}
return 0;
}

这一版的思路在写出来的时候没有什么大问题,很快就pass了,但是确实臭长了点
第一版:
我是用输入数字的形式来做的,但最后两个测试点是无法通过的。

#include <stdio.h>
int main(){
	long long int num,num2,num3;//注意,题目要求输入
	int n,i,j=0,m,flag=1;
	int a[20]={0},b[20]={0};
	
	scanf("%lld",&num);
	num2=num*2;
	num3=num*2;
	while(num>=1){
		n=num%10;
		a[n]++;
		num=num/10;
		
	}
	while(num2>=1){
		m=num2%10;
		b[m]++;
		num2=num2/10;
		j++;
	}
	for(i=0;i<j;i++){
		if(a[i]!=b[i]){
			flag=0;
			break;
		}
	}
	if(flag==1){
		printf("Yes\n");
		printf("%lld",num3);
		
	}else{
    	printf("No\n");
		printf("%lld",num3);	
	}
	return 0;
}

在这里插入图片描述
让我们来看看各个数据类型的能定义的范围:
unsigned int 0~4294967295
int 2147483648~2147483647
unsigned long 0~4294967295
long 2147483648~2147483647
long long的最大值:9223372036854775807(这个19位用%lld)
long long的最小值:-9223372036854775808
unsigned long long的最大值:1844674407370955161(这个19位用%llu)

__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615(这个也才20位,题目需要储存26位的数值)
所以这道题算是只能用字符的。也是我的基础没有打牢
第五题
7-43 Shuffling Machine
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid “inside jobs” where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.

The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

S1, S2, …, S13,
H1, H2, …, H13,
C1, C2, …, C13,
D1, D2, …, D13,
J1, J2
where “S” stands for “Spade”, “H” for “Heart”, “C” for “Club”, “D” for “Diamond”, and “J” for “Joker”. A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer K (≤20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.

Sample Input:
2
36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47
结尾无空行
Sample Output:
S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5
结尾无空行
菜狗是必须要翻译的

#include <stdio.h>
#include <string.h>
int main(){
    char  a[54][4]={"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 b[54],m,i,j,n,t;
	char c[54][4];
	
	scanf("%d",&t);
	for(i=0;i<54;i++){
		scanf("%d",&b[i]);
	}
	for(i=0;i<t;i++){
		for(j=0;j<54;j++){
		m=b[j];
		strcpy(c[m-1],a[j]);	
		}
		for(j=0;j<54;j++){
	    strcpy(a[j],c[j]);
		}
	}
	for(i=0;i<54;i++){
		printf("%s",c[i]);
        if(i<=52){
            printf(" ");
        }
	}
	return 0;
}

差不多一次过,注意最后没有空格

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值