100 Classic C-Algorithms

Source: Internet
Author: User
Tags greatest common divisor

1. Title: Classical Questions: A pair of rabbits, from the 3rd month after birth, every month, a pair of rabbits, rabbits

Zichang to the third month after a month and a pair of rabbits, if the rabbit is not dead, ask the total number of rabbits per month

For how much?

#include <stdio.h>
#include <stdlib.h>
int functionsum (int n)
{
if (n==1| | n==2) return 1;
Else
Return Functionsum (n-2) +functionsum (n-1);
}
void Main ()
{
int n;
printf ("Title: Classical Questions: There are a pair of rabbits, from the 3rd month after birth a pair of rabbits each month, the rabbit grows to the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask the total number of rabbits per month");
printf ("Please input month num=\n");
scanf ("%d", &n);
printf ("\ n totle=%d\n", Functionsum (n));
}

2. Determine how many primes are in between 101-200, and output all primes.

#include <stdio.h>
#include <stdlib.h>

void Functionother (int n,int m)
{
int i,j;
int sum=0;
for (i=n;i<=m;i++)
{
for (j=2;j<i;j++)
{
if (i%j==0)
{
Break
}
Else
{
if (j==i-1)
{
Sum+=1;
printf ("The%d is%d \ t", sum,i);
}
Continue
}

}

}

}

void Main ()
{
int n,m;
scanf ("%d%d", &n,&m);
Functionother (N,M);


}

3.

Title: Print out all the "daffodils", the so-called "Narcissus number" refers to a three-digit number, its members

The number cubic and equals the number itself. For example: 153 is a "narcissus number" because the 153=1 three-time Square

+5 of the three-time Square +3 of the three-time side.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int Regewei (int n)
{
int s=n%10;
return s*s*s;
}

int Reshiwei (int n)
{
int s= (N/10)%10;
return s*s*s;
}
int Rebaiwei (int n)
{
int s=n/100;
return s*s*s;
}
void Functionother ()
{
int i,j;
for (i=101;i<1000;i++)
{
int Sum=regewei (i) +reshiwei (i) +rebaiwei (i);
if (sum==i)
{
printf ("%d is Shuixianhua shu\n", i);
}
}

}

void Main ()
{
Functionother ();

}

4. Title: Decompose a positive integer into factorization. For example: Enter 90 and print out 90=2*3*3*5.

#include <stdio.h>
#include <stdlib.h>

void Functionother (int n,int i)
{


if (n%i==0)
{
printf ("%d", I);
if (n/i>1)
{
printf ("*");
}

Functionother (N/i,i);
}
Else
{
Functionother (n,i+1);
}


}

void Main ()
{
int n;
scanf ("%d", &n);

printf ("%d=", N);
Functionother (n,2);
}

5. Title: Enter two positive integers m and N to find their greatest common divisor and least common multiple.

#include <stdio.h>
#include <stdlib.h>
int functionother (int n,int m)
{
int min=n<m?n:m;
int max=n>m?n:m;
int temp;
while (max%min!=0)
{
Temp=min;
Min=max%min;
Max=temp;
if (max<min)
{
Temp=max;
Max=min;
Min=temp;
}
printf ("min=%d,max=%d", Min,max);
}
return min;
}

void Main ()
{
int n,m;
scanf ("%d%d", &n,&m);
printf ("The Max Gongyueshu is%d\n", Functionother (n,m));
printf ("The Min Gongbeishu is%d\n", N*m/functionother (n,m));

}

6. Title: Enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters

#include <stdio.h>
#include <stdlib.h>

void Main ()
{
char c;
int alpnum=0,space=0,otherchar=0;
while ((C=getchar ())! = ' \ n ')//main is to learn the string input method
{
if (c>= ' a ' &&c<= ' Z ' | | c>= ' A ' &&c<= ' Z ')
{
alpnum++;
}
else if (c== ')
{
space++;
}
Else
{
otherchar++;
}
}
printf ("alpnum=%d,space=%d,otherchar=%d", Alpnum,space,otherchar);

}

100 Classic C-Algorithms

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.