浙江大学-翁凯 C语言进阶,编程题
程序员文章站
2024-03-08 09:44:52
...
第一周
题目:
尴尬,第一次,只通过了一个测例
#include<stdio.h>
#include<string.h>
int main(){
char str1[10000] = {0};
char str2[10000] = {0};
char *p,*q;
int n =0;
gets(str1);
gets(str2);
p = str1;
q = str1;
while(1)
{
q = strstr(p,str2);
if(q == NULL)
{
if(n == 0)
printf("-1");
break;
}
n++;
p = q + strlen(str2);
printf("%d ",q-str1);
}
return 0;
}
然后找问题,,,发现原来搞错了输入顺序,修改过来之后
然后想来一个满分,再想了一个,发现还是有问题、、
#include<stdio.h>
#include<string.h>
int main(){
char str1[10000] = {0};
char str2[10000] = {0};
char *p,*q;
int n =0;
int len,i,flag;
gets(str1);
gets(str2);
p = str2;
q = str1;
len =strlen(str1);
flag = 0;
while(*p != '\0' )
{
while(*q != *p)
{
p++;
if(*p == '\0')
break;
}
for(i = 1;i < len;i++)
{
if(p[i] !=q[i])
break;
}
if(i == len)
{
flag = 1;
printf("%d ",p-str2);
}
p++;
}
if(!flag)
printf("-1");
return 0;
}
结果
然后又是一顿思考、、、上面两种方法分别都遗漏了一种情况,然后想呀想,发现是有一种第一次忘记了一个情况就是当字符串1为aa然后字符串为aaaa的输出,然后
#include<stdio.h>
#include<string.h>
int main(){
char str1[10000] = {0};
char str2[10000] = {0};
char *p,*q;
int n =0;
gets(str1);
gets(str2);
p = str2;
q = str2;
while(1)
{
q = strstr(p,str1);
if(q == NULL)
{
if(n == 0)
printf("-1");
break;
}
n++;
p = q + 1;
printf("%d ",q-str2);
}
return 0;
}
终于是解决了、、、、啊哈哈哈哈
更新第二周
#include<stdio.h>
#include<stdlib.h>
typedef struct Ints{
int n;
struct Ints * next;
}Ints;
//把p节点插入l链表中
Ints * inser(Ints *l,Ints *p){
if(!l->next){
l->next = p;
}
else{
p->next = l->next;
l->next = p;
l->n++;
}
return l;
}
int main(){
Ints *head = (Ints *)malloc(sizeof(Ints));
head->n = 0;
head->next = NULL;
int n;
Ints *p = head;
scanf("%d",&n);
while(n != -1){
p = (Ints *)malloc(sizeof(Ints));
p->n = n;
p->next = NULL;
head = inser(head,p);
scanf("%d",&n);
}
p = head->next;
for(n = 0;n<=head->n;n++){
printf("%d ",p->n);
p = p->next;
}
return 0;
}
上一篇: [机器学习-6]回归中的相关度和R平方值
下一篇: Java语法基础练习题1