输出C++USACO中出现意外的表情符号/笑脸
Unexpected emoticons/smileyes in output C++ USACO
我正试图从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];
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 将无符号char*转换为std::istream*C++
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- vscode g++链路故障:体系结构x86_64的未定义符号
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 使用gcc从静态链接的文件中查找可选符号
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- VC++本机单元测试,找不到调试符号
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 检查TCHAR数组输入是否为带符号整数C++
- 用符号版本替换对函数的所有调用
- 未解析的外部符号_MsiLocateComponentW@12.
- 如何打印boost多精度128位无符号整数
- 为什么我有一个"="符号输出和 2 个笑脸而不是正确的输出?C++
- 输出C++USACO中出现意外的表情符号/笑脸