二级程序设计

#include<stdio.h>
int main(void)
{
    int a,b,c,m,n,p;
    printf("enter m and n:");
    scanf("%d%d",&m,&n);
    a=m;
    b=n;
    c=a%b;
    while(c!=0){
        m=n;
        n=c;
        c=m%n;
    }
    p=a*b/n;
    printf("%d %d\n",n,p);
    return 0;
}

/*****使用函数求100以内的所有素数*****/
#include<stdio.h> #include<math.h> int main(void) { int count,m; int prime(int m); count=0; for(m=2;m<=100;m++){ if(prime(m)!=0){ printf("%6d",m); count++; if(count%10==0) printf("\n"); } } printf("\n"); return 0; } int prime(int m) { int i; double n; if(m==1) return 0; n=sqrt(m); for(i=2;i<=n;i++) if(m%i==0){ return 0; } return 1; }

/****最大公约数与最小公倍数*****/
#include<stdio.h> int main(void) { int m,n,a,b,c,p; printf("enter m:"); scanf("%d",&m); printf("enter n:"); scanf("%d",&n); a=m; b=n; c=m%n; while(c!=0){ m=n; n=c; c=m%n; } p=a*b/n; printf("%d %d\n",n,p); return 0; }

/***a,b两数和c的竖式***/

#include<stdio.h>

int main(void)

{

         long a,b,c,p,q;

         printf("enter a and b:");

         scanf("%8ld%6ld",&a,&b);

         printf("%8ld\n",p=a);

         printf("%2c%6ld\n",'+',q=b);

         printf(" %8s\n","--------");

         printf("%8ld\n",c=p+q);

         return 0;

}

/***将小写字母换成大写字母(大小写字母的ASCII码相差32位,将小写字母的ASCII码减去32即得大写字母)***/

#include<stdio.h>

int main(void)

{

         char a,b;

 

         a='x';

         b='y';

         a-=32;

         b-=32;

         printf("%c %c\n%d %d\n",a,b,a,b);

         return 0;

}     

#include<stdio.h>

int main(void)

{

         char a,b;

         a='X';

         b='Y';

         a+=32;

         b+=32;

         printf("%c %c\n%d %d\n",a,b,a,b);

         return 0;

}

/****不借助第三变量,通过计算交换两个变量****/

#include<stdio.h>

int main(void)

{

               int a=10,b=20;

 

               printf("a=%d,b=%d\n",a,b);

               a+=b;

               b=a-b;

               a-=b;

               printf("a=%d,b=%d\n",a,b);

               return 0;

}

/****一个数恰好等于它的因子之和,这个数就叫“完数”,找出1000以内的所有完数,并输出其因子****/

#include<stdio.h>

int main(void)

{

               int i,j,s;

               for(i=1;i<=1000;i++,s=1){

                   for(j=2;j<=i/2;j++)

                            if(i%j==0){

                                     s+=j;

                            }

                            if(s==i){

                                     printf("%d=1",i);

                                     for(j=2;j<=i/2;j++)

                                               if(i%j==0)

                                                        printf("+%d",j);

                                               printf("\n");

                            }

               }

               return 0;

}

/***打印出所有符合其各位数字立方之和等于其本身的三位数***/

#include<stdio.h>

int main(void)

{

               int i,j,k,x;

               for(x=100;x<1000;x++){

                   i=x%10;

                   j=(x/10)/10;

                   k=x/100;

                   if(x==i*i*i+j*j*j+k*k*k)

                            printf("%d \n",x);

               }

               return 0;

}

/****已知三位整数X和Y满足X+Y=1333,其中X的个位数是Y的百位数,Y的个位数是X的百位数,十位数一样,求满足条件的X和Y****/

#include<stdio.h>

int main(void)

{

               int i,j,k,x,y;

               for(i=1;i<=9;i++)

                   for(j=1;j<=9;j++)

                            for(k=1;k<=9;k++)

                                     if((i+k)*101+j*20==1333){     /***x+y=(100*i+10*j+k)+(100*k+10*j+i)=101*(i+k)+20*j ***/

                                               x=i*100+j*10+k;

                                               y=k*100+j*10+i;

                                               printf("%d+%d=1333\n",x,y);

                                     }

                                     return 0;

}

 

           

 

 

 

posted @ 2014-03-23 12:07  zhaoyang16  阅读(457)  评论(0编辑  收藏  举报