C语言例题
来源:互联网 发布:网络错误691怎么解决 编辑:程序博客网 时间:2024/05/29 18:20
/*用冒泡排序对10名学生的成绩进行排序*/
#include <stdio.h>
int maopao(int a[]);
int main()
{
int a[10];
int i;
printf("请输入10个学生的成绩(用空格分开):\n");
for(i=0; i<10; i++)
{
scanf("%d", &a[i]);
}
maopao(a);
return 0;
}
int maopao(int a[])
{
int i, j, s;
for(i=0; i<9; i++)
{
for(j=0; j<9-i; j++)
{
if(a[j] > a[j+1])
{
s= a[j];
a[j]= a[j+1];
a[j+1] = s;
}
}
}
for(i=9; i>=0; i--) //从大到小输出
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
/*用选择法对10个学生成绩进行排名*/
#include <stdio.h>
int xuanze(int a[]);
int main()
{
int a[10];
int i;
printf("请输入10位同学的成绩:\n");
for(i=0; i<10; i++)
{
scanf("%d", &a[i]);
}
xuanze(a);
return 0;
}
int xuanze(int a[])
{
int i, j, s;
for(i=0; i<9; i++)
{
for(j=i+1; j<10; j++)
{
if(a[i] > a[j])
{
s = a[i];
a[i] = a[j];
a[j] = s;
}
}
}
for(i=9; i>=0; i--)
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
/*查找一个数在数组中的排名*/
#include <stdio.h>
int main()
{
int a[10];
inti, s, flag; //s为要查找的数,flag用于标记要查找的数的名次
printf("请输入10个数(用空格分开):\n");
for(i=0; i<10; i++)
{
scanf("%d", &a[i]);
}
printf("请输入要查找的数:");
scanf("%d", &s);
maopao(a);
printf("排好序的数组:\n");
for(i=9; i>=0; i--)
{
printf("%d ",a[i]);
if(a[i] == s)
{
flag = 10-i; //数组从大到小排,总共10个,a[i]个前有i个元素
}
}
printf("\n该数的排名是:%d\n",flag);
return 0;
}
int maopao(int a[]) //用冒泡排序对具有10位数据的数组进行排序
{
int i, j, s; //s用来交换两个数
for(i=0; i<9; i++)
{
for(j=0; j<9-i; j++)
{
if(a[j] > a[j+1])
{
s= a[j];
a[j]= a[j+1];
a[j+1] = s;
}
}
}
return 0;
}
/*输入一个正整数,求出它是几位数,并逆序输出*/
#include <stdio.h>
int nixu(int z);
int main ()
{
int z;//z是输入的整数
printf ("请输入一个正整数:");
scanf ("%d",&z);
nixu(z);
return 0;
}
int nixu(int z)
{
int n, p;//z是输入的整数n表示正整数的位数m是逆序后的正整数
n= 0;
p= z;
while (z > 0)
{
z /= 10;
n++;
}
printf ("该数共%d位\n",n);
printf ("逆序为:");
while (n > 0)
{
printf ("%d", p%10);
p = p / 10;
n--;
}
printf("\n");
return 0;
}
/*将字符串b赋值为字符串a*/
#include <stdio.h>
int main()
{
char a[] = "hello world";
char b[100];
char *p1;
char *p2;
p1 = a;
p2 = b;
while(*p1 != '\0')
{
*p2 = *p1;
p1++;
p2++;
}
printf("数组b里的元素为:%s\n",b);
return 0;
}
/*在一个字符串中删除指定字符,如在chararcter中删除a*/
#include <stdio.h>
int del(char ch, char a[]);
int main()
{
char a[] = "chararcter";
char ch;
puts(a);
printf("please input the char you want to delete:\n");
scanf("%c",&ch);
del(ch,a);
return 0;
}
int del(char ch, char a[])
{
int i=0, j=0;
while(a[i] != '\0')
{
if(ch != a[i])
{
a[j++] = a[i];
}
i++;
}
a[j] = '\0';
puts(a);
return 0;
}
/*在字符串中删除一个字符串*/
#include <stdio.h>
int main()
{
char a[] = "chaarbarbcter";
char b[100]; //删除后的字符串
char c[] = "arb"; //要删除的字符串
char *p1;
char *p2;
char *p3;
int i;
p1 = a;
p2 = b;
p3 = c;
printf("将字符串%s中的%s删除\n", a, c);
while(*p1 != '\0')
{
while(*p1 == *p3)
{
p1++;
p3++;
if(*p1 != *p3)
{
p1--;
*p2= *p1;
p2++;
p1++;
p3= c;
continue;
}
while(*p3 != '\0')
{
p1++;
p3++;
}
p3 = c;
}
*p2 = *p1;
p2++;
p1++;
}
*p2 = '\0';
printf("%s\n",b);
return 0;
}
/*在字符串中添加一个字符*/
#include <stdio.h>
int main()
{
char a[] = "hllo"; //在hllo中h后添加e变成hello
char b[100];
char ch = 'e';
int i, j;
i= 0;
j= 0;
while(a[i] != '\0')
{
if(a[i] == 'h')
{
b[j] = a[i];
j++;
b[j] = 'e';
i++;
j++;
continue;
}
b[j] = a[i];
i++;
j++;
}
printf("原来的字符串是%s变为%s\n",a, b);
return 0;
}
/*求字符在字符串中的位置*/
#include <stdio.h>
int main()
{
char a[100]; //保存字符串
int b[100]; //保存查找字符的位置
char ch; //要查找的字符
int i, j;
i= 0;
j= 0;
printf("请输入一串字符:");
gets(a);
printf("请输入要查找的字符:");
scanf("%c",&ch);
while(a[i] != '\0')
{
if(a[i]== ch)
{
b[j] = i + 1;
j++;
}
i++;
}
for(j=0; b[j]!='\0'; j++)
{
printf("%d ",b[j]);
}
return 0;
}
/*指针法将一个数插入到有序数组中*/
#include <stdio.h>
int main()
{
int a[10] = {3,5,8,9,10,23,33,45,47,55};
int b[11]; //插入后的数组
int i, j, num; //num是想要插入的数
int *p1;
int *p2;
p1 = a;
p2 = b;
printf("有序数组为:\n");
for(i=0; i<10; i++)
{
printf("%d ",a[i]);
}
printf("\n请输入想插入的数:");
scanf("%d",&num);
if(num < a[0])
{
*p2 = num;
p2++;
for(i=0; i<10; i++)
{
*p2 = *p1;
p2++;
p1++;
}
}
if(num > a[9])
{
for(i=0; i<10; i++)
{
*p2 = *p1;
p2++;
p1++;
}
*p2 = num;
}
if((num >= a[0]) && (num <= a[9]))
{
for(i=0; i<10; i++)
{
if((num >= *p1) && (num <= *(p1+1)))
{
*p2 = *p1;
p2++;
*p2 = num;
p2++;
p1++;
}
*p2 = *p1;
p1++;
p2++;
}
}
printf("最终序列:\n");
p2 = b;
for(i=0; i<11; i++)
{
printf("%d ",*p2);
p2++;
}
printf("\n");
return 0;
}
/*用指针数组将一组字符串升序排列并输出*/
#include <stdio.h>
#include <string.h>
int main()
{
chara[100];
int i, j, lenth;
char *p;
char temp; //temp用来交换两个数
printf("请输入一串字符:");
gets(a);
p= a;
lenth = strlen(a);
for(i=0; i<lenth-1; i++)
{
for(j=0; j<lenth-1-i; j++, p++)
{
if(strcmp(p, (p+1)) > 0)
{
temp = *p;
*p = *(p+1);
*(p+1) = temp;
}
}
p = a;
}
puts(p);
return 0;
}
/*求任意两个数的最大公约数和最小公倍数(两个数的积=最大公约数*最小公倍数)*/
#include <stdio.h>
int gcd(int x,int y);
int main()
{
int m, n;
int a; //最大公约数
printf("please input two numbers:\n");
scanf("%d %d",&m, &n);
a= gcd(m,n);
printf("最大公约数是%d\n",a);
printf("最小公倍数是%d\n",m*n/a);
return 0;
}
int gcd(int x,int y) //求最大公约数函数
{
int tmp; //用于交换两个数
int r ; //余数
if(x < y)
{
tmp = x;
x = y;
y = tmp;
}
r= x % y;
while(r != 0)
{
x = y;
y = r;
r = x % y;
}
return y; //最大公约数
}
/***************************************************************************停车场计费:小于三小时,每小时2元;在3小时和19小时之间,每小时5元;超过19小时,每小时10元;
****************************************************************************/
#include <stdio.h>
int fare(int h);
int main()
{
int hour;
int a;
printf("please input the time you stayed:\n");
scanf("%d",&hour);
if(hour <= 0)
{
printf("please retype the hour:\n");
scanf("%d",&hour);
}
a= fare(hour);
printf("you should pay %d yuan\n",a);
return 0;
}
int fare(int h) //停车费用计算函数
{
int f; //返回值,停车费
if(h < 3)
{
f= h * 2;
}
else if(h <=19)
{
f= h * 5;
}
else
{
f= h * 10;
}
return f;
}
/***************************************************************************编写一个模拟硬币程序,每投掷一次硬币,程序都要打印正面或者反面,计算正反面出现的次数并打印出结果
****************************************************************************/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int flipcoin(int x);
int main()
{
int n;
printf("please input the times you flip the coins:\n");
scanf("%d",&n);
flipcoin(n);
return 0;
}
int flipcoin(int x) //抛硬币函数,每抛一次,都要打印出结果
{
int count1=0, count2=0;
srand((unsigned)time(NULL));
while(x--)
{
if(1 == rand()%2)
{
printf("正面\n");
count1++;
}
else
{
printf("反面\n");
count2++;
}
}
printf("正面出现%d次\n",count1);
printf("反面出现%d次\n",count2);
return 0;
}
/*求一元二次方程,ax2+bx+c=0的根,并考虑判别式的情况。*/
#include <stdio.h>
#include <math.h>
int equation(float a,float b,float c);
int main()
{
float a, b, c, x1, x2;
printf("please input three numbers:(e.g.:1,2,3)\n");
scanf("%f,%f,%f",&a,&b,&c);
equation(a,b,c);
return 0;
}
int equation(float a,float b,float c)
{
float x, x1, x2;
x= b*b - 4*a*c;
if(x < 0)
{
printf("no roots\n");
}
else if(0 == x)
{
x1 = (-b)/2*a;
printf("roots: x1 = x2 = %f\n",x1);
}
else
{
x1= ((-b) + sqrt(x))/2*a;
x2 = ((-b) - sqrt(x))/2*a;
printf("roots: x1 = %f, x2 = %f\n",x1, x2);
}
return 0;
}
/****************************************************************************已知a,b,c分别为1位数,当两个3位数之和为1333时,即abc+cba=1333,求a,b,c的值
****************************************************************************/
#include <stdio.h>
int solution();
int main()
{
solution();
return 0;
}
int solution()
{
int a, b, c;
int x, y;
for(a=1; a<10; a++)
{
for(b=1; b<10; b++)
{
for(c=1; c<10; c++)
{
x = 100*a + 10*b + c;
y = 100*c + 10*b + a;
if(1333 == (x + y))
{
printf("a = %d,b = %d,c =%d\n",a,b,c);
}
}
}
}
return 0;
}
/**************************************************************************查找某个字符在一个字符串中的位置,假设字符串中的字符是有序的,如“adfgikmnprs”中查找g的位置。(折半查找)
***************************************************************************/
#include <stdio.h>
#include <string.h>
int binarysearch(char ch,char a[]);
int main()
{
char a[] = "adfgikmnprs";
char ch;
puts(a);
printf("please input the capital you want to search:\n");
scanf("%c",&ch);
binarysearch(ch,a);
return 0;
}
int binarysearch(char ch,char a[])
{
int i, len, high, low, mid;
len = strlen(a);
low = 0;
high = len - 1;
while(low <= high)
{
mid = (low + high)/2;
if(ch> a[mid])
{
low = mid + 1;
}
else if(ch < a[mid])
{
high = mid - 1;
}
else
{
printf("the order of the capital is %d\n",mid+1);
break;
}
}
return 0;
}
/*将3行4列的矩阵转置,用函数实现*/
#include <stdio.h>
#define M 3
#define N 4
int transpose(int a[M][N],intb[N][M]); //将矩阵a转置为b并输出
int main()
{
int i, j;
int a[M][N] = {1,2,3,4,5,6,7,8,9,10,11,12};
int b[N][M];
printf("matrix a:\n");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
transpose(a,b);
return 0;
}
int transpose(int a[M][N],int b[N][M]) //将矩阵a转置为b并输出
{
int i, j;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
b[j][i] = a[i][j];
}
}
printf("matrixtranspose b:\n");
for(i=0; i<N; i++)
{
for(j=0; j<M; j++)
{
printf("%3d",b[i][j]);
}
printf("\n");
}
return 0;
}
/*求4个字符串中最小字符串*/
#include <stdio.h>
#define M 4
#define N 100
int minstring(char str[M][N]);
int main()
{
int i;
char str[M][N] = {"hello", "world", "ok","map"};
for(i=0; i<M; i++)
{
printf("str%d = %s\n",i, str[i]);
}
minstring(str);
return 0;
}
int minstring(char str[M][N])
{
int i, j;
char *p;
p= str[0];
for(i=0; i<M-1; i++)
{
if(*p > *(str[i+1]))
{
p = str[i+1];
}
}
printf("minstring = %s\n",p);
}
/*输入5个字符串,升序排列chara[5][100]*/
#include <stdio.h>
#include <string.h>
int order(char a[5][100]); //排序函数
int main()
{
int i;
char a[5][100];
printf("please input 5 strings:\n");
for(i=0; i<5; i++)
{
scanf("%s",&a[i]);
}
order(a);
return 0;
}
int order(char a[5][100]) //排序函数
{
int i, j;
char temp[100];
for(i=0; i<4; i++)
{
for(j=0; j<4-i; j++)
{
if(*a[j] > *a[j+1])
{
strcpy(temp,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],temp);
}
}
}
printf("升序排列:\n");
for(i=0; i<5; i++)
{
printf("%s\n",a[i]);
}
return 0;
}
- C语言例题1:
- C语言例题2:
- C语言例题3:
- C语言例题4:
- C语言例题5:
- C语言例题6:
- C语言例题7:
- C语言例题8:
- C语言例题9:
- C语言例题10:
- C语言例题11:
- C语言例题12:
- C语言例题13:
- C语言例题14:
- C语言例题15:
- C语言例题16:
- C语言例题17:
- C语言例题18:
- 深度学习: 从 Selective Search 到 RPN 再到 FPN
- Eclipse C++下OpenCV工程项目的模板建立
- Excel在统计分析中的应用—第十章—方差分析-方差分析表
- JDK1.5新特性之---自动装箱/拆箱
- Ubuntu下编译android所需ffmpeg的so库
- C语言例题
- 正则表达式
- woa鲸鱼优化算法matlab源码
- 持续更新-爬虫网上练习记录
- Tomcat优化详解
- Boostrap学习笔记
- Android主题
- mysql存储过程
- Codeforces Round #451 (Div. 2) B. Proper Nutrition