当前位置:网站首页>Daily question - xiaolele changes the number
Daily question - xiaolele changes the number
2022-07-02 17:39:00 【Protect Xiaozhou】
The topic comes from niuke.com
describe
Little Lele likes numbers , Especially like 0 and 1. He now has a number , I want to change the number of each person into 0 or 1. If a bit is odd , Just turn it into 1, If it's even , Then turn it into 0. Please answer what he finally got .
Input description :
Input contains an integer n (0 ≤ n ≤ 109)
Output description :
Output an integer , That is, the number obtained by xiaolele after modification .
Example 1
Input :
222222
Output :
0
Example 2
Input :
123
Output :
101
Thinking analysis :
First we enter an integer , Then we need to convert the even bits of the integer into 0, Odd digits become 1; Then it naturally involves retrieval conversion , Next, bloggers will share two ideas for solving problems .
The first is to use arrays , The advantage is data retrieval 、 It is easy to modify data , The disadvantage is that the program is cumbersome , Poor versatility , According to the instance , Make adjustments accordingly .
The second is making numbers , Through data retrieval , And then according to the question , Generate a new number , Until each item of the input integer is fetched , The new number is manufactured . Very easy to use .
Numeration method :
#include<stdio.h>
int main()
{
int a=0;// Integers a
int k=0;// Store the number of bits of each bit
int sum=0;// Make numbers
scanf("%d",&a);
// Define a i Used to control the data size of each bit , for example :123=1*100+2*10+3*1;
// The loop end condition is an integer a Everyone of has taken ;
//i*=10, amount to i=i*10; Because it's making numbers , So we should control the size of each bit
for(int i=1;a!=0;i*=10)
{
// And 10 You can get the last one by finding the module
k=a%10;
// Judge the parity of this bit
if(k%2==0)
{
k=0;// If it's even , Then this one is 0
sum+=k;
}
else
{
k=1;// If it's odd , Then this one is 1
sum+=k*i;// By riding i To adjust the size of digits , adopt sum Storage
}
// Divide 10, Remove the last digit from the approximate number , So we can judge the next
a=a/10;
}
// Print the number of manufacturing sum
printf("%d",sum);
return 0;
}
Array method :
#include<stdio.h>
int main()
{
int i = 0, j = 0;
int t[16] = { 0 };
int n;
float sum = 0;
scanf("%d",&n);
while (n)
{
// Judge parity , And the corresponding treatment
if (n % 2 == 0||n==0)
{
t[i] = 0;
sum += t[i];
}
else
{
t[i] = 1;
sum += t[i];
}
++i;
n = n / 10;
}
// Every bit of the data is the same ( identical ) The situation of
if (sum /( i +1)== t[0])
printf("%d", t[0]);
// Data starts with 0, We output non 0 Future data
else if (t[i] == 0)
{
for (j = i; j >=0; j--)
{
if (t[j] % 2 != 0)
while (j >=0)
{
printf("%d", t[j]);
j--;
}
}
}
// Print the converted data
else
while (i>=0)
{
printf("%d",t[i]);
--i;
}
return 0;
}
The topic comes from niuke.com :
Little Lele changed the number _ Niuke Tiba _ Cattle from
Thank you for watching this article , Please look forward to : Protect Xiao Zhou ღ
If there is infringement, please contact to modify and delete !
边栏推荐
- Chrome browser quick access stackoverflow
- From collection to output: inventory those powerful knowledge management tools - inventory of excellent note taking software (4)
- Vscode knowledge points - Common Errors
- Vscode + eslint configuration
- 【目标跟踪】|数据集汇总
- The construction of scalable distributed database cluster and the partition design of oneproxy sub database
- USB interface powered Bluetooth color light strip controller
- Sword finger offer 24 Reverse linked list
- Eth data set download and related problems
- Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
猜你喜欢
Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!
Microservice architecture practice: Construction of highly available distributed file system fastdfs architecture
easyswoole3.2重启不成功
VirtualLab基础实验教程-7.偏振(1)
Experience home office, feel the completion of the project | community essay solicitation
Platform management background and merchant menu resource management: merchant role management design
Sword finger offer 24 Reverse linked list
[非线性控制理论]8_三种鲁棒控制器的比较
Win10 system uses pip to install juypter notebook process record (installed on a disk other than the system disk)
Si446 usage record (I): basic data acquisition
随机推荐
PCL知识点——体素化网格方法对点云进行下采样
After meeting a full stack developer from Tencent, I saw what it means to be proficient in MySQL tuning
POJ - 1458 Common Subsequence(最长公共子序列)
This "architect growth note" made 300 people successfully change jobs and enter the big factory, with an annual salary of 50W
visibilitychange – 指定标签页可见时,刷新页面数据
Income and risk of linear programming example investment
Nexus簡介及小白使用IDEA打包上傳到Nexus3私服詳細教程
Schoolbag novel multithreaded crawler [easy to understand]
What is agile development process
em120.gige.h
例题 非线性整数规划
How to quickly distinguish controlled components from uncontrolled components?
Chmod command principle and usage details [easy to understand]
chrome浏览器快速访问stackoverflow
One year is worth ten years
Configure lamp+supervisor
HDU - 1114 Piggy-Bank(完全背包)
Eth data set download and related problems
Alibaba Tianchi SQL learning notes - Day3
线性规划例题 投资的收益与风险