我已经使用了这个代码,它运行良好,但我不知道它是如何工作的。 [英] I have used this code which is working good but I dont know how it works.

查看:66
本文介绍了我已经使用了这个代码,它运行良好,但我不知道它是如何工作的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< iostream>

#include< cmath>

使用命名空间std;

int main()

{

unsigned int n = 1234;

int a = n;

cout<<原始数字是< < a<< endl;

n = n%(unsigned int)pow(10.0,(double)floor(log10(n)));

cout<< ;申请代码后<< n;

返回0;

}

第一个cout是1234,第二个是234我是需要,但我不知道它是如何工作的,因为我是一个初学者。 :)



我尝试过:



:P

解决方案

这很简单:'%'运算符是模数 - 当除以y时,它返回x的余数:

 modulus = x%y 

因此,如果x为123且y为10,则模数为3:

 3 = 123%10 

因为123除以10是12余数3.

在你的代码中,你的原始数字是1234,除数是1000(检查数学而且很明显)。所以结果是1234的剩余部分除以1000,这是234


实际上它与软件开发没有任何关系,但更多的测试是看你如何能够跟随某些东西,并将其分开...

  //  您的行分为两个 
unisgned int d =( unsigned int )pow( 10 0 ,()地板(日志10(N)));
n = n%d; // 这是一个简单的模数(除法后提醒)运算符,如果n是234,则d必须为1000

// d如何成为1000 !!!
// (unsigned int)pow(10.0,(double)floor(log10(n)))
// log10(1234)=> 3.09123
// floor(3.09)=> 3
// pow(10,3)=> 1000





我会说,无论如何你不理解一个看似复杂的表达,把它分开......

这是一个很好的做法,当你学习新的东西或必须分析一些问题...


使用计算器手动执行步骤:



  1. log10(1234)= 3.0913
  2. floor(3.0913)= 3.0
  3. pow(10.0,3.0) )= 1000.0
  4. (unsigned int)1000.0 = 1000
  5. 1234%1000 = 234



关于操作,请参阅

log10 - C ++参考 [ ^ ]

floor - C ++参考 [ ^ ]

pow - C ++参考 [ ^ ]

类型转换( unsigned int ):类型转换 - C ++教程 [ ^ ]

Modulo运算符操作员 - C ++教程 [ ^ ]


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
unsigned int n = 1234;
int a=n;
cout<<"Original number is "<<a<<endl;
n = n % (unsigned int)pow(10.0, (double)floor(log10(n)));
cout<<"After applying code "<<n;
return 0;
}
first cout is 1234 and 2nd one 234 which i needed but i dont know how it works as i am a beginner. :)

What I have tried:

:P

解决方案

It's simple: the '%' operator is the Modulus - it returns the remainder of x when divided by y:

modulus = x % y

So if x was 123 and y was 10, the modulus would be 3:

3 = 123 % 10

because 123 divided by ten is 12 remainder 3.
In your code, your original number id 1234, and the divisor is works out to 1000 (check the maths and it's obvious). So the result is the remainder of 1234 divided by 1000, which is 234


Actually it has nothing to do with software development, but much more a test to see how you are able yo follow something, and break it apart...

// your line break into two
unisgned int d = (unsigned int)pow(10.0, (double)floor(log10(n)));
n = n % d; // this is a simple modulus (reminder after division) operator, and if n is 234 than d must be 1000

// the question how d became 1000!!!
// (unsigned int)pow(10.0, (double)floor(log10(n)))
// log10(1234) => 3.09123
// floor(3.09) => 3
// pow(10, 3) => 1000



I would say that in any case you do not understand a seemingly complicated expression, break it apart...
It is a good practice when you learn something new or have to analyse some problem...


Use a calculator to perform the steps manually:


  1. log10(1234) = 3.0913
  2. floor(3.0913) = 3.0
  3. pow(10.0, 3.0) = 1000.0
  4. (unsigned int)1000.0 = 1000
  5. 1234 % 1000 = 234


Regarding the operations see
log10 - C++ Reference[^]
floor - C++ Reference[^]
pow - C++ Reference[^]
Type casting (unsigned int): Type conversions - C++ Tutorials[^]
Modulo operator %: Operators - C++ Tutorials[^]


这篇关于我已经使用了这个代码,它运行良好,但我不知道它是如何工作的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