当前位置:网站首页>Programming exercises
Programming exercises
2022-07-18 07:06:00 【Smile Hun】
1. And then what time
Use four digits to represent time , The program should calculate the termination time according to the given time and elapsed time . The result is expressed in four digits .
notes : The second number indicates that the elapsed time may be greater than 60, It may also be less than 0
sample input :1120 120
sample output :1320
analysis
First convert the input value to Hours and minute Keep separately , Let the program distinguish between hours and minutes
Implementation code
scanf_s("%d %d", &time, &period);
hour1 = time / 100;
min1 = time % 100;
Here you are Be careful use scanf_s, Direct use scanf May lead to
1> The build project is complete “ Code testing .vcxproj” The operation of - Failure .
When calculating the end time , It is all converted into minutes , utilize C in , Divide an integer by an integer to get an integer The mechanism gets hours , Then check the total minutes 60 Of Remainder Get the minutes
Implementation code
hour2 = (hour1 * 60 + period) / 60;
min2 = (hour1 * 60 + min1 + period) % 60;
printf("%d:%d \n", hour2, min2);
final result
int main() {
int time, period, hour1, hour2, min1, min2;
scanf("%d %d", &time, &period);
hour1 = time / 100;
min1 = time % 100;
hour2 = (hour1 * 60 + period) / 60;
min2 = (hour1 * 60 + min1 + period) % 60;
printf("%d%d \n", hour2, min2);
return 0;
}
Output is 
Three digits in reverse order
Enter a three digit number and then output a three digit number in reverse order
notes : When the input number ends with 0 when , The output leading cannot be 0 Such as the input 700, Output is 7
analysis
The previous digit by digit reading is still used Integer divide by integer or Remainder complete .
The difficulty is how to complete the mantissa 0 Erasure of .
Solution :
- Add a loop statement to judge whether the mantissa is 0 ( More complicated )
int main() {
int number, first, second, third;
scanf_s("%d", &number);
first = number / 100;
second = (number / 10) % 10;// Here, you can first pair the read numbers 100 Remainder , Divided by 10, namely second = (number % 100) /10
third = number % 10;
if (third == 0) {
// Judge the third place first , That is, whether the single digit is zero , If zero , Then make the next judgment , If it is not zero, it will output hundreds, tens and bits in reverse order
if (second == 0) {
// Judge the second , That is, whether the tens are zero , If zero , Then make the next judgment , If not zero , Then ten bits and one bit are output in reverse order
if (first == 0) {
// Judge first , That is, whether the hundreds are zero , If it is zero, output “0”, If not zero , Then output the number on the hundreds
printf("0");
}
else {
printf("%d", first);
}
}
else {
printf("%d%d", second, first);
}
}else{
printf("%d%d%d", third, second, first);
}
return 0;
}
}
- To add , namely , Single digit 100+ Ten digits 10+ Hundreds of digits
int main() {
int number, first, second, third;
scanf_s("%d", &number);
first = number / 100;
second = (number / 10) % 10;
third = number % 10;
printf("%d", third*100 + second*10 + first);
return 0;
}
BCD Decrypt
BCD Number is a two digit decimal number expressed in one byte , Every four bits represent one bit , So if a BCD The hexadecimal number is 0x12, What he expresses is decimal 12, But Xiao Ming didn't learn BCD, Put all the BCD Numbers are converted into decimal output as binary numbers . therefore BCD Of 0x12 It is output as 18.
Now? , Your program needs to read this wrong decimal number . Tips : You can take 18 Convert to 0x12 Then it turns into 12.
Input format :
In the input line, give a [0,153] The integer of , It is guaranteed that it can be converted into BCD Count , That is to say, when this integer is converted to hexadecimal , There will be no A~F.
reading comprehension
When the input , Can't type , Such as 143( Hex is 0x0F) Such a number , Make sure that both digits of its hexadecimal number are numbers , It's not a letter .
Method 1:
int main() {
int number, first, second;
scanf_s("%d", &number);
first = number / 16;
second =number % 16;
printf("%d", first*10 + second);
return 0;
}
Method 2:
int main() {
int number;
scanf_s("%d", &number);
printf("%x", number); // %x Convert to binary output , And there is no previous “0x” sign
return 0;
}
The operation result is as shown in the figure :
边栏推荐
猜你喜欢
随机推荐
Excel导入导出注解通用版
php 生成excel表的2种方法
Exploration of yolact model structure
Mmdetection record of win10 installation
数据库存IP地址,用什么数据类型
Type-c边充边传数据应用OTG功能(LDR6028S)
Gridsearchcv reported an error: valueerror: input contains Nan, infinity or a value too large for dtype ('float64 ')
WM8960声卡相关问题
直播神器领夹式无线麦克风-LDR6028助力充电OTG方案
60 year old developer's suggestion, try to change it!
Array and string assignment problem (not initialized when defining)
免驱 USB 转串口 Billboard 芯片(LDR2001)
Thumbnail image processing class library
Rhcsa note 2
sensor 原理文章
复杂链表的复制
【面试必刷101】双指针
PHP 如何把二维数组变为 一维数组
[cloud native] 3.4 ruoyi cloud deployment practice (Part 1)
Type-C charging OTG chip (ldr6028s)








