[Algorithms] Interesting algorithms [31-40]

Source: Internet
Author: User

Example 31: Convert octal to decimal 1. program analysis:
2. program source code:

main(){ char *p,s[6];int n;  p=s;  gets(p);  n=0;  while(*(p)!='\0')  {n=n*8+*p-'0';  p++;}  printf("%d",n);}

Question 32: calculate the number of odd numbers that can be composed of 0 to 7. 1. program analysis:
2. program source code:
main(){  long sum=4,s=4;  int j;  for(j=2;j<=8;j++)/*j is place of number*/  { printf("\n%ld",sum);  if(j<=2)  s*=7;  else  s*=8;  sum+=s;}  printf("\nsum=%ld",sum);}

Question 33: an even number can always represent the sum of two prime numbers. 1. program analysis:
2. program source code:
#include "stdio.h"#include "math.h"main(){ int a,b,c,d;  scanf("%d",&a);  for(b=3;b<=a/2;b+=2)  { for(c=2;c<=sqrt(b);c++)  if(b%c==0) break;  if(c>sqrt(b))    d=a-b;  else    break;  for(c=2;c<=sqrt(d);c++)  if(d%c==0) break;  if(c>sqrt(d))    printf("%d=%d+%d\n",a,b,d);  }}

Question 34: judge whether a prime number can be divided by a few 9. 1. program analysis:
2. program source code:
Main () {long int m9 = 9, sum = 9; int zi, n1 = 1, c9 = 1; scanf ("% d", & zi); while (n1! = 0) {if (! (Zi/sum)/* If integer a is divided by a non-zero integer B, the quotient is an integer, and the remainder is zero, let's say that a can be divisible by B (or B can be divisible by a), and it is recorded as B |. Note that if B is 0, it is not called Division. */N1 = 0; else {m9 = m9 * 10; sum = sum + m9; c9 ++ ;}} printf ("% ld, can be divided by % d \ "9 \" ", sum, c9 );}

Question 35: A company uses a public phone to transmit data. The data is a four-digit integer and encrypted during the transmission process. The encryption rules are as follows: add 5 to each number, then, replace the number with the remainder divided by 10, and then exchange the first and fourth digits, and the second and third digits.
1. program analysis:
2. program source code:
main(){int a,i,aa[4],t;  scanf("%d",&a);  aa[0]=a%10;  aa[1]=a%100/10;  aa[2]=a%1000/100;  aa[3]=a/1000;  for(i=0;i<=3;i++)  {aa[i]+=5;   aa[i]%=10;  }  for(i=0;i<=3/2;i++)  {t=aa[i];   aa[i]=aa[3-i];   aa[3-i]=t;  }  for(i=3;i>=0;i--)    printf("%d",aa[i]);}

Question 36: there are a bunch of peaches on the beach, and five monkeys can score points. The first monkey divided the pile of peach creden into five portions, one more.

The monkey threw one more of them into the sea and took one. The second monkey divided the remaining peaches into five equal portions.
One, it also threw one more monkey into the sea and took one copy. The third, fourth, and fifth monkeys did the same,
How many peaches are there at least on the beach?
1. program analysis:
2. program source code:

main(){    int i,m,j,k,count;    for(i=4;i<10000;i+=4)    {       count=0;      m=i;      for(k=0;k<5;k++)      {       j=i/4*5+1;       i=j;       if(j%4==0)        count++;       else        break;      }     i=m;     if(count==4)     {       printf("%d\n",count);      break;      }    }}

Question 37: There are n people in a circle with sequential troubleshooting. When the first person reports the number (from 1 to 3), the person who reports the number 3 leaves the circle and asks the last person who left the number.

1. program analysis:
2. program source code:

#define nmax 50main(){  int i,k,m,n,num[nmax],*p;  printf("please input the total of numbers:");  scanf("%d",&n);  p=num;  for(i=0;i<n;i++)   *(p+i)=i+1;   i=0;   k=0;   m=0; while(m<n-1) {   if(*(p+i)!=0) k++;   if(k==3)   { *(p+i)=0;     k=0;     m++;   }    i++;    if(i==n) i=0;  }  while(*p==0) p++;    printf("%d is left\n",*p);}

Question 38: n products before recursion 1. program analysis:
2. program source code:

int fac (int n){   if(n==1)   return 1;   return n*fac(n-1);}

Question 39: remove the same string 1. program analysis:
2. program source code:

        public static string RemoveExtraCharAndWithoutSort(string str)        {            if (string.IsNullOrEmpty(str))            {                return null;            }            int[] tempArr = new int[256];            string newStr = string.Empty;            for (int i = 0; i < str.Length; i++)            {                if (tempArr[str[i]] == 0)                {                    tempArr[str[i]] = 1;                    newStr += str[i];                }            }            return newStr;        }

Question 40: The penultimate m Node 1. program Analysis: Compared with the two-way linked list, the one-way linked list can only access each node of the linked list from start to end, so if you want to find the m-last element of the linked list, you can only
Search from start to end. During the search process, set two pointers. The current Pointer Points to the currently accessed node,
The previous Pointer Points to the node before current, and the two are m nodes apart, so that when the current Pointer Points to the last
When a node is located, the elements pointed to by the previous pointer are the m-down elements. The processing process of the program is as follows:
2. program source code:

Element * CLinkList: FindMToLastElement (int m) {element * previous, * current; previous = current = head; // access M-1 for (int I = 0; I <m-1; ++ I) {current = current-> next; if (current = NULL) {printf ("number overstep \ n"); return NULL ;}} // The previous pointer and current pointer move to the end together while (current-> next! = NULL) {previous = previous-> next; current = current-> next;} // returns the previous pointer return previous ;}


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.