输出C++USACO中出现意外的表情符号/笑脸

Unexpected emoticons/smileyes in output C++ USACO

本文关键字:符号 笑脸 意外 C++USACO 输出      更新时间:2023-10-16

我正试图从USACO解决以下问题,我的代码产生了正确的输出,但它打印的是表情符号/符号,而不是实际数字。也就是说,它不是打印1,而是打印"Alt+1"=☺.我使用的是CodeBlocks IDE。

问题:回文是向前读和向后读相同的数字。12321是一个典型的回文。

给定一个以B为基数的数(2<=B<=20为基数10),打印所有整数N(1<=N<=300为基数10;同时打印回文平方的值。使用字母"A"、"B"等来表示数字10、11等

在基数B中打印数字及其平方。输入格式基线为B的单线样本输入

10

输出格式以B为底的两个整数的行。第一个整数是其平方为回文的数;第二个整数是平方本身。样本输出

1 1

2 4

3 9

11 121

22 484

26 676

101 10201

111 12321

121 14641

202 40804

212 44944

264 69696

我的代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
bool palindrome(string input)
{
    string reversed = string ( input.rbegin(), input.rend() );
    if(reversed==input)
        return true;
    else return false;
}
string base(int square, int B)
{
    string output="";
            while(square>0)
            {
               int remainder = square % B;
                square /= B;
                if (remainder > 9)
                        output+=char('A'  + remainder - 10);
                else
                    output+=remainder;
            }
        return string(output.rbegin(), output.rend() );
 //convert number to another base
}
int main()
{
    int B;
cin>>B;
for(int i = 1; i <= 300; i++)
{
    int square=i*i;
    string base1= base(square, B);
    if(palindrome(base1)==true)
       cout<<i<<" "<<base1<<"n";
}
return 0;
}

您可以以可控的方式将9以上的数字转换为字符,但将0到9的转换留给语言,这意味着您可以添加ASCII代码为0到9。

代替

output += remainder;

尝试

output += '0' + remainder;

然后,在下一步中,我强烈建议重写两个分支,不要依赖于底层编码中数字和字母的连续顺序。使用静态翻译字符串有什么问题?

output += character[remainder];