杂七杂八
1.比较大小
#include<stdio.h>
#define size 10
int compare(int a[]){
int m=0,*max=&a[0],*min=&a[0];
for(m=0;m<size;m++){
if(a[m]>*max) max=&a[m];
if(a[m]<*min) min=&a[m];
}
printf("The max=%d,situated in the num[%d]\nThe min=%d,situated in the num[%d]\n",*max,max-&a[0],*min,min-&a[0]);
return 0;
}
int main()
{
int num[size];
for(int i=0;i<size;i++) scanf("%d",&num[i]);
compare(num);
return 0;
}
2.连接字符串
//contact srcstr and dststr
#include<stdio.h>
#include<string.h>
int main(){
char srcstr[400],dststr[200];
printf("Welcome! Please give me your first string.\n");
scanf("%s",dststr);
printf("Well,please give me your another string.\n");
scanf("%s",srcstr);
char *p=&srcstr[0];
int size=strlen(dststr)+strlen(srcstr);
//printf("%d %d\n",strlen(dststr),strlen(dststr)+strlen(srcstr));
for(int i=strlen(dststr);i<size;i++){
dststr[i]=*p;
//printf("aaaaa%c %s\n",dststr[i],dststr);
p++;
}
printf("I will try to contact them...\n");
for(int i=0;i<size;i++) printf("%c",dststr[i]);
printf("\nSuccess!");
return 0;
}
3.字符串倒序
#include<stdio.h>
#include<string.h>
int main(){
char str[400],ans[200];
printf("Welcome! Please give me your string.\n");
scanf("%s",str);
char *p=&str[strlen(str)-1];
printf("I will try to reverse them...\n");
for(int i=0;i<strlen(str);i++){
printf("%c",*p);
p--;
}
printf("\nSuccess!");
return 0;
}
4.回文字符串
#include<stdio.h>
#include<string.h>
int main(){
char str[400];
int flag=0;
printf("Welcome! Please give me your string.\n");
scanf("%s",str);
char *p=&str[strlen(str)-1];
for(int i=0;i<strlen(str)/2;i++){
if(*p!=str[i]){
flag++;
break;
}
p--;
}
printf("I am judging now...\n");
if(flag!=0) printf("Sorry,the string you give me makes me disappointing. qwq");
else printf("Good job!");
return 0;
}
5.摸球球
#include<stdio.h>
int main(){
char *ball[]={"red","yellow","blue","white","black"};
for(int i=0;i<5;i++){
for(int j=i+1;j<5;j++){
for(int k=j+1;k<5;k++){
printf("%s %s %s is ok\n",ball[i],ball[j],ball[k]);
}
}
}
return 0;
}
6.堆栈
#include<stdio.h>
#include<stdlib.h>
struct nb{
int yyds;
char xtu;
struct nb *tjy;
};
typedef struct nb no1;
no1 *head,*pr;
int cnt=0;
no1 *creatnode(int in){
no1 *p;
p=(no1*)malloc(sizeof(no1));
if(p==NULL){
printf("I can't give you your memory. qwq\n");
exit(0);
}
p->tjy=NULL;
p->yyds=in;
return p;
}
no1 *pushstack(int num){
if(cnt==0){
head=creatnode(num);
pr=head;
cnt++;
}
else{
pr->tjy=creatnode(num);
pr=pr->tjy;
cnt++;
}
return pr;
}
int popstack(void){
no1 *p;
int ans;
p=head;
for(;;){
if(p->tjy==NULL) break;
else{
pr=p;
p=p->tjy;
cnt--;
}
}
pr->tjy=NULL;
ans=p->yyds;
free(p);
return ans;
}
int main()
{
int pushnum[5]={1,2,3,4,5};
int popnum[5];
for(int i=0;i<5;i++){
pushstack(pushnum[i]);
printf("push No.%d yyds: %d in stack\n",i+1,pushnum[i]);
}
for(int j=0;j<5;j++){
popnum[j]=popstack();
printf("pop No.%d yyds: %d out stack\n",5-j,popnum[j]);
}
return 0;
}