Topic 6-8 Statistics substring occurrences in the parent string
1, design ideas:
(1) The first step: first of all, to find out the number of substrings in the parent string, in the traversal of the array, the use of the IF condition statement to judge one by one;
Step two: In the process of traversal, Loop variable to add one by one, instead of looking at the number of characters in the substring, so that the more accurate to find the number of occurrences; (self-sensing algorithm is more troublesome, but really can not think of a simple algorithm, only will be used)
2, Experiment code
#include <stdio.h>int Fun (char *str,char *substr) {int result=0,i=0;for (i=0;str[i]!= '}; i++) {if (Substr[0]==str[i] &&substr[1]==str[i+1]&&substr[2]==str[i+2]) {result++; }}return result;
3, the problem debugging process encountered problems and solutions;
Topic 6-9 the remaining characters in a string in descending order, except for the kinsoku characters
1, design ideas:
(1) The first step: the topic can follow the previous non-character bubble sort method to achieve this problem, but in the traversal of the array, go out two;
The second step: according to this idea, first define the loop variable, then iterate over the array (note: In the traversal of the array should be careful not to include the first and the first two) and then follow the bubble method to sort;
2. Experiment Code
#include <stdio.h> int fun(char *s,int num) {int i=1,j=1,max;for(i=1;i<num-3;i++){ for(j=1;j<num-2;j++) { if(s[j]<s[j+1]) { char swap; swap=s[j];s[j]=s[j+1];s[j+1]=swap; } } } }
3、本题调试过程碰到问题及解决办法 本题没有问题;
Topic 7-1 Output Student Scores
1. Design Ideas
(1) The first step: according to test instructions, first of all to define a number of related variables, such as the number of students N, cyclic variable i, and so on;
The second step: after the creation of memory dynamic storage, (this I follow the teacher in class to copy copy, I understand that the content is not many, later I also went to the internet to check a lot of relevant dynamic storage related tutorials, but also a little understanding, but still not understand the profound)
The third step: after the dynamic storage is created, the contents (scores) are stored in the array, and the sum can be summed at the same time so as to carry out the averaging operation after the storage.
The fourth step: after the storage, the average, then iterate over the array to find the maximum and minimum value, (at this time the maximum and minimum variables should be initialized to the first address element of the array, easy to compare) after the array to find the maximum and minimum value;
Fifth step: Finally, the output can be carried out according to the requirements of the topic;
(2) Flowchart:
2、实验代码
#include<stdio.h>#include<stdlib.h>int main(){int n=0,i=0,*p,sum=0,max=0,min=0;double average=0;scanf("%d",&n);if((p=(int*)calloc(n,sizeof(int)))==NULL){ printf("Not able to allocate memory.\n"); exit(1);}for(i=0;i<n;i++){ scanf("%d",p+i); sum=sum+*(p+i);}average=(double)(sum*1.0/n*1.0);max=*p;min=*p;for(i=0;i<n;i++){ if(max<*(p+i)) { int swap=0; swap=max;max=*(p+i);*(p+i)=swap; }}for(i=0;i<n;i++){ if(min>*(p+i)) { int tmp=0; tmp=min;min=*(p+i);*(p+i)=tmp; }}printf("average = %.2lf\nmax = %.2lf\nmin = %.2lf",average,(double)max,(double)min);free(p);return 0;}
3、本题调试过程碰到问题及解决办法 错误信息1:
The problem is that I put Max and Min's initialization in the For loop, which leads to a point at the time of submission;
Correction method: After this error, I was also very puzzled, because I am typing an example of what is right but I can not cross that point. After I also tried the teacher said the single-step debugging, in the course of debugging I found that each time in the For loop will be Max and min in the initialization sequence, then only slightly suspicious, because I am still very inertial to think that even if the initialization is not a problem, because the results of the output is still correct. But it is always difficult this point, after I went to ask classmates, in the help of classmates, but also pointed out the problem, and finally I put Max and min initialized to the for before the last pass.