当前位置:网站首页>Bc116 xiaolele changed to digital

Bc116 xiaolele changed to digital

2022-06-23 01:41:00 'Dream_

( come from Cattle from Beginner programming training )

1. problem :

( Link up : Change the number

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 ≤ 10^9)

Output description :

Output an integer , That is, the number obtained by xiaolele after modification .

Example 1

Input :

222222

Output :

0

2. Ideas :

  First Enter an integer , According to the meaning , need Use / and % as well as while loop The whole number is divided into numbers for odd and even judgment (()%2 whether ==0), if Odd numbers make the new variable 1, Even numbers are 0 

secondly The main point is The presentation of the final number is an integer of the final variables of each digit , So you need an integer output at this time ( Instead of outputting as an array !), This Integer source On : The resulting number on each bit *pow And to present , and pow The power number is required ( Number of digits -1), The number of digits is count Record

3. Code :

#include<stdio.h>
#include<math.h>
int main()
{
	long int n = 0;
	//printf(" Please enter an integer n:");
	scanf("%ld", &n);
	//long int ret=Change(n);
	//printf(" The result is :");
	long int m = 0;
	long int sum = 0;
	long int ret = 0;
	float count = 0;
	while(n)// Use it carefully while  use for You need to know how many numbers an integer consists of   We need to ask for it again 
	{
		m = n % 10;// Start with the last one 
		n /= 10;
		if (0 == (m % 2))// even numbers - It can be 2 to be divisible by   Remainder is 0!
		{
			count++;
			 ret = 0;
			 sum += ret*(long int )(pow(10, (count - 1)));// Pay attention to the combination : No arrays   It is calculated in exponential form !!
			 //pow(float x,float y)
		}
		else
		{
			count++;
			ret = 1;
			sum += ret * (long int)(pow(10, (count - 1)));
		}
	}
	printf("%ld\n", sum);
	return 0;
}

4. Be careful :

pow(float x, float y)  float form , So this problem involves cast ! 

原网站

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