一、 验证歌德巴赫猜想:任意一个不小于6的偶数都可以表示成两个素数的和。从键盘任意给一个符合条件的数,输出相应的两个素数。
素数:指在一个大于1的自然数中,除了1和此整数自身外,没法被其他自然数整除的数
代码如下:
#include <stdio.h> #include <math.h> int sushu(int n) { int i,j; for(i = 2;i <= sqrt(n + 1);i++) { if(!(n%i)) return 0; break; } return 1; } int main() { int a,b,N; int i; printf("Please input a number N: N >= 6 && N%2==0\n"); scanf("%d",&N); if((N < 6) || (N & 1)) { printf("Please input a correct number!\n"); return 0; } else { for(i = 2;i <= N/2;i++) { if(sushu(i) && sushu(N -i)) printf("%d = %d + %d\n",N,i,N-i); break; } } return 0; }
判定一个数是否为素数的简单方法:
#include <stdio.h> int sushu(int n) { int i,j; for(i = 2;i <= sqrt (n + 1);i++) { if(!(n%i)) return 0; } return 1; }
二、完数问题:
题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
找出1000以内的所有完数。
代码如下:
#include <stdio.h> int Sum(int n) { int i; int sum = 0; for(i = 1;i < n;i++) { if(!(n%i)) sum += i; } return sum; } int main() { int j; for(j = 2;j < 1000;j++) { if(Sum(j) == j) printf("%d is a wanshu!\n",j); } return 0; }
三、题目:猜数字游戏
需求定义:
编写程序,实现控制台的猜数字游戏。游戏运行时产生一个0-100之间的随机整数,要求用户从控制台输入数字,若输入的数字比产生的数字小,则输出:“太小了,再大一点!”;若输入的数字比产生的数字大,则输出:“太大了,再小一点!”,若输入的数字和产生的数字相等,则输出:“恭喜你猜对了!”然后退出程序;若用户猜了10次还未猜对,则输出:“你太笨了,下次再来吧!”然后退出程序。
程序如下:
#include <stdio.h> #include <stdio.h> int main() { int n,m; int count = 0; srand((unsigned int)time(NULL)); n = rand(); n %= 100; while(count < 10) { printf("Please input a number:\n"); scanf("%d",&m); if(m == n) { printf("You are right!\n"); return 0; } else if(m < n) { printf("Too small !Please bigger!\n"); count++; } else { printf("Too big!Please smaller!\n"); count++; } } printf("You are stupid!Next!\n"); printf("This num is %d",n); return 1; }
执行结果如下:
fs@ubuntu:~/qiang/caishuzi$ ./caishuzi Please input a number: 50 Too big!Please smaller! Please input a number: 25 Too small !Please bigger! Please input a number: 37 Too small !Please bigger! Please input a number: 43 Too big!Please smaller! Please input a number: 40 Too big!Please smaller! Please input a number: 39 You are right!
这里有个函数,产生一个随机数,大家可以记一下
srand((unsigned int)time(NULL)); n = rand();
实际产生的数可能会很大,这里可以 n %=100,生成的数就是1~100之内的数了,可能不符合规范,但可以达到目的!
四、题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,
重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步
执行程序:
#include <stdio.h> int main() { int i,n; printf("Please input a num:\n"); scanf("%d",&n); printf("%d = ",n); for(i = 2;i <= n;i++) { while(n != i) { if(n%i == 0) { printf("%d*",i); n = n/i; } else break; } } printf("%d\n",n); }
执行结果如下:
fs@ubuntu:~/qiang/14$ ./14 Please input a num: 9 9 = 3*3 fs@ubuntu:~/qiang/14$ ./14 Please input a num: 36 36 = 2*2*3*3 fs@ubuntu:~/qiang/14$
五、题目:写一个函数,统计一个int型数据中有多少位为1;
程序分析:我们知道,如果判定某位是否为1的方法,一个整型数据有多少位为1,可以对此数据进行移位操作,然后判定最后一位是否为1,代码如下:
#include <stdio.h> int main() { int n,count = 0; int i; printf("Please input a num:\n"); scanf("%d",&n); for(i = 0; i < 32;i++) { if(n & 0x01) { count++; } n >>= 1; } printf("Total 1 = %d\n",count); }
执行结果如下:
fs@ubuntu:~/qiang/int$ ./3 Please input a num: 8 Total 1 = 1 fs@ubuntu:~/qiang/int$ ./3 Please input a num: 15 Total 1 = 4 fs@ubuntu:~/qiang/int$
六、鞍点问题
题目:有一个3X4矩阵,要求输出其鞍点(行列均最大的值),以及它的行号和列号。
int a[3][4] = {{123,94,-10,218},
{3,9,10,-83},
{145,16,44,-99}
};
程序分析:首先要搞明白鞍点不止一个,这题肯定涉及到对二维数据的遍历,然后比较大小,先找出当前行最大值,然后判定其在当前列是否为最大值,如果是,则输出
代码如下:
#include <stdio.h> int main() { int a[3][4] ={ {123,94,-10,218}, {3,9,10,-83}, {145,16,44,-99} }; int i,j,k; char flag,flag1; for(i = 0; i < 3; i++ ) { for(j = 0; j < 4; j++) { flag = 0; flag1 = 0; for(k = 0 ;k < 4;k++) { if(a[i][j] < a[i][k])//当前行是否最大 flag = 1; } for(k = 0 ;k < 3;k++) { if(a[i][j] < a[k][j])//当前列是否最大 flag1 = 1; } if(!flag && !flag1)//当前行当前列是否都是最大 printf("hang:%d lie:%d = %d\n",i,j,a[i][j]); } } return 0; }
执行结果如下:
fs@ubuntu:~/qiang/andian$ ./andian hang:0 lie:3 = 218 hang:2 lie:0 = 145 fs@ubuntu:~/qiang/andian$
七、题目、数组归并
已知两个升序数组a、b及空数组c:
int a[] = {1,3,5,7,9,11,13,15,17,19};
int b[] = {2,4,6,8,10,12,14,16,18,20};
int c[20] ;
编写程序将两个数组完成归并,并存入数组c中;
#include <stdio.h> int main() { int a[] = {1,3,5,7,9,11,13,15,17,19}; int b[] = {2,4,6,8,10,12,14,16,18,20}; int c[20]; int i, j, k; i = j = k = 0; while(i < 10 && j < 10) { if(a[i] > b[j]) c[k++] = b[j++]; else c[k++] = a[i++]; } while(i < 10) c[k++] = a[i++]; while(j < 10) c[k++] = b[j++]; printf("c[] = "); for(k = 0;k< 20;k++) printf("%d ",c[k]); printf("\n"); return 0; }
执行结果如下:
fs@ubuntu:~/qiang/shuzu$ gcc -o shuzu7 shuzu7.c fs@ubuntu:~/qiang/shuzu$ ./shuzu7 c[] = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 fs@ubuntu:~/qiang/shuzu$
八、指针输入一个字符串,内有数字和非数字字符,如a123X456 17960? 302tab5876 将其中连续的数字作为一个整数,一次存放到整数型数组a中,例如123放到 a[0],456放到 a[1]中,统计有多少个整数,并输出这些数;
代码如下:
<pre name="code" class="cpp">#include <stdio.h> #include <string.h> int main(int argc, const char *argv[]) { char b[100]; int a[100]; memset(a,'\0',100); char *p = b; int i = 0; int j; int sum = 0; int count = 0; int flag = 1;//标志位,遇到数字为0,遇到非数字为1;此处其初始值为1,默认首字符前面还是非数字,不输出整数,主要配合下面的程序 printf("请输入字符串:\n"); gets(b); while(*p ) { if(*p <= '9' && *p >= '0') { flag = 0;//遇到数字,flag=0 sum = sum*10 + *p++ - '0';//将字符数字转化成整数,此时并不输出。当下一个字符为非数字时,才输出 } else { while(flag == 0)//此时读到非数字字符,判断此时flag,如果此时flag为0.说明上一个字符为数字 { a[i++] = sum ;//此时将数字输出,赋给a[i],i++ sum = 0;//将sum清零 flag = 1;//非数字字符,flag置1 } p++;//此时flag为1,没有整数输出,则看下一个字符 } } //字符串结束后,会遇到两种情况,一个是最后一个字符为数字,另一种是非数字字符 if(flag == 0)//因为前面的程序中,整数的下一个字符为非数字时,才会输出整数,若最后一个是数字的话,则无法输出,所以这里对最后一个字符进行判断 a[i] = sum;//将最后一个整数输出 else i--;//此时最后一个字符为非数字,没有整数输出,但i多加了一次,所以此处i-- count = i + 1;//整数个数为i+1 printf("共有%d个整数\n",count); printf("这些整数是:\na[]="); for(j = 0; j < i+1; j++) printf("%d ",a[j]); printf("\n"); return 0; }
执行结果如下:
fs@ubuntu:~/qiang/tmp$ ./zhizhen1 请输入字符串: 123xiao45 ?<er97 共有3个整数 这些整数是: a[]=123 45 97 fs@ubuntu:~/qiang/tmp$ ./zhizhen1 请输入字符串: xiao12jian5w4gd67dd 共有4个整数 这些整数是: a[]=12 5 4 67 fs@ubuntu:~/qiang/tmp$
九、链表问题
创建一个单向链表,实现一个简单的学生成绩统计系统
代码如下:
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> #define DEBUG() printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__) typedef struct grade { int score; char name[10]; struct grade *next; }Node; Node *CreateList() { Node *p,*head,*tail; head = (Node *)malloc(sizeof(Node)); if(head == NULL) { printf("malloc fails!\n"); return 0; } head->next = NULL; tail = head; int i = 0; while(1) { int s; char n[10]; printf("Please input the student's name!\n"); gets(n); printf("Please input the student's score!\n"); scanf("%d",&s); getchar(); if( s ) { p = (Node *)malloc(sizeof(Node)); if(p == NULL) { printf("malloc fails!\n"); return 0; } p->score = s; strcpy(p->name,n); p->next = NULL; printf("name:%s score:%d\n",p->name,p->score); tail->next = p; tail = p; } else { return head; } } } DisplayList(Node *pnode) { pnode = pnode->next; while ( pnode ) { printf("name:%-6s score:%d\n",pnode->name,pnode->score); pnode = pnode->next; } } LookupList(Node *p) { char n[10]; char *t = n; printf("Please input the name you want:\n"); gets(n); p = p->next; while( p != NULL) { if (!strcmp(p->name,t)) { printf("%s' score is: %d\n",t,p->score); return 0; } else p = p->next; } printf("%s is not exeit!Please input the correct name\n",t); } DestroyList(Node *p) { Node *q; if(p->next != NULL) { q = p; p = p->next; free(q); q = NULL; } } InsertList(Node *p) { char n[10]; char *t = n; printf("Please input the name you want to insert after:\n"); gets(n); p = p->next; while( p != NULL) { if (!strcmp(p->name,t)) { int s; char m[10]; printf("Please input the student's name!\n"); gets(m); printf("Please input the student's score!\n"); scanf("%d",&s); getchar(); Node *q; q = (Node *)malloc(sizeof(Node)); strcpy(q->name,m); q->score = s; q->next = p->next; p->next = q; return 0; } else p = p->next; } } int main() { Node *p1; p1 = CreateList(); DisplayList(p1); LookupList(p1); InsertList(p1); DisplayList(p1); DestroyList(p1); return 0; }
执行结果如下:
fs@ubuntu:~/qiang/link$ ./link2 Please input the student's name! xiao Please input the student's score! 100 name:xiao score:100 Please input the student's name! zhi Please input the student's score! 85 name:zhi score:85 Please input the student's name! qiang Please input the student's score! 88 name:qiang score:88 Please input the student's name! ming Please input the student's score! 77 name:ming score:77 Please input the student's name! hui Please input the student's score! 78 name:hui score:78 Please input the student's name! null Please input the student's score! 0 name:xiao score:100 name:zhi score:85 name:qiang score:88 name:ming score:77 name:hui score:78 Please input the name you want: qiang qiang' score is: 88 Please input the name you want to insert after: ming Please input the student's name! fang Please input the student's score! 92 name:xiao score:100 name:zhi score:85 name:qiang score:88 name:ming score:77 name:fang score:92 name:hui score:78 fs@ubuntu:~/qiang/link$