当前位置:网站首页>[C question set] of Ⅵ

[C question set] of Ⅵ

2022-07-03 10:11:00 InfoQ

write in front

​​Hello, Hello, everyone  En, Learn together , Your smile (●'◡'●)
Welcome to → give the thumbs-up  +  Collection ️ +  Leaving a message.
️ We are not on the stage of our choice , Acting is not the script we chose
summary : I hope after you read it , It can help you , Insufficient, please correct ! Learn and communicate together
2021  Blog star of the year, Internet of things and embedded development  TOP5~2021  Blog star  Top100~ Alibaba cloud experts  ^  Star Blogger ~ Nuggets ⇿InfoQ  Creator ~ Zhou Bang  77» General list  1479

Question 26 → Realization N The class of ( respectively while、for) 

First N The class of is actually a hypothesis 5 Your class is 1x2x3x4x5 This is called 5 The class of , This question requires while Circulation and for The topic of circular hierarchy is actually to examine your understanding of circular , This topic should pay special attention to the expression in the next loop , And create a sum The total value is used for each cycle i Multiply sum, Print until the end of the cycle sum.

Question 27 → Find a specific number in an ordered array k( Two points search )

 

Binary search is also called binary search (Binary Search), It is an efficient search method . however , Half search requires that linear table must adopt sequential storage structure , And the elements in the table are arranged in order by keywords .
The basic idea of binary search is to n The elements are divided into roughly equal parts , take a[n/2] And x compare , If x=a[n/2], Then we find x, Algorithm abort ; If x<a[n/2], Then just in the array a The left half of the search continues x, If x>a[n/2], Then just in the array a Search the right half of x. Last , When we find the element, print it and then jump out . If you don't find it, jump out of the loop .

Question 28 → Use for Circular statements show 10 A random number ! Printing produces random numbers 1~100~

 

stay C In language , We usually use  <stdlib.h>  In the header file  rand()  Function to generate a random number , Its usage is :
int rand (void); Then the random number we need to generate in the above program is 1~100 How to generate this , It's actually easy . We don't know rand() The maximum value generated is 0~32768, So in this topic, we need to generate 1~100 In other words, we need a method to generate a certain random value , The modulo operator can be used at this time . The example is shown in the following code :

int ret = rand() % 10; // produce 0~9 The random number  -  Be careful 10 It's divisible
Of course, if you take a mold here 100 It just produces 0~99 The number of , You have to +1 That's it 1~100 The numbers between the ranges just match the requirements of the topic . therefore , In this topic, we actually use modulo operator to solve this problem .

Use  <time.h>  In the header file  time()  Function to get the current time ( Accurate to seconds ), It looks like this :
srand((unsigned)time(NULL)); Usually you only need to quote once !

Question 29

Print out the pyramid

Printing pyramids is nothing more than using for Loop nesting , When we enter numbers 5 When , Let's assume a running result of it to see if it is helpful for us to solve the problem ↓

 *
 ***
 ***** 
 *******
*********

The above is the input number 5, The printed pyramid . From this running result, it will be much easier for us to do the problem again .  If you don't understand its operation steps, you can debug more , Debugging can really help you solve many problems , Especially if you just learned C Language people or beginners , Even Daniel , All need debugging . It can be said that a person's time to write code may be only... Percent 30~40, While most people spend percent of their time writing and debugging code 60~70 了 .

Question 30

Enter two numbers , Find their greatest common divisor

The greatest common factor , Also known as the greatest common divisor 、 The greatest common factor , The largest of the common divisors of two or more integers .
a,b The greatest common divisor of is (a,b), alike ,a,b,c The greatest common divisor of is (a,b,c), The greatest common divisor of multiple integers has the same notation . There are many ways to find the greatest common divisor , There is a common factorization method 、 Short division 、 division 、 More derogation . The concept corresponding to the greatest common divisor is the least common multiple ,a,b The least common multiple of is [a,b].
This is also in mathematics. We often use short division .


