2019.11.25~2019.11.30学习报告
1. PTA-寻找完美数
#include<stdio.h>
int judge(int i) {
int j=0;
int n=1;
for(int k=2; k<i; k++) {
if(i%k==0){
j++;
n=n+k;
}
}
int a=0;
if(n==i&&i!=1){
printf("%d ",i);
a=1;
return a;
}
return 0;
}
int main() {
int a,b;
int j=0;
scanf("%d %d",&a,&b);
for(int i=a; i<=b; i++) {
j=judge(i);
if(j==1){
c=1;
}
}
if(c!=1){
printf("None");
}
}
这是第一次提交的,结果只对了一个,其他都是格式错误,感觉应该是结尾不能有空格,就将main函数里的参数c传入judge函数,判断输出的是否是第一个完美数。
#include<stdio.h>
int judge(int i,int c) {
int j=0;
int n=1;
for(int k=2; k<i; k++) {
if(i%k==0){
j++;
n=n+k+i/k;
}
}
int a=0;
if(n==i&&i!=1){
if(c==0){
printf("%d",i);
}
else{
printf(" %d",i);
}
a=1;
return a;
}
return 0;
}
第二次提交的时候有一个测试点没有通过,猜测是i太大的时候超时了,于是把judge函数里循环的循环条件改为
k<sqrt(i)
就通过了。
2.PTA-验证“哥德巴赫猜想”
#include<stdio.h>
#include<math.h>
int prime(int q){
int i=2;
for(;i<q;i++)
{
if(q%i==0){
return 0;
}
}
if(q==i){
return 1;
}
}
int main(){
int n;
scanf("%d",&n);
int m;
m=sqrt(n)+1;
int a;
int ta1;
int ta2;
for(int p=2;p<n/2;p++){
ta1=0;
ta2=0;
a=n-p;
if(prime(p)==0) continue;
if(prime(a)==0) continue;
printf("%d = %d + %d",n,p,a);
return 0;
}
}
第一次提交的时候长这样,依旧是最大N的时候运行超时,想了好久不知道怎么改,上网搜了一下:7-6 验证“哥德巴赫猜想”(20 分)得到:
int prime(int n){
//判断n为偶数时
if(n==2){
return 1;
}
if(n<2||n%2==0){
return 0;
}
//判断n为奇数时
for(int i=3;i*i<=n;i=i+2){
if(n%i==0){
return 0;
}
}
return 1;
}
int main(){
int n;
scanf("%d",&n);
if(n==4){
printf("4 = 2 + 2");
return 0;
}
for(int i=2;i<n/2;i++){
if(prime(i)&&prime(n-i)){
printf("%d = %d + %d",n,i,n-i);
break;
}
}
}
3.洛谷-P2089 烤鸡
只想到了这个方法:
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
if(n>30||n<10) {
printf("0");
return 0;
}
int cnt=0;
int a,b,c,d,e,f,g,h,i,j;
for(a=1; a<=3; a++)
for(b=1; b<=3; b++)
for(c=1; c<=3; c++)
for(d=1; d<=3; d++)
for(e=1; e<=3; e++)
for(f=1; f<=3; f++)
for(g=1; g<=3; g++)
for( h=1; h<=3; h++)
for(i=1; i<=3; i++)
for(j=1; j<=3; j++)
if(a+b+c+d+e+f+g+h+i+j==n)
cnt++;
printf("%d\n",cnt);
for(a=1;a<=3;a++)
for(b=1; b<=3; b++)
for(c=1; c<=3; c++)
for(d=1; d<=3; d++)
for(e=1; e<=3; e++)
for(f=1; f<=3; f++)
for(g=1; g<=3; g++)
for( h=1; h<=3; h++)
for(i=1; i<=3; i++)
for(j=1; j<=3; j++)
if(a+b+c+d+e+f+g+h+i+j==n)
printf("%d %d %d %d %d %d %d %d %d %d\n",a,b,c,d,e,f,g,h,i,j);
}
虽然通过了但是觉得这样太傻了
不过别人交的题解也没看懂…
感觉这个方法挺特别的:
4.链表
周二在b站上看了个讲解单链表的教程:C语言入门教程 第13讲 动态内存分配和链表,跟着打了两遍,看代码理解了一遍,感觉还不是很懂,就尝试自己打了一下。
然后顺便把教程里没说的删除弄出来了。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct stud {
char name[10];
struct stud *next;
};
void print(struct stud *p);
struct stud* remove(struct stud *p);
struct stud *build() {
struct stud *current,*nextp,*head;
head=(struct stud*)malloc(sizeof(struct stud));
char str[10];
printf("type a name: ");
scanf("%s",str);
getchar();
strcpy(head->name,str);
current=head;
char a;
printf("keep doing?\n");
scanf("%c",&a);
while(a=='y') {
printf("type a name: ");
scanf("%s",str);
getchar();
nextp=(struct stud*)malloc(sizeof(struct stud));
strcpy(nextp->name,str);
current->next=nextp;
current=nextp;
printf("keep doing?\n");
scanf("%c",&a);
}
current->next=NULL;
return head;
}
struct stud* insert(struct stud *p){
struct stud *insert,*current;
int position;
char str[10];
printf("insert position:\n");
scanf("%d",&position);
printf("type a name: ");
scanf("%s",str);
getchar();
current=p;
insert=(struct stud*)malloc(sizeof(struct stud));
strcpy(insert->name,str);
insert->next=NULL;
if(position!=0){
while(position>1)
{
current=current->next;
position--;
}//current=insert-1;
insert->next=current->next;
current->next=insert;
}
else {
insert->next=current;
p=insert;
}
printf("\npresent element:\n");
print(p);
return p;
}
int main() {
struct stud *p;
p=build();
printf("present element:\n");
print(p);
char b='b';
printf("insert? y or n");
scanf("%c",&b);
getchar();
while(b!='n'){
p=insert(p);
printf("insert? y or n");
scanf("%c",&b);
// getchar();
}
char c;
printf("delete a name? y or n ");
scanf("%c",&c);
getchar();
while(c!='n'){
p=remove(p);
printf("delete a name? y or n");
scanf("%c",&c);
getchar();
}
return 0;
}
void print(struct stud *p){
while(1) {
if(p->next!=NULL) {
printf("%s\n",p->name);
p=p->next;
}
else{
printf("%s\n",p->name);
break;
}
}
}
struct stud* remove(struct stud *p){
struct stud *position,*current=p;
int po;
printf("place: ");
scanf("%d",&po);
if(po!=0){
while(po!=1){
current=current->next;
po--;
}
position=current;
position=position->next;
current->next=position->next;
}
else{
p=current->next;
free(current);
}
printf("current list:\n");
print(p);
return p;
}