检查整数是否可被11整除的程序错误 [英] Error in program to check whether integer is divisible by 11 or not

查看:85
本文介绍了检查整数是否可被11整除的程序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关我的C ++程序的帮助.
需要编写一个程序检查整数是否可以被11整除.这是我的代码的副本.尽管运行结果不正确,但运行没有错误

I Need Help Concerning My C++ Program.
In Need To Write A Program That Checks Whether An Integer Is Divisible By 11 Or Not. Here Is A Copy Of My Code. It Runs Without Errors Though It Gives Wrong result

#include 
#include 
using namespace std;
int n, a, power=0, t=0;
bool divisible (int)
{
	a = abs (n);
	if (a<11)
		return false;
	else if (a=11)
		return true;
	else 
		while ((a/(static_cast<removed int="">(pow(10.0, power)))>=1))
		{
			
			if (((a/(static_cast<removed int="">(pow(10.0, power))))<10))
				switch (power % 2)
			{
				case 0:
				t+=(a/(static_cast<removed int="">(pow(10.0, power))));
				break;
				case 1:
				t-=(a/(static_cast<removed int="">(pow(10.0, power))));
				break;
			}
			
			else 
				switch (power % 2)
			{
				case 0:
				t += ((a/(static_cast<removed int="">(pow(10.0, power))))%10);
				break;
				case 1:
				t-= ((a/(static_cast<removed int="">(pow(10.0, power))))%10);
				break;
			}
			
			power++;
		}
		if (t%11 == 0)
			return true;
		else if (divisible (t) != 0)
			return false;
		
		
}


void main ()
{ 
	cout<<"Enter an integer \n";
	cin>>n;
	switch (divisible (n))
	{
	case 1: cout<<"The number "<<n<<" is divisible by 11 \n";
		break;
	case 0: cout<<"The number "<<n<<" is not divisible by 11 \n";
		break;
	}
}



[将问题的更多描述张贴在此处粘贴为答复]
这个问题要求我们使用指定的公式t = a0 -a1 + a2-a3 ... +(-1)kak ..因此,如果您希望查看我发布的代码,我将不胜感激..谢谢

问题是:

1.令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整除.编写一个程序以测试您的函数.



[Further description of problem posted as reply pasted here]
The question requires us to use the specified formula t = a0 -a1 + a2 - a3...+(-1)kak.. so i would appreciate it if you rather check my posted code .. thanks

The question is:

1. 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.

推荐答案

它给出了错误的结果"既不是描述性的也不是信息性的.您可以举出一些案例,这些案例行之有效,而其他案例则行不通.
您可能已经说过它几乎总是返回true".

您可能自己也找到了一种模式.

否则,如果(a = 11)..."
嗯,C和C ++提供了很好的旧运算符陷阱.

:)
"It Gives Wrong result" isn''t descriptive nor informative. You could have given examples of cases that work well and others that don''t.
You could have said "It almost always returns true".

And you could have found a pattern yourself.

"else if (a=11)..."
Ah the good old operator traps provided by C and C++.

:)


A(非常慢)解决方案:-D:
A (very) slow solution :-D :
int TakeByIndex(int iTest, int iIndex)
{
  return (iTest % pow(10, iIndex + 1)) / pow(10, iIndex); 
}
 

bool IsDivisibleBy11(int iTest)
{
  int a = 1,
      k = 1,
      i = 0,
      s = 0;
 

  while (k && a) {
    a = TakeByIndex(iTest, i++);
    k = TakeByIndex(iTest, i++);
    s += (a - k);
  }
 

  return (0 == s % 11);
}


尝试一下:):
Try it :) :
bool IsDivisibleBy11(int iTest)
{
  return (0 == iTest % 11);
}


这篇关于检查整数是否可被11整除的程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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