什么是输出?我想输出sholud是4但它给0? [英] What will be the output? I want output sholud be 4 but it is giving 0?

查看:84
本文介绍了什么是输出?我想输出sholud是4但它给0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include<math.h>
using namespace std;
int main() 
{
	//code
	int n,d,num,count,sum;
    num=13;
    d=2;
    sum=0;
    n=num;
	while(n>0)
	{
		sum=sum+n%10;
		n=n/10;
    }
    
	int diff=0;
	count=0;
	
   for(int i=0;diff>d;i++)
	{
		diff=num-sum;
		count++;
		num--;
		sum--;
	}

	printf("%d",count);
	return 0;
}





我的尝试:



我试过这个问题。它应该给出答案4.

refrence 具有特定差异的所有数字|实践| GeeksforGeeks [ ^ ]

推荐答案

好吧,如果你无法发现它 - 而且非常明显 - 使用调试器快速查看会告诉你问题的确切位置。



在函数的第一行放置断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一种新的(非常非常有用的)技能:调试!



但是我会给你一个提示:当你进入循环时, diff d 中的值是什么?
Well, if you can't spot it - and it's pretty obvious - a quick look with the debugger would have shown you exactly where the problem is.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

But I'll give you a hint: What are the values in diff and d when you enter your loop?


有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于什么是输出?我想输出sholud是4但它给0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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