第一篇:6174猜想C語言代碼-九院黃偉
6174猜想
1955年,卡普耶卡(D.R.Kaprekar)研究了對(duì)四位數(shù)的一種變換:任給出四位數(shù)k0,用它的四個(gè)數(shù)字由大到小重新排列成一個(gè)四位數(shù)m,再減去它的反序數(shù)rev(m),得出數(shù)k1=m-rev(m),然后,繼續(xù)對(duì)k1重復(fù)上述變換,得數(shù)k2.如此進(jìn)行下去,卡普耶卡發(fā)現(xiàn),無論k0是多大的四位數(shù),只要四個(gè)數(shù)字不全相同,最多進(jìn)行7次上述變換,就會(huì)出現(xiàn)四位數(shù)6174.#include
main()
{
int s,a, b, c, d;
int t=0,x;
int m,n;
printf(“請(qǐng)輸入一個(gè)四位每位不全相同的數(shù):”);
scanf(“%d”, &s);
a=s/1000;
b=s%1000/100;
c=s%100/10;
d=s%10;
for(x=0;;x++){
if(x==0){
if(a==b&&a==c&&a==d&&b==c&&b==d&&c==d){
printf(“輸入的四位不合法!n”);break;
}
}
a=s/1000;b=s%1000/100;c=s%100/10;d=s%10;
if(a
if(b if(c if(a if(b if(a m=a*1000+b*100+c*10+d; n=a+b*10+c*100+d*1000; if(m-n==6174){ printf(“s=%d-%d=%dn”, m, n, m-n); printf(“結(jié)束!n”);break; }else{ s=m-n; printf(“s=%d-%d=%dn”, m, n, s); }}} 考拉茲猜想 考拉茲猜想,又稱為3n+1猜想、角谷猜想、哈塞猜想、烏拉姆猜想或敘拉古猜想,是由日本數(shù)學(xué)家角谷靜夫發(fā)現(xiàn),是指對(duì)於每一個(gè)正整數(shù),如果它是奇數(shù),則對(duì)它乘3再加1,如果它是偶數(shù),則對(duì)它除以2,如此循環(huán),最終都能夠得到1。 #include main() { int n;//存儲(chǔ)該自然數(shù) int i; printf(“請(qǐng)輸入一個(gè)自然數(shù):”); scanf(“%d”,&n); for(i=1;;i++){ if(n==1){ printf(“第%d步:n=%dn”,i,n);break; }else if(n%2==0){ printf(“第%d步:n=%d÷2=%dn”,i,n,n/2); n=n/2; }else if(n%2==1){ printf(“第%d步:n=%d×3+1=%dn”,i,n,n*3+1); n=n*3+1; } } } #include “stdio.h” #include void main(){ int n=0;struct course *head=NULL;void insert(struct course **head,struct course *cou);void Print(struct course **head,int *n);void Modify(struct course **head,int *n);void Require(struct course **head);void Creat(struct course **head,int *n);void Delete(struct course **head,int *n);void Fun(struct course **head,int *n); Fun(&head,&n);} void insert(struct course **head,struct course *cou){ struct course *p0,*p1,*p2;p2=p1=*head;p0=cou;if(*head){ while((p0->semester>p1->semester)&&(p1->next)) { p2=p1; p1=p1->next; } if(p0->semester semester) { if(*head==p1)*head=p0; else p2->next=p0; p0->next=p1;} else { if(p0->semester==p1->semester){ while((p0->cID>p1->cID)&&(p1->next)&&(p0->semester==p1->semester)) { } if(p0->semester!=p1->semester){ } else { if(p0->cID<=p1->cID){ if(*head==p1)*head=p0;else p2->next=p0;p2=p1;p1=p1->next;p2->next=p0;p0->next=p1; p0->next=p1; } else {p1->next=p0;p0->next=NULL;} } } else {p1->next=p0;p0->next=NULL;} } } else { *head=p0; p0->next=NULL;} } void Print(struct course **head,int *n){ struct course *p;p=*head;if(*head){ if(*n==1)printf(“nThis %d record is:n”,*n); else printf(“nThese %d records are:n”,*n); printf(“semester cID name creditn”); do { printf(“%-10d%-10d%-18s%-12.1f n”,p->semester,p->cID,p->name,p->credit); p=p->next; }while(p!=NULL);} else printf(“nList null!n”);} void Modify(struct course **head,int *n){ struct course *p,*p2;int cID;if(*head){ Print(head,n);while(1){ printf(“nPlease input the cID which you want to modify:”); scanf(“%d”,&cID);p2=p=*head;while(p->next&&(cID!=p->cID)){ p2=p; p=p->next;} if(cID==p->cID){ printf(“Please input the new cID(1~60):”); scanf(“%d”,&p->cID); while(p->cID<0||p->cID>60) { printf(“nError!”); printf(“nPlease input the new cID(1~60):”); scanf(“%d”,&p->cID); } printf(“Please input the new semester(1~8):”); scanf(“%d”,&p->semester);while(p->semester<0||p->semester>8) { printf(“nError!”); printf(“nPlease input the new semester(1~8):”); scanf(“%d”,&p->semester); } printf(“Please input the new credit:”); scanf(“%f”,&p->credit); printf(“Please input the new name:”); scanf(“%s”,p->name); if(p==*head)*head=p->next; else p2->next=p->next; insert(head,p); break; } else printf(“%d not been found!n”,cID); } } else {printf(“nList null!n”);} } void Require(struct course **head){ struct course *p;float sum=0;int sem,i=0;printf(“nPlease input the semester which is required:”); scanf(“%d”,&sem);p=*head;while(p){ if(sem==p->semester) { i++;if(i==1)printf(“nsemester cID name creditn”);printf(“%-10d%-10d%-18s%-12.1f n”,p->semester,p->cID,p->name,p->credit); sum=sum+p->credit; } p=p->next;} printf(“The sum of credit in this term is:%.1fn”,sum);} void Creat(struct course **head,int *n){ struct course *p1;while(1){ p1=(struct course *)malloc(LEN); printf(“Please input the cID(1~60):”); scanf(“%d”,&p1->cID); while(p1->cID<0||p1->cID>60) { printf(“nError!”); printf(“nPlease input the cID(1~60):”); scanf(“%d”,&p1->cID); } if(p1->cID==0)break; printf(“Please input the semester(1~8):”); scanf(“%d”,&p1->semester); while(p1->semester<0||p1->semester>8) { printf(“nError!”); printf(“nPlease input the semester(1~8):”);scanf(“%d”,&p1->semester); } } } printf(“Please input the credit:”);scanf(“%f”,&p1->credit);printf(“Please input the name:”);scanf(“%s”,p1->name);insert(head,p1);*n=*n+1;printf(“nYou can continue until the cID is ”0“!n”);Print(head,n);void Delete(struct course **head,int *n){ struct course *p1,*p2;int cID;Print(head,n);if(*head){ printf(“Please input the cID of the course which you want to delete:”);scanf(“%d”,&cID);p1=*head; while(cID!=p1->cID&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(cID==p1->cID) { if(p1==*head)*head=p1->next; else p2->next=p1->next; printf(“Have delete cID:%dn”,cID); *n=*n-1; } else printf(“%d not been found!n”,cID);} } void Fun(struct course **head,int *n){ char num; while(1) { system(“cls”); puts(“**************** Main Menu ******************”); puts(“* 1.Add Records 2.Print Records *”); puts(“* 3.Delete Records 4.Modify Records *”); puts(“* 5.Require Records 6.Exit *”); printf(“Please input your choice: ”); scanf(“%d”,&num); switch(num) { case 1:Creat(head,n);break; case 2:Print(head,n);break; case 3:Delete(head,n);break; case 4:Modify(head,n);break; case 5:Require(head);break;case 6:exit(0);break; default: break; } printf(“nPress ”Enter“ to continue!”);getchar();getchar(); } } C語言驗(yàn)證哥德巴赫猜想(100以內(nèi)) #include “stdafx.h” #include “stdio.h” int ss(int i) { int j; if(i <= 1) return 0; if(i == 2) return 1; for(j = 2;j < i;j++) { if(i % j == 0) return 0; else if(i!= j + 1) continue; else return 1; } } int main() { int i, j, k, flag1, flag2, n = 0;for(i = 6;i < 100;i += 2) } {} return 0;for(k = 2;k <= i / 2;k++){} j = i-k;flag1 = ss(k);if(flag1){} flag2 = ss(j);if(flag2){} printf(“%3d=%3d+%3d,”, i, k, j);//輸出結(jié)果 n++;if(n % 5 == 0)//每個(gè)數(shù)自動(dòng)換一行 printf(“n”);//調(diào)用ss函數(shù)判斷另一個(gè)數(shù)是否為素?cái)?shù) //如果都是素?cái)?shù)//調(diào)用ss函數(shù)判斷當(dāng)前數(shù)是否為素?cái)?shù)//循環(huán)判斷是否為素?cái)?shù)//如果等于返回//如果小于等于返回 #include #include int is_prime(int); main(){ } //驗(yàn)證[a,b]區(qū)間內(nèi)的整數(shù)是否符合猜想 int yanzhengGDBH(int a,int b){ } int gdbh(int n){//驗(yàn)證偶數(shù)n能否分解成兩個(gè)素?cái)?shù) int a;a=3;while(a } } if(is_prime(a)){// 判斷a是否是素?cái)?shù)} a++;if(is_prime(n-a)){// 判斷n-a是否是素?cái)?shù)} printf(“OK!%d=%d+%dn”,n,a,n-a);return 1;printf(“gedebahe ,ni cuo le!n”);return 0; int is_prime(int n){//判斷n是否是素?cái)?shù) } int i=2;if(n<2)return 0;while(i<=sqrt(n)){//2-根號(hào)n,找n的因子} return 1;//i是n的因子,不是素?cái)?shù) if(n%i==0){} i++;return 0;//i是n的因子,不是素?cái)?shù)第二篇:考拉茲猜想C語言代碼-九院黃偉
第三篇:C語言課程設(shè)計(jì)代碼
第四篇:C語言驗(yàn)證哥德巴赫猜想
第五篇:c語言 哥德巴赫猜想2