華為機の試験問題:10個の整数があって、前の格数を順番にm個の位置に後ろに移動させて、最後にm個の数が一番前のm個の数になります.移動後の整数シーケンスの最初のm個の数と後のm個の数の和を計算します.
6993 ワード
質問説明:10個の整数があり、前の格子数を順番にm個の位置に後ろに移動させ、最後にm個の数が一番前のm個の数になります.移動後の整数シーケンスの最初のm個の数と後のm個の数の和を計算します.実行時間制限:無制限メモリ制限:無制限入力:まず10個の整数を入力し、スペースで区切ります.移動個数(すなわち整数m)出力を入力:移動後の整数シーケンス移動後の整数シーケンスの前m個と後m個の和.サンプル入力:1 2 3 4 5 6 7 8 9 10 3サンプル出力:8 9 10 1 2 3 6 7 27 18
#include
#include "string" // #include "string.h"
using namespace std;
// int
void convert_str_int(string str_array[],int * int_array)
{
for (int i = 0; i < 10; i++)
{
string str = str_array[i];
int res = 0;
for (int i = 0; i < str.length(); i++)
{
res = res + (str[i]-'0')*(pow(10,(str.length()-i-1)));//^ ,sqrt()
}
*(int_array+i)= res;
}
}
// m ,
void move_and_compute(int *array,int move_step,int *res_fore =0,int *res_back=0)
{
int place = 10-move_step;
int temp[10];
int res_temp1=0;
int res_temp2=0;
// m ,
for (int i = 0; i < move_step; i++)
{
temp[i] = *(array+place+i);
res_temp2 = res_temp2+temp[i];
}
// 10-m
for (int i = 0; i < place; i++)
{
temp[move_step+i] = *(array+i);
}
// , m
for (int i = 0; i < move_step; i++)
{
res_temp1 = res_temp1 + temp[10-move_step+i];
}
//
*res_fore = res_temp2;
*res_back = res_temp1;
for (int i = 0; i < 10; i++)
{
*(array+i) = temp[i];
}
}
void remove_blank(string str,string *res)
{
int index = 0;
for (int i = 0; i < str.length(); i++)
{
int step_num = 0;
string temp = "";
for (int j = 0;' '!=str[i+j]&&i+step_numif (""!= temp)
{
*(res+index) = temp;
index++;
}
}
}
int main()
{
//
string str(100,'0');
cout<<" 10 , :"<cin,str);
//
cout<" :";
int move_step = 0;
cin>>move_step;
string num_array[10];//
int res_fore = 0;// m
int res_back = 0;// m
int res[10];//
// , num_array
remove_blank(str,num_array);
//
convert_str_int(num_array,res);
//
move_and_compute(res,move_step,&res_fore,&res_back);
//
cout<<" :"<for (int i = 0; i < 10; i++)
{
cout<" ";
}
cout<" "<" :"<" "<<" "<<10-move_step<<" :"<