C ++程序输出数字 [英] C++ Program to output a number

查看:92
本文介绍了C ++程序输出数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让n = akak-1ak-2 ... a1a0为整数,t = a0 – a1 + a2-…+(-1)kak.已知当且仅当t可被11整除时,n才可被11整除.例如,假设n =8784204.则t = 4 – 0 + 2 – 4 + 8 – 7 + 8 = 11.乘以11,则得出8784204可被11整除.如果n = 54063297,则t = 7 – 9 + 2 – 3 + 6 – 0 + 4 – 5 =2.因为2无法被11整除,所以54063297无法整除由11.
编写一个函数,该函数接受一个整数作为输入,并返回一个布尔值,该整数指示整数是否可以被11整除.编写一个程序来测试您的函数.

我在这里看到有人发布了相同的问题,但是他们正在使用C,而我必须使用C ++.

以下是我所拥有的,但未实现公式:
使用名称空间std

Let n = akak-1ak-2…a1a0 be an integer and t = a0 – a1 + a2 - … + (-1)kak. It is known that n is divisible by 11 if and only if t is divisible by 11. For example, suppose n = 8784204. Then t = 4 – 0 + 2 – 4 + 8 – 7 + 8 = 11. Since 11 is divisible by 11, it follows that 8784204 is divisible by 11. If n = 54063297, then t = 7 – 9 + 2 – 3 + 6 – 0 + 4 – 5 = 2. Because 2 is not divisible by 11, 54063297 is not divisible by 11.
Write a function that takes as input an integer and that return a Boolean indicating whether the integer is divisible or not by 11. Write a program to test your function.

I saw here someone posted the same question but they are using C and I have to use C++.

Below is what I have but it not implementing the formula:

#include using namespace std;
int main()
{
	int num;
	int divis;
	int sum;
	cout << "Enter a positive integer:";
		cin >> num;
	cout << endl;
	divis = num;
	sum = 0;
	do
	{ 
		sum = sum - num %10;
		num = num / 10;
		sum = sum + num %10;
		num = num/10;
	}
while (num >0);
cout <<"The sum of the integers:" << sum << endl;
if (sum % 11 == 0)
cout<< divis << " is divisible by 11" << endl;
else
cout << divis << " is not divisible by 11" <<endl;
}

推荐答案

我认为您还不了解这个问题.我可以为您提供基本的布局.尝试更多,然后在遇到问题时告诉我们.

1.在编写程序之前,请先了解要求.如果跳过此步骤,那么在实际编码时将面临许多困难.

2.了解要求后,准备所需的公式或逻辑.把它们写下来很有帮助.

3.在开始编码时,您必须对需要做的事情有透彻的了解,否则最终只会写出随机代码或不符合您目的的代码.

以下是一些伪代码可以帮助您入门:

I think you haven''t understood the question. I can help you out with a basic layout. Try more and then let us know when you are stuck.

1. Before writing your program, understand the requirements first. If this step is skipped, then you will face numerous difficulties when you actually code.

2. Having understood the requirements, prepare your formulae or logic required. Write them down it helps.

3. By the time you start coding, you must have a thorough understanding of what you need to do or you will only end up writing random code or code which doesn''t serve your purpose.

Here''s a little pseudo code to help you get started:

#include (include your header files here)

//main function
int main ()
{
  //Accept user input;
  //Call a function and pass the input value;
  //Depending on the value returned show the output requested;
}

//Actual function
return_type function_name(enter the parameter variable here)
{
  //Do the required task and find out whether the number is divisible or not;
  //If divisible by 11 then return true as value else return false;
}



您可以使用它开始.



You can use this to start off.


您的算法有点错误.第一个数字是加法,而不是减法.因此,您需要交换+和-操作的顺序.

这样看来我得到了正确的结果

Your algorithm is slightly wrong. The first number is added, not subtracted. hence you need to swap the order of the + and - operations.

With that I seem to be getting the correct results

#include <iostream>

using namespace std;

int main() {
	int num;
	int divis;
	int sum;
	cout << "Enter a positive integer:";
	cin >> num;
	cout << endl;
	divis = num;
	sum = 0;
	do { 
		sum = sum + num % 10;
		num = num / 10;
		//This could go inside an if (num > 0), but it really makes no differance
		sum = sum - num % 10;
		num = num / 10;
	} while (num > 0);
	cout << "The sum of the integers: " << sum << endl;
	if (sum % 11 == 0) {
		cout << divis << " is divisible by 11" << endl;
	} else {
		cout << divis << " is not divisible by 11" << endl;
	}
}


安德鲁(Andrew)的答案使用的是operator %(11),这在您的练习中可能是不允许的. 您必须改进代码以处理算法的各种结果:
Andrew''s answer uses operator %(11) which is probably unallowed in your exercise.
You must improve your code to handle various results from the algorithm:
#include <cmath> // for std::abs()
bool divisible_by_11(int n)
{
    n = std::abs(n);
    while (n > 11)       // handle cases like 9191919
    {
        int n1 = 0;
        while (n)
        {
            n1 += n % 10;   // add last digit of n to n1
            n /= 10;        // divide n by ten
            n1 -=  n % 10;  // substract last digit of n to n1
            n /= 10;        // divide n by ten
        }
        n = (n1 == 0) ? 11 : std::abs(n1); // handle cases like 3333 (n1 == 0) or 91919191 (n1 < 0)
    }
    return n == 11;
}


如果要在0上返回true,则必须进行一些更改:)
欢呼声,
AR


If you want to return true on 0 you have to make some changes :)
cheers,
AR


这篇关于C ++程序输出数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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