C语言数学函数相关操作

今天主要介绍C语言关于数学函数几个的相关操作。我用的编译器是tcc。实在没必要用一些大型的编译器,tcc小巧实用,实为居家旅行必备之神器!

1.求任意两个数之间的素数,我加了一个素数判断的函数,很实用。code:

 1 /*求任意两数之间的素数*/
 2 /*long 为 2^31-1=2,147,483,647*/
 3 #include <stdio.h>
 4 #include <math.h>  /*double sqrt(double)*/
 5 int isPrime(long num); 
 6 void AtoBPrime(long n1,long n2);
 7 int main()
 8 {
 9    if(isPrime(102))
10    {
11       printf("Prime");
12    }
13    else
14    {
15       printf("Not Prime!");
16    }
17    AtoBPrime(100,10000);
18    return 0;
19 }
20 /*判断是否是素数,1-是素数,0-不是素数,输入为任意整数*/
21 int isPrime(long num)
22 {
23    int flag = 0;
24    int mov = 2;
25    long high;
26    if(num <= 1)
27    {
28       flag = 0;
29    }
30    if(2 == num)
31    {
32     flag = 1;
33    }
34    high = (long)sqrt(num);
35    for(; mov<high; mov++)
36    {
37     if(num % mov == 0)
38    {
39      flag = 0;
40      mov = num;
41     }
42     else if(high != mov+1)
43     {
44      continue;
45     }
46     else
47     {
48      flag = 1;
49     }
50    }
51  
52    return flag;
53 }
54 /*求任意两个数之间的素数*/
55 void AtoBPrime(long n1,long n2)
56 {
57  long mov;
58  int cnt=0;
59  printf("the array prime:\n");
60  for(mov=n1;mov<n2;mov++)
61  {
62   if(isPrime(mov))
63   {
64    cnt++;
65    if(cnt%5 == 0)
66    {
67     printf("\n");
68    }
69    printf("%d ",mov);
70   }
71  }
72 }

2.回文数。

 1 /*Palindrome:回文数,指正读,反读都是一样的数*/
 2 #include <stdio.h>
 3 #include <stdlib.h> /*char* ltoa (long, char*, int)*/
 4 #include <string.h> /*size_t strlen(char *)*/
 5 #include <math.h>
 6 int isPalindrome(long num); 
 7 void AtoBPalindrome(long n1,long n2);
 8 int main()
 9 {
10      if(isPalindrome(101))
11      {
12           printf("Palindrome\n");
13      }
14      else
15      {
16          printf("Not Palindrome!\n");
17      }
18      AtoBPalindrome(10,10000);
19      return 0;
20 }
21 /*判断是否是素数,1-是回文数,0-不是回文数,输入为任意整数*/
22 int isPalindrome(long num)
23 {
24  int i,len,half;
25  int flag=0;
26  char arr[20];  /*字符型数组,存放数的各个位上的数字*/
27  ltoa(num,arr,10);
28  /***************************
29  char* ltoa (long, char*, int);  
30  long:指定装换的长整形数字
31  char*:接受字符串
32  int:进制属性
33  ****************************/
34  len = strlen(arr);
35  half = len/2;
36  for(i=0;i<=half;i++)
37  {
38   if(arr[i] != arr[--len])
39   {
40    flag = 0;
41    break;
42   }
43   if(i >= half)
44   {
45    flag = 1;
46   }
47   else
48   {
49    flag = 0;
50   }
51  }
52  return flag;
53 }
54 /*打印三层回文数*/
55 void AtoBPalindrome(long n1,long n2)
56 {
57  long mov;
58  for(mov=n1 ; mov<n2 ;mov++)
59  {
60   if(isPalindrome(mov)&&isPalindrome(mov*mov)&&isPalindrome(mov*mov*mov))
61   {
62    printf("n=%d\n",mov);
63   }
64  }
65 }

