(已解决)579B但答案仍旧错误terminate called after throwing an instance of ‘ std::bad_array_length‘,
输入16进制16 ff 10
就会出现如上问题。应该是字符串问题。不过为什么?
#include<stdio.h>
#include<string.h>
char z[17]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
int main(){int a,b;char n[50]={};while(scanf("%d%s%d",&a,n,&b)!=EOF){int product=1;int len=strlen(n);//上面N数组长度已定义。int anum[len]={1,2,3},i,j;printf("len=%d\n",len);for(i=0;i<3;i++){j=15;while(j>=0){if(n[i]==z[j]){anum[i]=j;printf("%d\n",anum[i]);break;}else j--;}}//把字符串转换成整型数组 printf("n[2]=%d\n",n[2]);printf("anum[0]=%d\n",anum[0]);printf("anum[1]=%d\n",anum[1]);printf("anum[2]=%d\n",anum[2]);for(i=0;i<len;i++){printf("%d",anum[i]);}printf("\n");long long int y=0; while(len--){y+=anum[len]*product;product*=a;}//转换为10进制数 printf("y=%d\n",y);int bnum[50]={0};i=0,j=0;do{bnum[i++]=y%b;y/=b;}while(y!=0);//转换为b进制数字字符串len=i;for(i=0;i<len;i++){printf("%d ",bnum[i]);}printf("\n");char b[50];for(i=0;i<len;i++){b[i]=z[bnum[i]];}b[i]='\0';//转换为b进制字符串for(i=len-1;i>=0;i--){printf("%c",b[i]);} printf("\n");}return 0;
}
问题在于已定义n[50],后面又求长度strlen,所以我写了个函数来求长度,但Oj仍然没法通过。
#include<stdio.h>
#include<string.h>
char z[17]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
int nlen(char n[]){int len=strlen(n);return len;
}
int main(){int a,b;char n[50]={};while(scanf("%d%s%d",&a,n,&b)!=EOF){int product=1;int len=nlen(n);int anum[len]={},i,j;//printf("len=%d\n",len);for(i=0;i<3;i++){j=15;while(j>=0){if(n[i]==z[j]){anum[i]=j;//printf("%d\n",anum[i]);break;}else j--;}}//把字符串转换成整型数组 /*printf("n[2]=%d\n",n[2]);printf("anum[0]=%d\n",anum[0]);printf("anum[1]=%d\n",anum[1]);printf("anum[2]=%d\n",anum[2]);for(i=0;i<len;i++){printf("%d",anum[i]);}printf("\n");*/long long int y=0; while(len--){y+=anum[len]*product;product*=a;}//转换为10进制数 //printf("y=%d\n",y);int bnum[50]={0};i=0,j=0;do{bnum[i++]=y%b;y/=b;}while(y!=0);//转换为b进制数字字符串len=i;/*for(i=0;i<len;i++){printf("%d ",bnum[i]);}printf("\n");*/char b[50];for(i=0;i<len;i++){b[i]=z[bnum[i]];}b[i]='\0';//转换为b进制字符串for(i=len-1;i>=0;i--){printf("%c",b[i]);} printf("\n");}return 0;
}
正确代码:memcoy0
人家直接‘9’-‘0’,‘f’-‘a’+10;这样来计算值。和我方法不一样。
#include <cstdio>
#include <cstring>int CharToOct(int a, char n[]) { //按进制a将n字符串(可表示2-16进制)转换为10进制数int sum = 0, product = 1;for (int i = strlen(n) - 1; i >= 0; i--) {if (n[i] <= '9') sum += (n[i] - '0') * product;else if (n[i] <= 'F') sum += (n[i] - 'A' + 10) * product; //大写字母符号else if (n[i] <= 'f') sum += (n[i] - 'a' + 10) * product; //小写字母符号product *= a;}return sum;
}void OctToChar(int temp, int b, char r[]) { //将10进制数按b进制转换成r字符串int i = 0;do {int k = temp % b;if (k <= 9) r[i++] = '0' + k; //十进制符号else r[i++] = 'A' + k - 10; //用大写字母表示大于9的数字temp /= b;} while (temp != 0);r[i] = '\0'; //必须添加结束符, 不然strlen无法正确判别长度
}int main() {int a, b; // 2-16char n[100];while (scanf("%d%s%d", &a, n, &b) != EOF) {int temp = CharToOct(a, n);if (b == 10) {printf("%d\n", temp);continue;}char r[100];OctToChar(temp, b, r);for (int j = strlen(r) - 1; j >= 0; j--) printf("%c", r[j]);printf("\n");}return 0;
}
(已解决)579B但答案仍旧错误terminate called after throwing an instance of ‘ std::bad_array_length‘,相关推荐
- c++运行时报错terminate called after throwing an instance of ‘std::bad_alloc‘
程序运行时发生错误 terminate called after throwing an instance of 'std::bad_alloc'what(): std::bad_alloc 代码类似 ...
- Centos7 编译C++项目错误解决 : terminate called after throwing an instance of ‘std::regex_error‘
文章目录 1 问题原因 1.1 确保编译器支持std::regex 2 解决方法 3 Centos7升级gcc 3.1 安装centos-release-scl 3.2 安装devtoolset 3. ...
- Linux运行python文件出现以下错误:terminate called after throwing an instance of ‘std::runtime_error‘
Linux运行python文件出现以下错误:terminate called after throwing an instance of 'std::runtime_error'
- 错误:【terminate called after throwing an instance of 'std::logic_err】
自己写代码的时候,出现了一下提示 terminate called after throwing an instance of 'std::logic_error'what(): basic_stri ...
- XGB模型训练报错 terminate called after throwing an instance of ‘std::bad_alloc‘ what()
背景:需要做XGB模型增量训练,但是因为一些原因没有得到原来的XGB模型,只有dump文件. XGB模型的dump文件是它的树结构存储为一个相对人类好理解的模型文件,但并不能被重新加载进XGB进行增量 ...
- 报错信息 terminate called after throwing an instance of ‘std::cad_alloc‘ what():std::bad_alloc
最近做了一道bfs算法的程序题 输入测试数据后卡在运行框中 然后会产生一个错误信息 第一次见到这样的报错于是记录一下解决过程 terminate called after throwing an in ...
- terminate called after throwing an instance of ‘std::runtime_error‘ what(): locale::facet::_S_cre
在运行程序时出错: terminate called after throwing an instance of 'std::runtime_error' what(): locale::fac ...
- terminate called after throwing an instance of ‘std::runtime_error‘
terminate called after throwing an instance of 'std::runtime_error' what(): random_device::random_d ...
- terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr
运行时报错: terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::sub ...
最新文章
- tomcat高并发的配置
- 使用科大讯飞语音转文字的服务进行电话录音分析
- JSP实战型程序连载:通用数据库连接JavaBean
- 四个变量的图表怎么做_PPT中的图表怎么做才高大上?4步帮你搞定!
- 钉钉用户数破5亿 低代码应用数8个月增长86万
- amazon云计算平台_腾讯云首次公开边缘计算网络开源平台,拥抱5G与万物互联
- git上传项目全部流程
- Spring Boot Test 进行JPA 测试保存数据到数据库
- UniWebView笔记
- 单元测试,冒烟测试,SIT测试,UAT测试
- originpro 2021 附安装教程
- Basler千兆网相机使用相关设置
- js 实现下拉菜单 完整代码
- 3D图库框架范围与示例
- Zynga以特别的《CSR Racing 2》系列活动庆祝布加迪110周年
- Python爬虫实战 - 抓取BOSS直聘职位描述 和 数据清洗
- 《MFC添加语音功能》
- 小米手机、一加手机、华为手机、小米手环NFC刷门禁卡,全教程!
- 走进VOT--《High Performance Visual Tracking with Siamese Region Proposal Network》阅读翻译
- 关于nose的简单用法
热门文章
- (连载)Android 8.0 : 系统启动流程之Linux内核
- Linux系统chmod命令读、写、执行
- Xilinx FPGA资源解析与使用系列——Transceiver(十)PRBS、RX Equalizer、CDR
- html标签转换字符类型,java把html标签字符转换成普通字符(反转换成html标签)
- Excel遇到错误div/0显示为0或者不显示
- 如何证明根号3是无理数?------顺便说下希帕索斯和第一次数学危机
- 软件测试工程师需要学什么?
- 从PLC ,PAC ,到施耐德的自动化开放系统
- 使用FFmpeg进行mp4与m3u8之间转换
- Python: numpy tile()函数 可实现ndarray的横向纵向复制