当前位置:网站首页>1. Preliminary exercises of C language (1)
1. Preliminary exercises of C language (1)
2022-07-06 13:42:00 【It's Wang Jiujiu】
Catalog
TEXT 1( Functions and ternary operators )
TEXT 4( Quotient and mold taking )
TEXT 6( Set the domain width )
TEXT 9( Find the maximum number )
TEXT 1( Functions and ternary operators )
Write a function , Enter two different integers , Ask the larger one .
int max(int x, int y)// A return value is required , So set it to int, If no return value is required , You can set void
{
return (x > y ? x:y);// ternary operators ( Conditional operators )
}
#include<stdio.h>
int main()
{
int a = 0, b = 0;
scanf("%d%d", &a, &b);
printf(" The larger number is %d\n", max(a, b));
return 0;
}
Introduce the trinocular operator :
- Format of ternary operator ( expression 1? expression 2: expression 3)
- expression 1 It's true , Execute expression 2; If the expression 1 For false , Execute expression 3.
- (x > y ? x:y) It means : if x>y It's true , Then return to x; if x<y For false , Then return to y.
When a function needs to return a value , Set the function type to int, Again return One integer is enough . If the function doesn't need a return value , Set the function type to void that will do .
because a、b All integer variables , So will a、b The parameters of are passed to max Function time ,max Functions also need to use integers to receive , here a and b Is the argument ,x and y It's a parameter , A formal parameter is a temporary copy of an argument , Changing parameters does not change arguments , How to change arguments through formal parameters ? see TEXT 2.
TEXT 2( The ginseng )
Write a function , Exchange the values of two variables .
void change(int* px, int* py)// Because I'm wearing an address , So use a pointer to receive ,px、py Is a pointer variable
{
int i = 0;
i = *px; // Find... By address a、b Value , Indirect change a、b
*px = *py;
*py = i;
}
int main()
{
int a = 10, b = 20;
printf(" Exchange before :a=%d,b=%d\n", a, b);
change(&a, &b);// Use the address symbol , take a、b Pass the address of to the function
printf(" After exchanging :a=%d,b=%d\n", a, b);
return 0;
}
You can change the arguments through the address , Here we take the address symbol to a、b Send the address of to px、py, Because the parameter passed is an integer , Here we use integer pointer int* receive ,px、py It's a pointer variable , Stored a、b The address of .
hypothesis a The address for 0x0012ff40,b The address for 0x0012ff44, When we take the address symbol, we will a、b Send the address of to px、py after ,px、py What's in it is a and b The address of , By changing the data stored in the address , And then indirectly change a、b Value . Array parameter transfer , It's the address of the first element .
TEXT 3(return)
A function is known y=f(x), When x < 0 when ,y = 10; When x = 0 when ,y = 0; When x > 0 when ,y = -10
Enter an integer x(-10000<x<10000), Find the output value y.
#include<stdio.h>
int fun(int x)
{
if (x > 0)
return -10;
else if (x < 0)
return 10;
else
return 0;
}
int main()
{
int x = 0;
scanf("%d", &x);
printf(" When x=%d when ,y=%d", x, fun(x));
return 0;
}
In the defined function , If it contains multiple nesting , It can be used return Jump out of function , Go straight back to .
TEXT 4( Quotient and mold taking )
Given two integers a and b (0 < a,b < 10,000), Calculation a Divide b The integer quotient and remainder of .
#include<stdio.h>
int main()
{
int a = 0, b = 0;
scanf("%d%d", &a, &b);
printf("a/b=%d,a%%b=%d\n", a / b, a % b);// Want to print % need %%
return 0;
}
%% For escape characters , The function is to show %.
TEXT 5(ASCII code )
Characters represented by character constants or character variables are stored in memory as ASCII Code storage . Convert below ASCII Code for the corresponding characters and output them .73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33
#include<stdio.h>
int main()
{
char arr[13] = { 73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33 };
printf("%s\n", arr);
return 0;
}
putchar It can also be used. ASCII Code output corresponding characters , however putchar Only one character can be output at a time , It is troublesome to output strings .
TEXT 6( Set the domain width )
Enter a person's date of birth ( Including mm / DD / yyyy ), There is no separator between the number of year, year and day , Put the year in the birthday 、 month 、 Each day is exported separately .
#include<stdio.h>
int main()
{
int year = 0, month = 0, day = 0;
scanf("%4d%2d%2d", &year, &month, &day);// Intercept data by setting the domain width
printf("year=%d\nmonth=%d\nday=%d\n", year, month, day);
return 0;
}
By setting the domain allowance scanf Intercept data , for example %4d It means : Domain width is 4, The data in it will be stored in year in .
TEXT 7( Escape character )
Print... On the screen
printf("Hello world!\n");
cout << "Hello world!" << endl;
#include<stdio.h>
int main()
{
printf("printf\(\"Hello world!\\n\"\)\;\n");
printf("cout << \"Hello world!\" << endl;\n");
return 0;
}
When the content you want to print is confused with escape characters , Add a \ that will do , For example, I want to print a “\n”, It only needs “\\n” that will do .
TEXT 8(for loop )
Print 1-100 Between all 3 The number of multiples of
#include<stdio.h>
int main()
{
int num = 0;
for (num = 1; num <= 100; num++)
{
if (num % 3 == 0)
{
printf("%d ", num);
}
}
printf("\n");
return 0;
}
The code can also be written as :
#include<stdio.h>
int main()
{
int num = 0;
for (num = 3; num <= 100; num+=3)
{
printf("%d ", num);
}
printf("\n");
return 0;
}
Based on our understanding of Mathematics , You can directly set each print 3 Multiple , Omit the part of judgment .
TEXT 9( Find the maximum number )
get 4 Students' grades , Programming to find the highest score .
#include<stdio.h>
int main()
{
int arr[5] = { 0 };
int i = 0;
int max = 0;
for (i = 0; i < 4; i++)
{
scanf("%d", &arr[i]);
if (arr[i] > max)
{
max = arr[i];
}
}
printf(" The highest score is %d\n", max);
return 0;
}
Because the result can't be negative , So suppose the highest score is 0 branch , Judge every time you enter your grades , If it is higher than the current highest score , Then update the highest score .( The advantage of this method is that every time you input data, you will judge , When the input is completed, the result is also judged , Very simple and fast )
TEXT 10( Bubble sort )
Writing code will give 5 An integer number is output from small to large .
#include<stdio.h>
void change(int* px, int* py)// Exchange two numbers
{
int i = 0;
i = *px;
*px = *py;
*py = i;
}
int main()
{
int arr[6] = {7,8,1,2,6};
int i = 0;
int j = 0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 5-1-i; j++)
{
if (arr[j] > arr[j + 1])
{
change(&arr[j], &arr[j + 1]);
}
}
}
for (j = 0; j < 5; j++)
{
printf("%d ", arr[j]);
}
return 0;
}
The core idea of bubble sorting is the ratio of two adjacent numbers , Big stand back . The maximum value can be found in the first round of sorting , The second round of sorting can find the second largest value , When sorting goes to n-1 Wheel time , The order has been arranged .
边栏推荐
- 20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
- 2.C语言初阶练习题(2)
- 一段用蜂鸣器编的音乐(成都)
- 5. Function recursion exercise
- 8.C语言——位操作符与位移操作符
- 1.初识C语言(1)
- Redis cache obsolescence strategy
- 抽象类和接口的区别
- 透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
- (original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
猜你喜欢
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
canvas基础1 - 画直线(通俗易懂)
The overseas sales of Xiaomi mobile phones are nearly 140million, which may explain why Xiaomi ov doesn't need Hongmeng
2.C语言初阶练习题(2)
Change vs theme and set background picture
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
强化学习系列(一):基本原理和概念
4.二分查找
The latest tank battle 2022 full development notes-1
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
随机推荐
The difference between overloading and rewriting
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
Detailed explanation of redis' distributed lock principle
杂谈0516
Comparison between FileInputStream and bufferedinputstream
稻 城 亚 丁
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
4.二分查找
5.MSDN的下载和使用
仿牛客技术博客项目常见问题及解答(三)
Rich Shenzhen people and renting Shenzhen people
六种集合的遍历方式总结(List Set Map Queue Deque Stack)
20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
最新坦克大战2022-全程开发笔记-1
Leetcode.3 无重复字符的最长子串——超过100%的解法
Cloud native trend in 2022
1.C语言矩阵加减法
MPLS experiment
西安电子科技大学22学年上学期《基础实验》试题及答案
5. Download and use of MSDN