Question 26 code

for The use of recycling ↓

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main(void)
{
 int i = 0;
 int j = 0;
 int sum = 1;
 printf(&quot; Please enter a number :&quot;);
 scanf(&quot;%d&quot;, &j);

 for (i = 1; i <= j; i++)
 {
 sum = sum * i;//sum  Realization N The sum of the factorials of
 }
 printf(&quot;sum = %d\n&quot;, sum);
 return 0;
}

while The use of recycling ↓
Running results

Possible running results  
Please enter a number :5
sum = 120


Question 27 code

#include<stdio.h>
int main(void)
{
 int k = 7;
 int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 int sz = sizeof(arr) / sizeof(arr[0]); 
 int left = 0; 
 int right = sz - 1; 

 while (left <= right) 
 {
 int mid = (left + right) / 2; 
 if (arr[mid] < k)
 {
 left = mid + 1;
 }
 else if (arr[mid] > k)
 {
 right = mid - 1;
 }
 else
 {
 printf(&quot; eureka , The array subscript :%d, Elements %d\n&quot;, mid,arr[mid]);
 break;
 }
 }
 if (left > right)
 {
 printf(&quot; Can't find !\n&quot;);
 }
 return 0;
}

Running results
eureka , The array subscript 6, Elements 7

Question 28 code

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM 10
int main(void)
{
 int i = 1;
 srand((unsigned)time(NULL));
 for (i = 1; i <= NUM; i++)
 {
 int ret = rand() % 100 + 1;
 printf(&quot; The first %-2d Time : Numbers -->%d\n&quot;, i, ret);
 }
 return 0;
}

Running results
The first 1  Time  :  Numbers -->13
The first 2  Time  :  Numbers -->74
The first 3  Time  :  Numbers -->71
The first 4  Time  :  Numbers -->42
The first 5  Time  :  Numbers -->23
The first 6  Time  :  Numbers -->10
The first 7  Time  :  Numbers -->10
The first 8  Time  :  Numbers -->47
The first 9  Time  :  Numbers -->11
The first 10 Time :  Numbers -->46


Question 29 code

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main(void)
{
 int i, j, k;
 int input;
 printf(&quot; Please enter a number :-->&quot;);
 scanf(&quot;%d&quot;, &input);
 for (i = 1; i <= input; i++)
 {
 // Ideas : Space  &  Print  *
 for (j = 1; j <= input - i; j++)
 {
 printf(&quot; &quot;);
 }
 for (k = 1; k <= 2 * i - 1; k++)
 {
 printf(&quot;*&quot;);
 }
 printf(&quot;\n&quot;);
 }
 return 0;
}

Running results
Please enter a number :-->5

 *
 ***
 ***** 
 *******
*********


Question 30 code

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
 int a = 0, b = 0, t = 0;
 printf(&quot; Please enter two numbers :&quot;);
 scanf(&quot;%d %d&quot;, &a, &b);
 while (t = a%b)
 {
 a = b;
 b = t;
 }
 // hypothesis :a = 10、b = 20
 //t = 10 
 printf(&quot;|--------------------|\n&quot;);
 printf(&quot;| The greatest common divisor of two numbers :%d|\n&quot;,b);
 printf(&quot;|--------------------|\n&quot;);
 return 0;
}

Running results
Please enter two numbers :10 20
The greatest common divisor of two numbers :10



This series has not been updated. It has been more than three months since the last article , In fact, I have been writing this series. After all, I can brush it myself C Some topics of language, and then summarize these brushed topics. Write a blog with five topics , But there are many things left behind , Now I remember ( •̀ .̫ •́ )*

null
You don't know these five questions (^∀^●)ノシ
It's said that long pressing the collection button will surprise you. You might as well make it ヾ(^▽^*)))

null


原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/184/202207030920453236.html