#include<iostream>
#include<algorithm>
using namespace std;
string big(int a[]){
int b[]={4};
int k=sizeof(a)/sizeof(int);
reverse(a,a+5);//反转
int re[7];
int j=0;
for(int i=0;i<k;i++){
re[i+j]=a[i]*b[j];//相乘
}
for(int i=0;i<6;i++){
if(re[i]>9){
re[i+1]=re[i+1]+re[i]/10;
re[i] %= 10;//进位
}
}
if(re[5]==0){
static string str1,str2,str3,str4,str5,str6;
str1= re[0]+'0';
str2= re[1]+'0';
str3= re[2]+'0';
str4= re[3]+'0';
str5= re[4]+'0';
str6=str1+str2+str3+str4+str5;
return str6;
}
else{
static string str1,str2,str3,str4,str5,str6,str7;
str1= re[0]+'0';
str2= re[1]+'0';
str3= re[2]+'0';
str4= re[3]+'0';
str5= re[4]+'0';
str6= re[5]+'0';
str7=str1+str2+str3+str4+str5+str6;
return str7;
}
}
int main(){
int a[5]={1,2,3,4,5};
string g=big(a);
cout<<g;
}
函数调用结果错误(单独使用函数可以用)
int re[7];
但a只有5个元素,那么在函数中并没有给re[5]和re[6]赋值啊,会是一个随机值,怎么会有正确的结果呢
试试a[7]吧
供参考:
#include<iostream>
#include<algorithm>
using namespace std;
string big(int a[]) {
int b[] = { 4 };
int k = 5;
cout<<sizeof(a) / sizeof(int)<<endl;
reverse(a, a + 5);//反转
for (int i = 0; i < k; i++)
cout << a[i]<<" ";
cout << endl;
int re[7] = {0};
int j = 0;
for (int i = 0; i < k; i++) {
re[i + j] = a[i] * b[j];//相乘
}
for (int i = 0; i < 6; i++) {
if (re[i] > 9) {
re[i + 1] = re[i + 1] + re[i] / 10;
re[i] %= 10;//进位
}
}
if (re[5] == 0) {
static string str1, str2, str3, str4, str5, str6;
str1 = re[0] + '0';
str2 = re[1] + '0';
str3 = re[2] + '0';
str4 = re[3] + '0';
str5 = re[4] + '0';
str6 = str1 + str2 + str3 + str4 + str5;
return str6;
}
else {
static string str1, str2, str3, str4, str5, str6, str7;
str1 = re[0] + '0';
str2 = re[1] + '0';
str3 = re[2] + '0';
str4 = re[3] + '0';
str5 = re[4] + '0';
str6 = re[5] + '0';
str7 = str1 + str2 + str3 + str4 + str5 + str6;
return str7;
}
}
int main() {
int a[5] = { 1,2,3,4,5 };
string g = big(a);
cout << g;
}