首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++嵌套循环,按幂递增基数

C++嵌套循环,按幂递增基数
EN

Stack Overflow用户
提问于 2015-12-29 07:39:43
回答 1查看 114关注 0票数 0

我需要些帮助。我想要达到我的图像result上所示的结果

第一个问题...我的老师犯错了吗? 2^10 = 1024,而不是2048...?!但是nvm..

我唯一的问题是超过100.000的数字-请帮帮忙

到目前为止,我的代码如下

代码语言:javascript
复制
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string>
#include <sstream>
#include <ostream>
#include <stdio.h>
#include <conio.h>
#include <fstream>

using namespace std;

int main() {
    int p;      // exponent
    float b;    // base

    cout << setw(4);
    for (b=1; b<11; b++) {
        cout << " | " << setw(12) << b;
    }
    cout << endl;
    for (b=1; b<=10; b++) {
        cout << b;  
        for (p=1; p<=10; p++) {
            cout << " | " << setw(12) << pow(b, p);
        }
        cout << endl;
    }
    return 0;
}

它的输出是here

请帮帮忙,

诚挚的问候!

EN

回答 1

Stack Overflow用户

发布于 2015-12-29 07:54:32

如果问题是输出,那么最简单的做法是将pow()返回值转换为long long或编译器的64位int类型。

这将自动使用operator <<long long重载,这将有效地删除科学记数法。

代码语言:javascript
复制
 for (p=1; p<=10; p++) {
    cout << " | " << static_cast<long long>(pow(b, p));

Live Example

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34502393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档