:利用第三个变量来实现数值的交换
int tmp;
????????tmp = a;
??????a = b;
??????b = tmp;
?同时被两个数整除
a%b==0 && a%c==0
如水仙花数
#include<stdio.h>
?
int main()
{
????int i,x,y,z;
????for(i=100;i<1000;i++)
????{
????????x=i%10;
????????y=i/10%10;
????????z=i/100%10;
????????
????????if(i==(x*x*x+y*y*y+z*z*z))
????????printf("%d\n",i);
????????
????}
????return 0;
}
int i;
????for(i=1;i<20;i++){
????????if(i%2==1){
????????????printf("%d为奇数\n",i);
????????}
????}
int i;
????for(i=1;i<20;i++){
????????if(i%2==0){
????????????printf("%d为偶数\n",i);
????????}
????}
#include <stdio.h>
int main()
{
????int a = 6351;
????int n1 = a/1000;
????int n2 = a%1000/100;
????int n3 = a%100/10;
????int n4 = a%10;
????printf("%d",n4*1000 + n3*100 + n2*10+ n1);
}
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum = sum + i;
}
int a, b = 1;
for (a = 1; a <= 100; a++)
{
b *= a;
}
#include <stdio.h>
int f(int a) { ???????///运用递归函数进行分母的阶乘
if (a == 1)
return 1;
else
return a * f(a - 1);
}
double fun(int n) { ??//调用f函数,累加求和
int i;
double sum;
for (i = 1; i <= n; i++) {
sum += 1.0 / f(i);
}
return sum;
}
int main() {
int n = 20;
double s;
s = fun(n);
printf("%lf", s);
return 0;
}
用哥德巴赫猜想举例 证明任何一个充分大的偶数(大于等于6),总可以表示成两个素数(质数)之和。
#include <stdio.h>
int s(int a) { ????????????????????????????????????????//判断是否为素数
int i;
for (i = 2; i < a; i++) {
if (a % i == 0) {
return 0; ????????????????????????// 不是素数 ?返还0
}
}
if (i == a)
return 1; ??????????????????????????????????//是素数 返还1
}
int main() {
int j, b, k = 9;
scanf("%d", &j);
if (j < 6 || j % 2 == 1) {
printf("输入错误!!!!!\n");
} else {
for (b = 1; b < j; b++) {
if (s(b) == 1 && s(j - b) == 1) {
printf("猜想正确 %d ?%d", b, j - b);
break;
}
}
}
return 0;
}
:最大值,最小值:
#include <stdio.h>
int main() {
int i,k, max, min;
int a[10] ;
for (i = 0; i < 10; i++)
scanf("%d", &a[i]); //定义数组并赋初值
max = a[0];
min = a[0]; //先认为a[0]最大
for (i = 0; i < 10; i++)
if (a[i] > max) { //如果某元素大于max,就取代max的原值
max = a[i];
k = i + 1; //记下此元素的行号;
}
printf("k=%d\nmax=%d\n", ?k, max);
for (i = 0; i < 10; i++)
if (a[i] < min) { //如果某元素小于min,就取代min的原值
min = a[i];
k = i + 1; //记下此元素的行号;
}
printf("k=%d\nmin=%d\n", ?k, min);
:冒泡排序:
#include <stdio.h>
#define N 5
int main() {
int a[N];
int i = 0, j = 0, t = 0;
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
for (i = 0; i < N ; i++) //比较趟数
for ( j = 0; j < N - 1 - i; j++) { //比较次数
if (a[j] > a[j + 1]) { //进行交换
t = a[j + 1];
a[j + 1] = a[j];
a[j] = t;
}
}
for (i = 0; i < N; i++)
printf("%d ", a[i]);
}
选择排序:
#include <stdio.h>
#define N 5
main()
{
int a[N];
int i = 0, j = 0, t = 0;
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
for (i = 1; i < N; i++) ?//比较趟数
for ( j = i; j < N; j++) //比较次数
{
if (a[i - 1] > a[j]) ?//进行交换
{
t = a[i - 1];
a[i - 1] = a[j];
a[j] = t;
}
}
for (i = 0; i < 5; i++)
printf("%d ", a[i]);
}
折半查找:
#include <stdio.h>
int binarySearch(int arr[], int target, int left, int right) {
????while (left <= right) {
????????int mid = left + (right - left) / 2;
????????// 如果目标元素在中间位置
????????if (arr[mid] == target) {
????????????return mid;
????????}
????????// 如果目标元素比中间位置的元素小,在左半部分继续查找
????????if (arr[mid] > target) {
????????????right = mid - 1;
????????}
????????// 如果目标元素比中间位置的元素大,在右半部分继续查找
????????else {
????????????left = mid + 1;
????????}
????}
????// 目标元素不存在于数组中
????return -1;
}
int main() {
????int arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17};
int n = sizeof(arr) / sizeof;
插入:
#include <stdio.h>
#define N 10
int main() {
int e[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, f[N + 2];
int i, j, k, a, b;
scanf("%d", &a);//插入位置
a = a - 1;
scanf("%d", &b);//插入数
for (i = 0; i < a; i++)
f[i] = e[i];
f[i] = b;//插入
for (i = a; i < N; i++)
f[i + 1] = e[i];
}
//删除:
#include <stdio.h>
#define N 10
int main() {
int i = 0, a, e[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
scanf("%d", &a);//删除位置
a = a - 1;
if (N > a) {
for (; a < N; a++)
e[a] = e[a + 1];
e[N - 1] = '\0';//在整型数组中没有必要,但在字符串中一定不要忘记结尾加‘\0’
for (i = 0; i < ?N - 1; i++)
printf("%d ", e[i]);
} else
printf("输入错误");
}
#include <stdio.h>
main() {
int a[3][3] = {1, 9, 7, 2, 3, 8, 4, 5, 6}, i, j, n;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
printf("%d ", a[i][j]);
printf("\n");
}
scanf("%d", &n);
for (i = 0; i < 3; i++)
for (j = 0; j <= i; j++)
a[i][j] = (a[i][j]) * n;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
printf("%d ", a[i][j]);
printf("\n");
}
#include <stdio.h>
#include <string.h>
main() {
char a[3][20], str[20];
int i;
for (i = 0; i < 3; i++)
gets(a[i]);
if (strcmp(a[0], a[1]) > 0)
strcpy(str, a[0]);
else
strcpy(str, a[1]);
if (strcmp(a[2], str) > 0)
strcpy(str, a[2]);
printf("最大的字符串为;%s", str);
#include <stdio.h>
main() {
int a[9][9], i, j;
for (i = 0; i < 9; i++) {
a[i][0] = 1;//定义第一列元素全为1
a[i][i] = 1;//定义行列数相同的元素全为1
}
for (i = 2; i < 9; i++) { //从第三行开始遍历
for (j = 1; j <= i - 1; j++)//第一行开始遍历
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];//元素值为上一行该列元素与上一行上一列元素相加
}
for (i = 0; i < 9; i++) {
for (j = 0; j <= i; j++)
printf("%d\t", a[i][j]);
printf("\n");
}
}
#include <stdio.h>
main() {
char str[80], c;
int i, num = 0, word;
gets(str);
for (i = 0; (c = str[i]) != '\0'; i++) { //对输入的字符串进行遍历
if (c == ' ')//遇到空格字符,使word为0
word = 0;
else if (word == 0) {
word = 1;
num++;
}
}
printf("单词总数为;%d", num);
#include <stdio.h>
#define N 80
#include <string.h>
main() {
char a[N], b[N];
int i, j = 0, d, c;
gets(a);
for (i = 0; i < N; i++) {
if (a[i] == '*')
continue;
else {
d = i;
break;
}//从头开始判断到不为星号的元素取其下标
}
for (i = strlen(a) - 1; i > 0; i--) {
if (a[i] == '*')
continue;
else {
c = i;
break;
}//从尾开始判断到不为星号的元素取其下标
}
for (i = d; i <= c; i++) {
b[j++] = a[i];
}
b[j] = '\0';
puts(b);
例如:一维数组中的原始内容为1,2,3,4,5,6,7,8,9,10;p的值为3。移动后,一维数组中的内容为5,6,7,8,9,10,1,2,3,4。
#include <stdio.h>
#define N 80
void fun(int *w, int k, int n) {
int b[N], i, j = 0;
for (i = 0; i <= k; i++) {//前k+1项存起来
b[i] = w[i];
}
for (i = 0; i < n - k; i++)//后几项往前覆盖
w[i] = w[k + i + 1];
for (i = n - k - 1 ; i < n; i++)//再把前k+1项放到w后面
w[i] = b[j++];
}
int main() {
int a[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int i, k, n = 15;
printf("The original data:\n");
for (i = 0; i < n; i++)
printf("%3d", a[i]);
printf("\n\nEnter k:");
scanf("%d", &k);
fun(a, k, n);
printf("\nThe data after moving:\n");
for (i = 0; i < n; i++)
printf("%3d", a[i]);
printf("\n\n");
return 0;
例:
一维数组元素是
2 2 2 3 4 4 5 6 6 6 6 7 7 8 9 9 10 10 10
删除后,数组中元素应该是
2 3 4 5 6 7 8 9 10
#include <stdio.h>
#define N 80
int fun(int a[], int n) {
int i, j = 0;
for (i = 1; i < n; i++) { ????//遍历数组
if (a[j] != a[i]) ????//如果前后两项不一致
a[++j] = a[i];//j项往后一个元素就用来存放这个不一样的a[i]元素
}
n = j + 1;//更新删除多余元素后的数组长度
return n;
}
int main() {
int a[N] = {2, 2, 2, 3, 4, 4, 5, 6, 6, 6, 6, 7, 7, 8, 9, 9, 10, 10, 10, 10}, i, n = 20;
printf("The original data:\n");
for (i = 0; i < n; i++)
printf("%3d", a[i]);
n = fun(a, n);
printf("\n\nThe data after deleted:\n");
for (i = 0; i < n; i++)
printf("%3d", a[i]);
printf("\n\n");
return 0;
}
#include ???<stdio.h>
#include ???<stdlib.h>
#define ???N ???8
typedef ?struct list
{ ?int ?data;
???struct list ?*next;
} SLIST;
void fun( SLIST ?*h, int ?x)
{ ?SLIST ?*p, *q, *s;
???s=(SLIST *)malloc(sizeof(SLIST));
/**********found**********/
???s->data=___1___;/// x?????s是你所要添加的链表
???q=h;
???p=h->next;
???while(p!=NULL && x>p->data) {
/**********found**********/
??????q=___2___;//q->next ????向后遍历
??????p=p->next;
???}
???s->next=p;///跳出循环 说明找到S链表应该插入的位置,把S的尾巴和下一链表连起来
/**********found**********/
???q->next=___3___;///上一条把尾部连好后,再把前一链表的尾巴跟S链表连起来
}
SLIST *creatlist(int ?*a)
{ ?SLIST ?*h,*p,*q; ?????int ?i;
???h=p=(SLIST *)malloc(sizeof(SLIST));
???for(i=0; i<N; i++)
???{ ?q=(SLIST *)malloc(sizeof(SLIST));
??????q->data=a[i]; ?p->next=q; ?p=q;
???}
???p->next=0;
???return ?h;
}
void outlist(SLIST ?*h)
{ ?SLIST ?*p;
???p=h->next;
???if (p==NULL) ?printf("\nThe list is NULL!\n");
???else
???{ ??printf("\nHead");
???????do { printf("->%d",p->data); ?p=p->next; ?} while(p!=NULL);
???????printf("->End\n");
???}
}
void main()
{ ?SLIST ?*head; ?????int ?x;
???int ?a[N]={11,12,15,18,19,22,25,29};
???head=creatlist(a);
???printf("\nThe list before inserting:\n"); ?outlist(head);
???printf("\nEnter a number : ?"); ?scanf("%d",&x);
???fun(head,x);
???printf("\nThe list after inserting:\n"); ?outlist(head);
}
#include <stdio.h>
#include <stdlib.h>
#define N 6
typedef struct node {
int data;
struct node *next;
} NODE;
void fun(NODE *h) {
NODE *p, *q;
int t;
p = h;
while (p) {
q = p->next; ??//准备比较p,q中元素的大小
while (q) {
if (p->data > q->data) {
t = p->data;
p->data = q->data;
q->data = t;
}//按小到大排
q = q->next; 更新q的位置 向后遍历
}
p = p->next;//判断完初始位置的p中的元素后,向后遍历 准备对下一位置进行排序
}
}
NODE *creatlist(int a[]) {
NODE *h, *p, *q;
int i;
h = NULL;
for (i = 0; i < N; i++) {
q = (NODE *)malloc(sizeof(NODE));
q->data = a[i];
q->next = NULL;
if (h == NULL)
h = p = q;
else {
p->next = q;
p = q;
}
}
return h;
}
void outlist(NODE *h) {
NODE *p;
p = h;
if (p == NULL)
printf("The list is NULL!\n");
else {
printf("\nHead");
do {
printf("->%d", p->data);
p = p->next;
} while (p != NULL);
printf("->End\n");
}
}
int main() {
NODE *head;
int a[N] = {0, 10, 4, 2, 8, 6};
head = creatlist(a);
printf("\nThe original list:\n");
outlist(head);
fun(head);
printf("\nThe list after inverting:\n");
outlist(head);
return 0;
}
感谢通读全文。