zoukankan      html  css  js  c++  java
  • 2016.1.18(孪生素数、交换)

     1 //孪生素数
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<math.h>
     5 #include<assert.h>
     6 int is_prime(int x)
     7 {
     8     int i,m;
     9     assert(x>=0);  //当x>=0不成立时程序终止
    10     if(x==1) return 0;
    11     m=floor(sqrt(x)+0.5);  //四舍五入
    12     for(i=2;i<=m;i++)
    13     {
    14         if(x%i==0)
    15             return 0;
    16     }
    17     return 1;
    18 }
    19 int main()
    20 {
    21     int i,m;
    22     while(~scanf("%d",&m))
    23    {
    24         for(i=m-2;i>=3;i--)
    25     {
    26         if(is_prime(i)&&is_prime(i+2))

    27 {
    28             printf("%d %d
    ",i,i+2);
    29             break;
    30         }
    31     }
    32    }
    33     return 0;
    34 }
    
    
     1 //交换
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<math.h>
     5 #include<assert.h>
     6 void swap1(int &a,int &b)
     7 {
     8     int t;
     9     t=a;
    10     a=b;
    11     b=t;
    12 }
    13 int main()
    14 {
    15     int a=3,b=4;
    16     printf("交换前:%d,%d	",a,b);
    17     swap1(a,b);
    18     printf("交换后:%d,%d
    ",a,b);
    19     return 0;
    20 }

    
    
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<assert.h>
     5 void swap1(int *a,int *b)
     6 {
     7     int t;
     8     t=*a;
     9     *a=*b;
    10     *b=t;
    11 }
    12 int main()
    13 {
    14     int a=3,b=4;
    15     printf("交换前:%d,%d	",a,b);
    16     swap1(&a,&b);
    17     printf("交换后:%d,%d
    ",a,b);
    18     return 0;
    19 }
     
  • 相关阅读:
    Root of AVL Tree
    04-树4 是否同一棵二叉搜索树
    03-树3 Tree Traversals Again
    03-树2 List Leaves
    283. Move Zeroes
    506. Relative Ranks
    492. Construct the Rectangle
    476. Number Complement
    461. Hamming Distance
    389. Find the Difference
  • 原文地址:https://www.cnblogs.com/csustwj/p/5140497.html
Copyright © 2011-2022 走看看