[Algorithms] Interesting algorithms [1-10]

Source: Internet
Author: User

I recently saw some good C speech algorithms on the Internet. Enumerate and use them for your own learning. Some interesting algorithms include different implementations of C.


Question 1: How many three numbers can be composed of 1, 2, 3, and 4 numbers that are different from each other and have no repeated numbers? What is it? 1. Program Analysis: It can be filled in hundreds of digits, ten digits, and one digit is 1, 2, 3, and 4. Make up all sorts and then proceed
Drop the order that does not meet the conditions.

2. program source code:

Main () {int I, j, k; printf ("\ n"); for (I = 1; I <5; I ++) /* The following is a triple loop */for (j = 1; j <5; j ++) for (k = 1; k <5; k ++) {if (I! = K & I! = J & j! = K)/* Ensure that I, j, and k are different */printf ("% d, % d, % d \ n", I, j, k );}}

Question 2: the enterprise's bonus is based on the profit.

If the profit (I) is less than or equal to 0.1 million yuan, the bonus can be raised by 10%; if the profit is higher than 0.1 million yuan, if the profit is lower than 0.2 million yuan, the portion lower than 0.1 million yuan will be charged by 10%, and the portion higher than 0.1 million yuan, 7.5% for Coco; 0.2 million for a part higher than 0.4 million yuan between 0.2 million and 5%; 0.4 million for a part higher than 0.6 million yuan between 0.4 million and 3%, or for a part higher than yuan; between 0.6 million and 1 million, if the price is higher than 0.6 million yuan, the price can be increased to 1.5%. If the price is higher than 1 million yuan, the price of more than 1 million yuan is increased by 1%, and the current month's profit is input from the keyboard, how many bonuses should be paid?

1. Program Analysis: Use the number axis to divide and locate. Note that you need to define the bonus type as a growth integer.
2. program source code:
main(){  long int i;  int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;  scanf("%ld",&i);  bonus1=100000*0.1;bonus2=bonus1+100000*0.75;  bonus4=bonus2+200000*0.5;  bonus6=bonus4+200000*0.3;  bonus10=bonus6+400000*0.15; if(i<=100000)  bonus=i*0.1; else if(i<=200000)     bonus=bonus1+(i-100000)*0.075;    else if(i<=400000)        bonus=bonus2+(i-200000)*0.05;       else if(i<=600000)           bonus=bonus4+(i-400000)*0.03;          else if(i<=1000000)              bonus=bonus6+(i-600000)*0.015;             else              bonus=bonus10+(i-1000000)*0.01;printf("bonus=%d",bonus);}

Question 3: an integer. After 100 is added, it is a full number of workers, and 168 is a full number of workers. What is this number? 1. Program Analysis: If the value is less than 0.1 million, add 100 to the number before the start, and then add 268 to the number before the start. If
The following conditions are met. See the specific analysis:
2. program source code:

# Include "math. h "main () {long int I, x, y, z; for (I = 1; I <100000; I ++) {x = sqrt (I + 100 ); /* x is the result after 100 is added. */y = sqrt (I + 268 ); /* y is the result after 168 is added. */if (x * x = I + 100 & y * y = I + 268) /* If the square of the square root of a number is equal to this number, it indicates that this number is a full number of bytes */printf ("\ n % ld \ n", I );}}

