当前位置:网站首页>Niuke C basic exercises
Niuke C basic exercises
2022-07-23 22:30:00 【< vince>】

There are times when you can ride the wind and break the waves , Sail the clouds straight to the sea .
List of articles
- Topic 1 : Niuniu line segment
- Topic two : Pass score
- Topic three : Judge integer parity
- Topic four : Determine whether vowels or consonants
- Topic 5 : Niuniu's judgment question
- Topic 6 : Judgement of leap year
- Topic 7 : Judging letters
- Topic 8 : The four seasons
- Topic nine : Health assessment
- Topic ten : Xiaolele looks for the largest number
Preface
Hello, everyone ! I am a vince, In the process of learning programming language, you should find , The theoretical basis is very important , Then it is more important to actually operate the machine , The computer operation is nothing more than writing project code , More of course is to brush questions ~ Then at this time, a good question brushing website is really very important , It will make you go in the right direction , Clear learning ideas , therefore , ad locum Recommend a study to the little friends who love learning 、 The website of question brushing, Niuke , There are all kinds of interview questions , It can really bring you a good learning experience .
Study loving relatives ! Please click on me to register ! Study 、 Brush problem
The following is the Niuke question bank interface :
Next , Let's get down to business , Let's take a look at some related examples .
Text
Topic 1 : Niuniu line segment
describe Niuniu has a line segment placed in a two-dimensional coordinate system , The coordinates of two points of a given line segment (x1,y1),(x2,y2) . Niuniu wants to know the square of the length of this line segment
Input description : First line input x1 and y1, Space off . Second line input x2 and y2, Space off . among x1 , y1 ,x2 ,y2 Are integers.
Output description : The square of the length of the output line segment
The code is as follows :
#include <stdio.h>
#include <math.h>
int main(void)
{
int x1 = 0, x2 = 0;
int y1 = 0, y2 = 0;
scanf("%d %d",&x1, &y1);
scanf("%d %d",&x2, &y2);
int ret = pow((x1-x2),2)+pow((y1-y2),2);
printf("%d\n",ret);
}
summary :
Note that you need to examine the question carefully , The problem is to find its square .
Topic two : Pass score
describe KiKi I want to know if he passed the exam , Please help him judge . Enter a fraction represented by an integer from the keyboard , Program to determine whether the score is within the pass range , If you pass , namely : A score greater than or equal to 60 branch , It's output “Pass”, otherwise , Output “Fail”.
Input description : Multi group input , Each line of input includes a fraction represented by an integer (0~100).
Output description : Enter... For each line , Output “Pass” or “Fail”.
The code is as follows :
#include <stdio.h>
int main(void)
{
int n = 0;
while(scanf("%d",&n)!=EOF)
{
if(n>=60)
{
printf("Pass\n");
}
else
{
printf("Fail\n");
}
}
}
summary :
Start here C The branch structure of , Slowly walk into C The theme of !
There are multiple sets of inputs for this question , So pay attention when typing the code !
Topic three : Judge integer parity
describe KiKi Want to know the parity of an integer , Please help him judge . Enter any integer from the keyboard ( Range -231~231-1), Program to judge its parity .
Input description : Multi group input , Each line of input includes an integer .
Output description : Enter... For each line , The output number is odd (Odd) Or even (Even).
The code is as follows :
#include<stdio.h>
int main()
{
int n=0;
while(~scanf("%d",&n))
{
if(n%2==0)
{
printf("Even\n");
}
else
{
printf("Odd\n");
}
}
return 0;
}
Topic four : Determine whether vowels or consonants
describe KiKi Start learning English letters ,BoBo The teacher told him , There are five letters A(a), E(e), I(i), O(o),U(u) Called vowels , All other letters are called consonants , Please help him write a program to judge whether the input letter is a vowel (Vowel) Or consonants (Consonant).
Input description : Multi group input , Enter one letter per line .
Output description : Enter... For each group , Output as a line , If the input letter is a vowel ( Include case ), Output “Vowel”, If the input letter is a non vowel , Output “Consonant”.
The code is as follows :
#include<stdio.h>
#include<ctype.h>
int main()
{
char letter = 0;
while (scanf(" %c", &letter)!=EOF)//%c If a space is added in front of it, it is to eliminate carriage return
{
//getchar();// If there is no preceding space, eliminate carriage return , Just use getchar Eliminate spaces
letter = toupper(letter);// This function converts lowercase letters to uppercase letters
switch (letter)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':printf("Vowel\n"); break;
default:printf("Consonant\n");
}
}
return 0;
}
summary :
There are two strange knowledge points in this problem :
1、 First comes the use of getchar() Function to eliminate carriage returns , Here to %c Adding a space in front also meets the format requirements of this topic , But we need to learn getchar() Function to eliminate the use of carriage return !
2、 It's here toupper() function , The function is : Convert lowercase letters to their corresponding uppercase letters .
Topic 5 : Niuniu's judgment question
describe Niu Niu inputs an integer from the keyboard x And the left and right boundaries l and r Three integers in total . Please judge x Whether in l and r Between ( That is, whether there is l≤x≤r )
Input description : Input in sequence x ,l ,r Three integers . Space off .
Output description : If there is l≤x≤r The output true , Otherwise output false
The code is as follows :
#include <stdio.h>
int main(void)
{
int x = 0, l = 0, r = 0;
scanf("%d %d %d",&x,&l,&r);
if(x >= l && x <= r)
printf("true\n");
else
printf("false\n");
}
Topic 6 : Judgement of leap year
describe Judge an integer n Is it a leap year
Input description : Enter an integer n (1≤n≤2018)
Output description : Is leap year output "yes" Otherwise output "no"
The code is as follows :
#include<stdio.h>
int main(void)
{
int n = 0;
scanf("%d",&n);
if(n % 4 == 0 && n % 100 != 0 || n % 400 == 0)
printf("yes\n");
else
printf("no\n");
}
summary :
In this problem, we need to remember the conditions for judging leap years :(n % 4 == 0 && n % 100 != 0 || n % 400 == 0).
Topic 7 : Judging letters
describe Enter any character from the keyboard , Program to determine whether it is a letter ( Include case ).
Input description : The input includes one character .
Output description : The output character is a letter (YES) Or not (NO).
The code is as follows :
#include<stdio.h>
int main(void)
{
char n = 0;
scanf("%c", &n);
if (n >= 'a' && n <= 'z' || n>='A' && n <= 'Z')
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
Topic 8 : The four seasons
describe In the meteorological sense , Usually, the 3~5 The month is spring (spring),6~8 The month is summer (summer),9~11 The month is autumn (autumn),12 month ~ Coming year 2 The month is winter (winter). Please enter the year and month according to , Output the corresponding season .
Input description : The input data format is fixed YYYYMM In the form of , namely : The year accounts for 4 One digit , Month share 2 One digit .
Output description : Output the season corresponding to the month ( Use English words to express , All in lowercase ).
The code is as follows :
#include <stdio.h>
int main(void)
{
int m = 0;
scanf("%d",&m);
int n = m % 100;
switch(n)
{
case 3:
case 4:
case 5:printf("spring\n");break;
case 6:
case 7:
case 8:printf("summer\n");break;
case 9:
case 10:
case 11:printf("autumn\n");break;
default:printf("winter\n");
}
}
Topic nine : Health assessment
describe BMI Index ( Body mass index ) It's the number of kilograms divided by the square of meters of height , At present, it is a standard commonly used in the world to measure the degree of weight and health . for example : A person's height is 1.75 rice , The weight is 68 kg , His BMI=68/(1.752)=22.2( kg / rice 2). When BMI Index is 18.5~23.9 Time is normal , Otherwise, it means that there is a health risk to the body . Program to judge human health .
Input description : a line , Enter a person's weight ( kg ) And height ( rice ), Separated by a space .
Output description : a line , Output body Normal( normal ) or Abnormal( Is not normal ).
The code is as follows :
#include<stdio.h>
int main(void)
{
double m = 0,n = 0;
double BMI = 0;
scanf("%lf %lf",&m,&n);
BMI = m / (n*n);
if(BMI >= 18.5 && BMI <= 23.9)
{
printf("Normal\n");
}
else
{
printf("Abnormal\n");
}
}
Topic ten : Xiaolele looks for the largest number
describe Xiaolele gets 4 The maximum number , Please help him program to find the maximum number .
Input description : a line ,4 It's an integer , Separate with spaces .
Output description : a line , An integer , For input 4 The largest of the integers .
Code (1) as follows :
#include <stdio.h>
int main(void)
{
int n = 0;
int max = 0;
while(scanf("%d",&n) != EOF)
{
if(max < n)
{
max = n;
}
}
printf("%d\n",max);
}
Code (2) as follows :
Use the combination of array and ternary operator to judge
#include <stdio.h>
int main(void)
{
int arr[4] = {
0};
for(int i = 0; i < 4; i++)
{
scanf("%d",&arr[i]);
}
int max = 0;
for(int i = 0; i < 4; i++)
{
max = max < arr[i] ? arr[i] : max;
}
printf("%d\n",max);
}
Conclusion
A good learning environment is really very important , This is of great help to your study , Niuke is one of the good learning environments , If you haven't experienced it yet, go and experience it Study loving relatives ! Please click on me to register ! Study 、 Brush problem
Niuke review exercise , Coders must type more code , Step back from the basic question type , From qualitative change to quantitative change , come on. !
I also hope you can find some inspiration from these topics , If there are deficiencies or other methods , Please type in the comment area ~ learn from each other , Make progress with each other ~
Goodbye !
Thank you for reading !
边栏推荐
猜你喜欢

海外资深玩家的投资建议(3) 2021-05-04

STM32单片机使用ADC功能驱动手指检测心跳模块

Rails with OSS best practices

Multithreading problem: why should we not use multithreading to read and write the same socket connection?

Introduction to I2C Principle & Application of esp32

Machine learning exercises -- right rate regression

Leetcode high frequency question 53. maximum subarray sum, continuous subarray with maximum sum, return its maximum sum

疯狂的牛市,下半场何去何从?2021-04-30

JS——事件代理和应用场景

Record the process of the first excavation and intersection
随机推荐
小说里的编程 【连载之十七】元宇宙里月亮弯弯
Financial products with an annual yield of 6%
如何徹底强制殺死後臺無關進程?
Multithreading problem: why should we not use multithreading to read and write the same socket connection?
【AcWing】周赛
关于电脑端同步到手机端数据
MySQL index transaction
TreeMap
除了钱,创业者还需要什么?专访明月湖创赛创投机构
Programming in the novel [serial 19] the moon bends in the yuan universe
大淘营批量采集商品,如何将未上传的宝贝保存下来等后面再导入采集上传
糖尿病遗传风险检测挑战赛Baseline
Ali onedate's layered thought
人生总需要一点激情
【golang学习笔记】并发基础
Programming in the novel [serial 17] the moon bends in the yuan universe
Basic syntax of MySQL DDL and DML and DQL
LeetCode高频题53. 最大子数组和,具有最大和的连续子数组,返回其最大和
Array - 704. Binary search
产品生命周期,常见的项目职能,信息流 都是什么











