7-1 Simplified insertion sort
#include<stdio.h> int main() { int n,a[1001],x; scanf("%d", & amp;n); for(register int i=0;i<n;i + + ) { scanf("%d", & amp;a[i]); } scanf("%d", & amp;x); a[n]=x;//Put x at the end of the array for(register int i=n;i>=1;i--)//Comparison starts from the last bit of the array in a way similar to bubble sorting { if(a[i]<a[i-1])//If it is smaller, move forward { int t; t=a[i-1]; a[i-1]=a[i]; a[i]=t; } else { break; } } for(register int i=0;i<=n;i + + ) { printf("%d ",a[i]); } return 0; }
7-2 Spiral Square
#include <stdio.h> int main() { int n, num = 1, pos, a[101][101]; //pos is the number of circles scanf("%d", & amp;n); if (n % 2 == 1) { pos = n / 2 + 1;//if n is an odd number } else { pos = n / 2;//n is an even number } for (register int i = 1; i <= pos; i + + ){ //fill in circles //horizontal for (register int j = i; j <= (n - i + 1); j + + ) { a[i][j] = num; num + + ; } num--;//Avoid duplication of corners //right side vertical for (register int j = i; j <= (n - i + 1); j + + ) { a[j][n - i + 1] = num; num + + ; } num--; //lower side for (register int j = (n - i + 1); j >= i; j--) { a[n - i + 1][j] = num; num + + ; } num--; //left vertical for (register int j = (n - i + 1); j > i; j--) { a[j][i] = num; num + + ; } } //generate first and then output for (register int i = 1; i <= n; i + + ) { for (register int j = 1; j <= n; j + + ) { printf("=", a[i][j]); } printf("\ "); } return 0; }
7-3 Matrix A times B
I haven’t passed my own code, source of ideas: pta7-3 matrix A multiplied by B
The core part is basically copied. It is recommended to learn matrix multiplication first.
#include<stdio.h> int main() { int a[101][101],b[101][101],ar,ac,br,bc,ans;//r is the row and c is the column scanf("%d %d", & amp;ar, & amp;ac); for(register int i=1;i<=ar;i + + ) { for(register int j=1;j<=ac;j + + ) { scanf("%d", & amp;a[i][j]); } } scanf("%d %d", & amp;br, & amp;bc); for(register int i=1;i<=br;i + + ) { for(register int j=1;j<=bc;j + + ) { scanf("%d", & amp;b[i][j]); } } if(ac!=br) { printf("Error: %d != %d",ac,br); return 0; } printf("%d %d\ ",ar,bc); for(register int i=1;i<=ar;i + + ) { for(register int j=1;j<=bc;j + + ) { ans=0; for(register int k=1;k<=ac;k + + ) { ans=ans + a[i][k]*b[k][j]; } printf("%d",ans); if(j!=bc) { printf(" "); } } printf("\ "); } return 0; }
7-5 Go together for an autumn outing
#include<stdio.h> int main() { int n,a[1001]; scanf("%d", & amp;n); for(register int i=1;i<=n;i + + ) { scanf("%d", & amp;a[i]); } for(register int i=1;i<=n/2;i + + ) { printf("%d %d\ ",a[i],a[n-i + 1]);//Output the first and last elements of the array } return 0; }
7-6 Deletion of array elements
#include<stdio.h> int main() { int n,k,a[1001]; scanf("%d", & amp;n); for(register int i=0;i<n;i + + ) { scanf("%d", & amp;a[i]); } scanf("%d", & amp;k); for(register int i=1;i<=k;i + + ) { int t; scanf("%d", & amp;t); for(register int j=t-1;j<n;j + + )//Starting from the deleted bit, move each item forward { a[j]=a[j + 1]; } } for(register int i=0;i<(n-k-1);i + + )//The number of elements in the array has changed after deletion { printf("%d ",a[i]); } printf("%d\ ",a[n-k-1]);//In order to comply with the format return 0; }
7-7 supermarket decals
#include<stdio.h> int main() { int n,a[1001]; scanf("%d", & amp;n); for(register int i=1;i<=n;i + + ) { scanf("%d", & amp;a[i]); } for(register int i=1;i<=n;i + + )//Sort, bubble sort is used here { for(register int j=i;j<=(n-1);j + + ) { if(a[j]>a[j + 1]) { int t; t=a[j]; a[j]=a[j + 1]; a[j + 1]=t; } } } int pos=1,ans=0; //Used to check sorting results /*for(register int i=1;i<=n;i + + ) { printf("%d ",a[i]); }*/ for(register int i=2;i<=n;i + + ) { if(a[i]==(a[i-1] + 1))//Judge continuity { pos + + ; } if(pos==3)//If there are three consecutive numbers { ans + + ; pos=1; } } printf("%d",ans); return 0; }
7-8 Square matrix circular shift to the right
#include<stdio.h> int main() { int n,m,a[101][101],b[101][101]; scanf("%d %d", & amp;m, & amp;n); if(m>n)//It is periodic, moving n distances is the same as the original { m=m%n; } for(register int i=1;i<=n;i + + ) { for(register int j=1;j<=n;j + + ) { scanf("%d", & amp;a[i][j]); b[i][j]=a[i][j];//Copy the original copy } } for(register int i=1;i<=n;i + + ) { for(register int j=1;j<=n;j + + ) { if((j + m)<=n)//If the array range will not be exceeded during the right shift { a[i][j + m]=b[i][j]; } else//If it exceeds, fold off the excess and put it at the beginning, that is, subtract n { a[i][j + m-n]=b[i][j]; } } } for(register int i=1;i<=n;i + + ) { for(register int j=1;j<=n;j + + ) { printf("%d ",a[i][j]); } printf("\ "); } return 0; }
7-9 Print Yang Hui triangle
Classic questions
#include<stdio.h> int main() { int n,a[101][101]; scanf("%d", & amp;n); for(register int i=1;i<=n;i + + )//Initialization, the numbers on both sides are one { a[i][1]=1; a[i][i]=1; } for(register int i=3;i<=n;i + + ) { for(register int j=2;j<=i;j + + ) { a[i][j]=a[i-1][j-1] + a[i-1][j];//This is the law of Yang Hui’s triangle } } for(register int i=1;i<=n;i + + ) { for(register int j=1;j<=i;j + + ) { if(j==1)//In order to comply with the format { for(register int k=(n-i);k>=1;k--) { printf(" "); } } printf("M",a[i][j]);//Each number occupies four digits } printf("\ "); } return 0; }
7-10 divided into two columns
#include<stdio.h> struct ss { int num,pos;//num is the student number, pos records odd and even numbers }a[1001]; int main() { int n; scanf("%d", & amp;n); for(register int i=1;i<=n;i + + ) { scanf("%d", & amp;a[i].num); if(i%2==1) { a[i].pos=1; } else { a[i].pos=0; } } //printf("%d",a[1].num); for(register int i=1;i<=n;i + + )//first column { if(a[i].pos==1) { if(i==1) { printf("%d",a[i].num); continue; } printf(" ");//For formatting printf("%d",a[i].num); } } //printf("\ %d",a[2].num); printf("\ "); for(register int i=1;i<=n;i + + )//second column { if(a[i].pos==0) { if(i==2) { printf("%d",a[i].num); continue; } printf(" %d",a[i].num);//For format } } printf("\ ");//If the number of people is 1, the point must have three lines to pass? return 0; }
7-11 Left-pad
#include<stdio.h> #include<string.h> int main() { int n; char c,s[100001]; scanf("%d %c", & amp;n, & amp;c); getchar(); //Be careful to remove the carriage return at the end of the last input, that is, a single character cannot be connected to my string (my understanding) gets(s); //puts(s);//Used during debugging if(n<=strlen(s))//The number of digits to be retained is less than the string length { for(register int i=(strlen(s)-n);i<strlen(s);i + + ) { printf("%c",s[i]); } return 0; //End program } for(register int i=1;i<=(n-strlen(s));i + + ) { printf("%c",c);//Insufficient completion } for(register int i=0;i<strlen(s);i + + ) { printf("%c",s[i]);output as usual } return 0; }
7-12 Matrix operations
#include<stdio.h> int main() { int n,a[101][101],ans=0; scanf("%d", & amp;n); for(register int i=1;i<=n;i + + ) { for(register int j=1;j<=n;j + + ) { scanf("%d", & amp;a[i][j]); if(i==n||j==n)//Do not add the last column and row { continue; } ans + =a[i][j];//Accumulate while inputting } } for(register int i=2,j=(n-1);i<=(n-1),j>=2;i + + ,j--) { ans-=a[i][j];//Subtract the ones on the sub-diagonal, be careful not to subtract too much on the two corners } printf("%d",ans); return 0; }
7-13 Array circular shift left
#include<stdio.h> int main() { int n,m,a[1001],b[1001]; scanf("%d %d", & amp;n, & amp;m); if(m>n)//Moving n has the same effect as no movement { m=m%n; } for(register int i=0;i<n;i + + ) { scanf("%d", & amp;a[i]); b[i]=a[i];//Copy the original copy } for(register int i=0;i<n;i + + ) { if((i-m)<0)//Determine whether the array range is exceeded { a[i-m + n]=b[i]; } else//If not exceeded, move directly { a[i-m]=b[i]; } } printf("%d",a[0]);//For formatting for(register int i=1;i<n;i + + ) { printf(" %d",a[i]);//For format } return 0; }