Question 4: enter a day of a month to determine the day of the year? 1. Program Analysis: Taking January 1, March 5 as an example, we should add up the previous two months, and then add 5 days, that is, the day of the current year.
In this case, an additional day is required when the input month is greater than 3 in a leap year.
2. program source code:
Main () {int day, month, year, sum, leap; printf ("\ nplease input year, month, day \ n"); scanf ("% d, % d, % d ", & year, & month, & day); switch (month)/* calculate the total number of days of the previous month */{case 1: sum = 0; break; case 2: sum = 31; break; case 3: sum = 59; break; case 4: sum = 90; break; case 5: sum = 120; break; case 6: sum = 151; break; case 7: sum = 181; break; case 8: sum = 212; break; case 9: sum = 243; break; case 10: sum = 273; break; case 11: sum = 304; break; case 12: sum = 334; break; default: printf ("data error"); break;} sum = sum + day; /* plus the day of the day */if (year % 400 = 0 | (year % 4 = 0 & year % 100! = 0)/* determine whether it is a leap year */leap = 1; else leap = 0; if (leap = 1 & month> 2) /* if It is a leap year and the month is greater than 2, the total number of days should be plus one day */sum ++; printf ("It is the % dth day. ", sum );}

Question 5: Enter three integers x, y, and z. Please output these three numbers from small to large. 1. Program Analysis: we try to put the smallest number on x and compare x with y first. If x> y, the values of x and y are exchanged,
Then compare x with z. If x> z, the value of x is exchanged with z to minimize x.
2. program source code:

Main () {int x, y, z, t; scanf ("% d", & x, & y, & z); if (x> y) {t = x; x = y; y = t;}/* swap x, y value */if (x> z) {t = z; z = x; x = t;}/* swap x, z value */if (y> z) {t = y; y = z; z = t;}/* swap z, y value */printf ("small to big: % d \ n", x, y, z );}

Question 6: Ask to output the chess board. 1. Program Analysis: Use I control rows and j to control columns, and control the output black square or white square according to the sum of I + j.
2. program source code:

#include "stdio.h"main(){int i,j;for(i=0;i<8;i++) {  for(j=0;j<8;j++)   if((i+j)%2==0)    printf("%c%c",219,219);   else    printf(" ");   printf("\n"); }}

Question 7: classical question: there is a rabbit. every month from the first 3rd months after birth, a rabbit is born. After the third month, a rabbit is born every month. If the Rabbit does not die, what is the total number of rabbits per month? 1. program analysis: the rabbit rule is a sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21 ....
2. program source code:
Main () {long f1, f2; int I; f1 = f2 = 1; for (I = 1; I <= 20; I ++) {printf ("% 12ld % 12ld", f1, f2); if (I % 2 = 0) printf ("\ n");/* controls the output, four lines per line */f1 = f1 + f2;/* values are assigned to the third month in the first two months */f2 = f1 + f2; /* Add the values of the first two months to the third month */}}
 

Question 8: determine the number of prime numbers between-and output all prime numbers. 1. Program Analysis: Method for Determining prime numbers: Remove 2 to sqrt (This number) with a single number. If it can be divisible, it indicates that this number is not a prime number, and vice versa. 2. program source code:
#include "math.h"main(){ int m,i,k,h=0,leap=1; printf("\n"); for(m=101;m<=200;m++)  { k=sqrt(m+1);   for(i=2;i<=k;i++)     if(m%i==0)      {leap=0;break;}   if(leap) {printf("%-4d",m);h++;        if(h%10==0)        printf("\n");        }   leap=1;  } printf("\nThe total is %d",h);}

Question 9: Calculate the prime number within 100 1. program analysis:
2. program source code:
# Include
 
  
# Include "math. h" # define N 101 main () {int I, j, line, a [N]; for (I = 2; I
  
   
Question 10: print out all the "Daffodils". The so-called "Daffodils" refers to a three-digit number. Each digit is equal to the number itself. For example, 153 is a "Daffodils" because 153 = the power of 1 + the power of 5 + the power of 3. 1. program analysis: The for loop is used to control the number of 100-999. Each number is used to calculate a single digit, ten digits, and hundreds of digits. 2. program source code:
   
Main () {int I, j, k, n; printf ("'waterflower' number is:"); for (n = 100; n <1000; n ++) {I = n/100;/* break down hundreds of digits */j = n/10% 10;/* break down ten digits */k = n % 10; /* Split a single position */if (I * 100 + j * 10 + k = I * I + j * j + k * k) {printf ("%-5d", n) ;}} printf ("\ n ");}


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.