1.最大公约数与最小公倍数
#include <stdio.h>
void Swap(int &a,int &b)
{
int t;
t = a;
a = b;
b = t;
}
int gcd(int a,int b)
{
int t;
if ( a < b)
Swap(a,b);
while( a % b)
{
t = a % b;
a = t;
b = t;
}
return b;
}
int gbd(int a,int b)
{
return a * b /gcd(a,b);
}
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("最大公约数为 : %d\n",gcd(a,b));
printf("最小公倍数为 :%d\n",gbd(a,b));
}
2.一元二次方程的解(复数)
#include <stdio.h>
#include <math.h>
double a,b,c,disc,x1,x2,realpart,imagpart;
void dis1(double a,double b,double c)
{
printf("has two equal roots:%8.4f\n",-b/(2*a));
}
void dis2(double a,double b,double c)
{
x1 = (-b + sqrt(disc))/(2*a);
x2 = (-b - sqrt(disc))/(2*a);
printf("has distinct real roots: %8.4f and %8.4f\n",x1,x2);
}
void dis3(double a,double b,double c)
{
realpart = -b/(2*a);
imagpart = sqrt(-disc)/(2*a);
printf("has complex roots:\n");
printf("%8.4f + %8.4fi\n",realpart,imagpart);
printf("%8.4f - %8.4fi\n",realpart,imagpart);
}
int main()
{
scanf("%lf,%lf,%lf",&a,&b,&c);
printf("The equation");
if (fabs(a) <1e-6)
{
printf("is not a quadratic\n");
}
else
{
disc = b * b - 4 * a * c;
if (fabs(disc) <= 1e-6)
{
dis1(a,b,c);
}
else if (disc > 1e-6)
{
dis2(a,b,c);
}
else
{
dis3(a,b,c);
}
}
return 0;
}
3.素数判断
#include <stdio.h>
#include <math.h>
bool prime(int n)
{
for ( int i = 2;i <= sqrt((double)n);i++)
{
if ( n % i == 0)
{
return false;
}
}
return true;
}
int main()
{
int n;
scanf("%d",&n);
if (prime(n))
printf("%d 是一个素数。\n",n);
else
printf("%d 不是一个素数。\n",n);
return 0;
}
4.数组转置
#include <stdio.h>
void change(int array[3][3])
{
int t;
for (int i = 0;i < 3;i++)
{
for (int j = 0;j <= i;j++)
{
//printf("%d ",array[i]);
t = array[i][j];
array[i][j] = array[j][i];
array[j][i] = t;
}
}
}
int main()
{
int array[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
change(array);
for (int i = 0;i < 3;i++)
{
for (int j = 0;j <3;j++)
{
printf("%d ",array[i][j]);
}
printf("\n");
}
}
5.字符串反序输出
#include <stdio.h>
#include <string.h>
void nixu(char c[])
{
for ( int i = strlen(c) - 1;i >= 0;i--)
{
printf("%c",c[i]);
}
}
int main()
{
char c[10];
gets(c);
nixu(c);
return 0;
}
6.字符串连接
#include <stdio.h>
#include <string.h>
void add(char a[],char b[])
{
char c[100];
int i;
for ( i = 0;i < strlen(a) + strlen(b);i++)
{
i < strlen(a) ? c[i] = a[i] : c[i] = b[i - strlen(a)];
}
c[i] = '\0';
printf("%s",c);
}
int main()
{
char a[10];
char b[10];
gets(a);
gets(b);
add(a,b);
return 0;
}
7.元音字母复制
#include <stdio.h>
#include <string.h>
void copya(char a[],char b[])
{
int i,j;
for ( i = j = 0;i < strlen(a);i++)
{
if ( a[i] == 'a' || a[i] == 'o' || a[i] == 'i' || a[i] == 'o' || a[i] == 'e')
{
b[j++] = a[i];
}
}
b[j] = '\0';
printf("%s",b);
}
int main()
{
char a[10];
char b[10];
gets(a);
copya(a,b);
return 0;
}
8.输出加空格
#include <stdio.h>
#include <string.h>
void change(char c[])
{
for ( int i = 0;i < strlen(c);i++)
{
printf("%c ",c[i]);
}
}
int main()
{
char c[10];
gets(c);
change(c);
return 0;
}
9.统计字符
#include <stdio.h>
#include <string.h>
void count(char c[])
{
int character = 0,space = 0,number = 0,other = 0;
for ( int i= 0;i < strlen(c);i++)
{
if ( c[i] >= 'A' && c[i] <= 'Z' || c[i] >= 'a' && c[i] <= 'z')
{
character++;
}
else if ( c[i] >= '0' && c[i] <= '9')
{
number++;
}
else if ( c[i] == ' ')
{
space++;
}
else
other++;
}
printf("字母有 %d 个。\n",character);
printf("数字有 %d 个。\n",number);
printf("空格有 %d 个。\n",space);
printf("其他有 %d 个。\n",other);
}
int main()
{
char c[10];
gets(c);
count(c);
}
10.最长单词输出
#include <stdio.h>
#include <string.h>
void fuction(char c[])
{
int t1 = 0,t2,max = 0;
int t,t0;
for(int i = 0;i < strlen(c);i++)
{
if ( c[i] == ' ' || i == strlen(c) - 1)
{
t2 = i;
if ( max < t2 - t1)
{
t = t1;
t0 = t2;
t1 = t2 + 1;
}
}
}
for (int i = t;i < t0;i++)
{
printf("%c",c[i]);
}
}
int main()
{
char c[100];
gets(c);
fuction(c);
}
11.字符的气泡排序
#include <stdio.h>
#include <string.h>
void bubble(char a[])
{
int len = strlen(a);
char c;
for ( int i = 0;i < len - 1;i++)
{
for ( int j = 0;j < len -1 - i;j++)
{
if ( a[j] > a[j+1])
{
c = a[j];
a[j] = a[j+1];
a[j+1] = c;
}
}
}
printf("%s",a);
}
int main()
{
char c[100];
gets(c);
bubble(c);
}
12.牛顿法迭代求根
#include <stdio.h>
#include <math.h>
void newtown(int a,int b,int c,int d)
{
float x = 1.0;
float x0 = 0;
float f;
float fd;
while(fabs(x - x0)>= 1e-5)
{
x0 = x;
f = a * x0 * x0 * x0 + b * x0 * x0 + c * x0 + d;
fd = 3 * a * x0 * x0 + 2 * b * x0 + c;
x = x0 - f / fd;
}
printf("解为 %lf \n",x);
}
int main()
{
newtown(1,2,3,4);
return 0;
}
13.递归求多项式
#include <stdio.h>
double function(int n,double x)
{
if ( n == 0 )
return 1;
else if ( n == 1)
return x;
else
return (2 * n - 1) * x - function(n-1,x) - (n-1) * function(n-2,x)/n;
}
int main()
{
int n;
double x;
scanf("%d%lf",&n,&x);
double result = function(n,x);
printf("the result is %lf\n",result);
return 0;
}
14.计算成绩
#include <stdio.h>
#include <stdlib.h>
void stu_average(float score[10][5])
{
float sum = 0;
for (int i = 0;i < 10;i++)
{
for (int j = 0;j < 5;j++)
{
sum += score[i][j];
}
printf("学号:%d,平均分:%5.2f\n\n",i+1,sum/5);
sum = 0;
}
}
void cou_average(float score[10][5])
{
float sum = 0;
for (int i = 0;i < 5;i++)
{
for (int j = 0;j < 10;j++)
{
sum += score[j][i];
}
printf("课程:%d,平均分:%5.2f\n\n",i+1,sum/10);
sum = 0;
}
}
void findmax(float score[10][5])
{
float max = score[0][0];
int a,b;
for (int i = 0;i < 10;i++)
{
for (int j = 0;j < 5;j++)
{
if ( max < score[i][j])
{
max = score[i][j];
a = i;
b = j;
}
}
}
printf("学号:%d,课程:%d,最高分数:%5.2lf\n",a,b,max);
}
void square(float score[10][5])
{
float sum = 0;
float a[10];
for (int i = 0;i < 10;i++)
{
for (int j = 0;j < 5;j++)
{
sum += score[i][j];
}
a[i] = sum / 5;
}
float p = 0,q = 0,t;
for (int i = 0;i < 10;i++)
{
p += a[i] * a[i];
q += a[i];
}
t = p / 10 + (q / 10) * (q / 10);
printf("方差为%5.2lf",t);
}
int main()
{
float score[10][5];
for (int i = 0;i < 10;i++)
{
for (int j = 0;j < 5;j++)
{
score[i][j] = rand() % 100;
printf("%5.2lf ",score[i][j]);
}
printf("\n\n");
}
stu_average(score);
cou_average(score);
findmax(score);
square(score);
return 0;
}
15.几个函数
#include <stdio.h>
#include <string.h>
void input (char na[][20], int nu[]);
void sorts (char na[][20], int nu[]);
void search (char na[][20], int nu[], int x);
int main()
{
char name[10][20];
int num[10], numb, i;
input(name, num);
sorts(name, num);
for (i=0; i<10; i++){
printf("%8s ", name[i]);
printf("%d\n", num[i]);
}
printf("Input your number:");
scanf("%d", &numb);
search(name, num, numb);
return 0;
}
void input (char na[][20], int nu[])
{
int i;
for (i=0; i<10; i++){
printf("Input No.%d name:", i+1);
scanf("%s", na[i]);
printf("Input No.%d number:", i+1);
scanf("%d", &nu[i]);
}
}
void sorts (char na[][20], int nu[])
{
int i, j, temp;
char tm[20];
for (i=0; i<10; i++)
for (j=i+1; j<10; j++){
if (nu[i]>nu[j]){
temp=nu[i];
nu[i]=nu[j];
nu[j]=temp;
strcpy(tm, na[i]);
strcpy(na[i], na[j]);
strcpy(na[j], tm);
}
}
}
void search (char na[][20], int nu[], int x)
{
int low, high, mid;
low = 0;
high = 9;
while (low<=high){
mid=(low+high)/2;
if (x>nu[mid])
low=mid + 1;
else if (x<nu[mid])
high=mid-1;
else if (x==nu[mid]){
printf("%s\n", na[mid]);
break;
}
}
if (x!=nu[mid])
printf("No match!\n");
}
16.进制转换
#include <stdio.h>
void trans(char n)
{
if ( n >= '0' && n <= '9')
{
printf("%c",n);
}
else
printf("%d",n - 'A' + 10);
}
int main()
{
char c;
scanf("%c",&c);
trans(c);
return 0;
}
17.递归输出字符串
#include <stdio.h>
void Convert(int n)
{
char p;
if(n/10 != 0)
{
Convert(n/10); //通过递归输出
}
p=n%10+48;
printf("%c",p);
}
int main()
{
int n ;
scanf("%d",&n);
Convert(n);
return 0;
}
18.计算天数
#include <stdio.h>
void function(int year,int month,int day)
{
int day2 = 0;
int month2 = (year % 4 && !(year % 100) || year % 400) ? 29 : 28;
switch(month)
{
case 1:
day2 = day;
break;
case 2:
day2 = day + 31;
break;
case 3:
day2 = day + 31 + month2;
break;
case 4:
day2 = day + 31 + month2 + 31;
break;
case 5:
day2 = day + 31 + month2 + 31 + 30;
break;
case 6:
day2 = day + 31 + month2 + 31 + 30 + 31;
break;
case 7:
day2 = day + 31 + month2 + 31 + 30 + 31 + 30;
break;
case 8:
day2 = day + 31 + month2 + 31 + 30 + 31 + 30 + 31;
break;
case 9:
day2 = day + 31 + month2 + 31 + 30 + 31 + 30 + 31 + 31;
break;
case 10:
day2 = day + 31 + month2 + 31 + 30 + 31 + 30 + 31 + 31 + 30 ;
break;
case 11:
day2 = day + 31 + month2 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
break;
case 12:
day2 = day + 31 + month2 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
break;
}
printf(" %d 月 %d 日是 %d 年的第 %d 天。\n",month,day,year,day2);
}
int main()
{
int year,month,day;
printf("请输入年月日:\n");
scanf("%d%d%d",&year,&month,&day);
function(year,month,day);
return 0;
}