1. method for determining whether a year is a leap year
#include "stdio.h"int isRun(int num){int ret =0;if(num%4==0){ret=1;if(num%100==0){ret =0;}if(num%400==0){ret =1;}}return ret;}int main(){int year;scanf("%d",&year);if(isRun(year)){printf("run\n");}else{printf("no\n");}return 0;}
2 Please write a program which can print all 6 digitsnumbers composed of 1, 2, 2, 3, 4, 5.
#include "stdio.h"void showAnother(char *str,int start,int num){int i=0;char c;if(str[start]=='\0'){printf("%s\n",str);}if(str == NULL)return;for(i=start;i<num;i++){c = str[start];str[start]=str[i];str[i]=c;showAnother(str,start+1,num);c = str[start];str[start]=str[i];str[i]=c;}}void showDigit(char *str,int start,int num){int i=0;char c;if(str == NULL)return;for(i=start;i<num;i++){c = str[start];str[start]=str[i];str[i]=c;showAnother(str,start+1,num);c = str[start];str[start]=str[i];str[i]=c;}}int main(){char str[]="122345";showDigit(str,0,sizeof(str)/sizeof(str[0]));}
3. Enter an English sentence to flip the order of words in the sentence, but the order of Characters in the word remains unchanged. Words in a sentence are separated by space characters. For simplicity, punctuation marks are processed like normal letters.
For example, if "I am astudent." Is input, "student. A am I" is output ".
#include "stdio.h"#include "string.h"#define MAX 1024#define CHARLEN 256void strReverse(char *str,int start,int len){int begin;int end;int temp,i;char c;if(str == NULL)return;begin =start;for(end=start;end<=len;end++){if(str[end]==' '||str[end]=='\0'){temp =end;temp -=1;for(;begin<temp;begin++,temp--){c =str[temp];str[temp]=str[begin];str[begin]=c;}begin =end+1;}}end =len-1;for(i=start;i<end;i++,end--){c = str[i];str[i]=str[end];str[end]=c;}}int main(){char str[MAX];int i;int len;gets(str);len = strlen(str);strReverse(str,0,len);printf("%s\n",str);return 0;}
4. Question: enter two strings to remove all the characters from the first string. For example, if you enter "theyare students." and "aeiou", the first string after deletion becomes "Thy R stdnts .".
#include "stdio.h"#include "string.h"#define MAX 1024#define CHARLEN 256int main(){char str[MAX];char strToMove[MAX];int mask[CHARLEN];int i;int len;int lenToMove;memset(mask,0,sizeof(int)*CHARLEN);gets(str);getchar();scanf("%s",strToMove);len = strlen(str);lenToMove = strlen(strToMove);for(i=0;i<lenToMove;i++){mask[strToMove[i]]=1;}for(i=0;i<len;i++){if(!mask[str[i]])printf("%c",str[i]);}putchar('\n');}
5. calculate all the substrings of a string (one for different orders)
The general meaning of the question is that all the sub-strings of a string (the length ranges from 1 to the total length, but the order is different, for example, AB and BA are counted as one)
This question has a solution that can be processed using binary.
#include "stdio.h"#include "string.h"#include "math.h"#define MAX 1024#define CHARLEN 256int main(){char str[MAX];int len;int number;int i,j;scanf("%s",str);len = strlen(str);number = pow(2,len);printf("All str list\n");for(i=1;i<number;i++){for(j=0;j<len;j++){if((i>>j)&1){putchar(str[j]);}}putchar('\n');}}
6. Swap two nodes in list
Swap (node * head, node * First, node * Second)
It is also complicated to construct nodes for this question, which means to exchange two nodes.
Of course, this question requires pointer operations.
Values cannot be exchanged because the type is unknown.
7Find longest repeat substring, which can be overlap and case sensitive
This question is a typical suffix array question.
Should be relatively simple
I will try it again.
#include "stdio.h"#include "string.h"#include "math.h"#include "assert.h"#define MAX 1024#define CHARLEN 256int Partion(char *str[MAX],int s,int e){int i=s-1;int j;char *p = str[e];char *temp;for(j=s;j<e;j++){if(strcmp(str[j],p)<0){temp = str[i+1];str[i+1] = str[j];str[j]=temp;i+=1;}} temp = str[i+1];str[i+1] = p;str[e]=temp;return i+1;}void quickSort(char *str[MAX],int start,int end){int q;if(start<end){q = Partion(str,start,end);quickSort(str,start,q-1);quickSort(str,q+1,end);}}int comLen(char *str1,char *str2){int count =0;assert(str1&&str2);while(*str1==*str2){if(*str1=='\0'||*str2=='\0')break;count++;str1++;str2++;}return count;}int main(){char str[MAX];char answer[MAX];char *strPoint[MAX];int i,len;int maxLen,clen;scanf("%s",str);len = strlen(str);for(i=0;i<len;i++){strPoint[i]=str+i;}quickSort(strPoint,0,len-1);maxLen =0;for(i=0;i<len-1;i++){clen = comLen(strPoint[i],strPoint[i+1]);if(clen >maxLen){strncpy(answer,strPoint[i],clen);answer[clen]='\0';maxLen = clen;}}printf("%s\n",answer);}
Solve the problem by using the quick row + suffix array, but the test was quite smooth.