3.神奇的6174

 1 /*神奇6174,找到一个四位数,从大到小排列和从小到大排列所得数的差为它自己*/
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <math.h>
 6 int isSame(long num); 
 7 void AtoBSame(long n1,long n2);
 8 int main()
 9 {
10  AtoBSame(1000,10000);
11  return 0;
12 }
13 int isSame(long num)
14 {
15  int flag = 0;
16  int arr[4];
17  int i=0,j,temp;
18  int n1,n2;//重新组合数字
19  /*解析数字*/
20  arr[0] = num/1000;
21  arr[1] = num/100%10;
22  arr[2] = num/10%10;
23  arr[3] = num%10;
24  
25  /*排序,从大到小*/
26  for(;i<3;i++)
27  {
28   for(j=i+1;j<4;j++)
29   {
30    if(arr[i]<arr[j])
31    {
32     temp = arr[j];
33     arr[j] = arr[i];
34     arr[i] = temp;
35    }
36   }
37  }
38  n1 = arr[0]*1000+arr[1]*100+arr[2]*10+arr[3];
39  n2 = arr[3]*1000+arr[2]*100+arr[1]*10+arr[0];
40  /*两个数是相差是否是一个数*/
41  if(num == n1-n2)
42  {
43   flag = 1;
44  }
45  return flag;
46 }
47 /*求能够满足条件的特殊数*/
48 void AtoBSame(long n1,long n2)
49 {
50  long mov;
51  for(mov=n1;mov<n2;mov++)
52  {
53   if(isSame(mov))
54   {
55    printf("num=%d\n",mov);
56   }
57  }
58 }

4.数字e,与最大公约数,最小公倍数。

 1 /*实用的数字操作*/
 2 #include <stdio.h>
 3 #include <math.h>
 4 float efun();/*求的数字e*/
 5 long divisor(long n1,long n2);/*最大公约数*/
 6 long multiple(long n1,long n2);/*最小公倍数*/
 7 int main()
 8 {
 9  float e=0.0f;
10  long d,m;
11  
12  e = efun();
13  printf("%8f\n",e);
14  /*最大公约数与最小公倍数测试语句*/
15  d = divisor(9,100);
16  m = multiple(10,101);
17  printf("d=%d\nm=%d",d,m);
18  return 0;
19 }
20 float efun()
21 {
22  float e=1.0f,n=1.0f;
23  int i = 1;
24  while(1/n > 1e-10)
25  {
26   e += 1/n;
27   i++;
28   n = i*n;
29  }
30  return e;
31 }
32 /*求最大公约数函数*/
33 long divisor(long n1,long n2)
34 { 
35  long t,c,d; 
36  /*说明n1比较大*/
37  if(n1<n2)
38  {
39   t = n1;
40   n1 = n2;
41   n2 = t;
42  }
43  /*求最大公约数*/
44  c = n1%n2;
45  while(c != 0)
46  {
47   n1 = n2;
48   n2 = c;
49   c = n1%n2;
50  }
51  d = n2;/*d为最大公约数*/
52  return d;
53 }
54 /*求小公倍数函数*/
55 long multiple(long n1,long n2)
56 {
57  long c,m;
58  c = n1*n2;
59  m = c/divisor(n1,n2);
60  return m;
61 }

5.牛顿法解方程。

 1 /*牛顿法解方程*/
 2 #include <stdio.h>
 3 #include <math.h>  /*fabs(float)*/
 4 float Newton(float a , float b , float c , float d);
 5 int main()
 6 {
 7  float x;
 8  x = Newton(1,2,3,4);
 9  printf("x=%.5f\n",x);
10  return 0;
11 }
12 /*牛顿解法函数形式*/
13 float Newton(float a , float b , float c , float d)
14 {
15  float x=1.0f,x0,f,f1;
16  do
17  {
18   x0 = x;
19   f = ((a*x0+b)*x0+c)*x0+d;  /*函数本身*/
20   f1 = (3*a*x0+2*b)*x0+c;  /*导数*/
21   x = x0 - f/f1;    /*迭代公式*/
22  }while(fabs(x-x0) >= 1e-5);
23  return x;
24 }
25  

6.计算系统运行时间

/*计算系统运行时间*/
#include <stdio.h>
#include <conio.h>/*kbhit(void)*/
/*********************************************
kbhit(void)
功 能及返回值:检查当前是否有键盘输入,若有则返回一个非0值,否则返回0
用 法:int kbhit(void); 
*********************************************/
#include <stdlib.h>/*void _sleep (unsigned long);tcc*/
typedef struct time
{
 int hour;
 int minute;
 int second;
}time;
int main()
{
 time t = {0,0,0};
 while(!kbhit())
 {
  _sleep(1000);/*1s=1000ms*/
  if(t.second == 59)
  {
   t.minute = t.minute + 1;
   if(t.minute == 60)
   {
    t.hour = t.hour+1;
    t.minute = 0;
   }
   t.second = 0;
  }
  else
  {
   t.second = t.second+1;
  }
  printf("%d:%d:%d\n",t.hour,t.minute,t.second);
 }
}

 

posted @ 2013-01-16 23:11  alexander.bruce.lee  阅读(553)  评论(0编辑  收藏  举报