4286 views|1 replies

20

Posts

0

Resources
The OP
 

C language algorithm to calculate the age of beautiful women [Copy link]

First accumulate hard power, then accumulate soft power, and finally accumulate smart power. C language is one of the hard power of embedded technology.

When learning technology, the key is to make progress step by step and accumulate bit by bit.

Today we will analyze one of the C language algorithm exercises to calculate the age of a beautiful woman.

The following content only represents personal opinions and may not be correct. It is for reference only and is for reference only.

Calculate the age of a beautiful woman
Question: It is known that the cube of a beautiful woman's age is a four-digit number, the fourth power is a six-digit number, and the cube and fourth power of her age just use up the ten numbers from 0 to 9.
Write a program to find the age of a beautiful woman.

Question analysis:
Assuming the age of the beauty is A, then the following four conditions must be met at the same time:

Condition 1: 999<A*A*A<10000

Condition 2: 99999<A*A*A*A<1000 000

Condition 3: Calculate the numbers A*A*A and A*A*A*A, each of which corresponds to each digit from 0 to 9.

3.1 Separate each digit of the result of A*A*A and A*A*A*A, and store each digit into each element of the array: unsigned char niu[10];.

niu[0]=(A*A*A)/1000;

niu[1]=(A*A*A)%1000/100;

niu[2]=(A*A*A)%1000%100/10;

niu[3]=(A*A*A)%1000%100%10;

niu[4]=(A*A*A*A)/100000;

niu[5]=(A*A*A*A)/10000%10;

niu[6]=(A*A*A*A)/1000%10;

niu[7]=(A*A*A*A)/100%10;

niu[8]=(A*A*A*A)/10%10;

niu[9]=(A*A*A*A)%10;

3.2 Use the bubble algorithm to sort the digits in the array niu[10] from low to high.

3.3 Then each element of niu[10]; is compared with xyd[10]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; one by one. If it holds true, the number can be calculated.

Condition 4: The age range of beauties is: 0<A<100.

Calculate the age of beautiful women program code

/*Define global variables, the default value is 0*/

unsigned int my,a,b,i,j,temp;

char flag=0; //define a flag

unsigned int niu[10];

unsigned int xyd[10]=

{0,1,2,3,4,5,6,7,8,9};

void main()

{

/*Loop to find the number that meets the conditions*/

for(my=0;my<100;my++)

{

/*The cube of the beauty's age is a four-digit number*/

a=my*my*my;

/*The fourth power of the beauty’s age is a six-digit number*/

b=my*my*my*my;

/*The cube of the beauty's age is a four-digit number. First, decompose the four-digit number and store it in the array*/

niu[0]=a/1000;

niu[1]=a/100%10;

niu[2]=a/10%10;

niu[3]=a%10;

/*The fourth power of the beauty's age is a six-digit number. First, decompose the six-digit number and store it in the array*/

niu[4]=b/100000;

niu[5]=b/10000%10;

niu[6]=b/1000%10;

niu[7]=b/100%10;

niu[8]=b/10%10;

niu[9]=b%10;

/*Bubble sort will sort the numbers finally stored in the array in ascending order*/

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

{

for(j=0;j<9-i;j++)

{

if(niu[j]>niu[j+1])

{

temp = niu[j+1];

niu[j+1]=niu[j];

niu[j]=temp;

}

}

}

for(i=0;i<10;i++)

{

/*After the sorting, determine whether the array contains numbers from 0 to 9.

Judge in sequence. If the conditions are met, flag=1 and continue to judge. Repeat 10 times

End the loop and then determine whether the flag is 1. If it is, print the age of the beauty*/

if(niu[i]==xyd[i])

{

flag=1;

continue;

}

else

{

flag=0;

break;

}

}

if(flag==1)

{

/*The result is 18 years old, the girl is so young*/

printf("Beauty's age: %d\n",my);

}

}

}

If you want to quickly master C language programming, you must practice more and write more programs.

This post is from Programming Basics

Latest reply

eqs
Very good, I learned a lot from the article. Thanks to the moderator, I have a better understanding of it.   Details Published on 2024-1-16 17:34
 

4

Posts

0

Resources
2
 

Very good, I learned a lot from the article. Thanks to the moderator, I have a better understanding of it.

This post is from Programming Basics
 
 
 

Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>
Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list