错误简单输出C ++ [英] Error simple output C++
本文介绍了错误简单输出C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好帮助简单的输出程序..
当int非常大是错误的时候..
还有更好的解决方案吗?
喜欢
n = 1000000000000
k = 1000000000001
这将是错误
我尝试过的事情:
hello help simple output program..
when the int very large is error..
is there a better solution?
like
n = 1000000000000
k = 1000000000001
this will be error
What I have tried:
long long n;
long long k;
unsigned int count = 0;
cin >> n >> k;
for(int i = 1; i <= n; i++)
{
for(int y = i+1; y <= n; y++)
{
if(i + y == k)
{
count = count + 1;
}
}
}
cout << count << endl;
return 0;
推荐答案
在大多数平台上,C / C ++类型unsigned int
是32位宽。这意味着最大值。十进制值为4,294,967,295,小于1,000,000,000,000。
如果需要更大的值,请使用64位值:unsigned long long
或uint64_t
(包括 cstdint 与C ++ 11或 stdint.h )。
On most platforms the C/C++ typeunsigned int
is 32-bit wide. That means the max. decimal value is 4,294,967,295 which is less than 1,000,000,000,000.
If you need larger values use 64 bit values:unsigned long long
oruint64_t
(include cstdint with C++11 or stdint.h).
你对n和k使用long long类型,它也需要用于i,y和k,因为正如Jochen写的那样,在达到n和n之前将达到计数限制k。
You used the long long type for n and k and it also needs to be used for i, y, and k because, as Jochen wrote, the limit for count will be reached before it is reached for n and k.
你知道有不同类型的整数,大小不同。
你需要保持一致,你不能有n
和k
作为long long类型并期望使用int作为计数器,其中n为循环限制。
2个嵌套循环非常简单,因此非常粗暴。
拿一张纸和一支铅笔用小值解决问题,编写每个解决方案或更改你的代码,为每个解决方案打印i和j。
做一点分析,你应该很容易删除内循环。
进一步分析,你可以删除两个循环并找到一个数学公式,直接给你答案。
You know that there is different types of integers, different sizes.
You need to be consistent, you can't haven
andk
as long long type and expect to use ints as counters with n as the loop limit.
The 2 nested loops are very simple minded, and thus brute force.
Take a sheet of paper and a pencil and solve the problem with small values, write each solution or change your code to print i and j for each solution.
do a little analyze, you should easily be able to remove the inner loop.
with further analyze, you may be able to remove both loops and find a mathematical formula which will give you directly the answer.
这篇关于错误简单输出C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文