.输入一行数字:123 423 5645 875 186523
在输入第二行:23
将第一行中含有第二行中“23”的数输出并排序
结果即:123 423 186523
思路: .依据第二行输入的数,每次除以这个数的的 上限 (23 -- 100, 123-- 1000)取余数 比较 ,不同就再 除10,继续。
如果存在那 放入一个数组中。
#include<iostream>
#include<algorithm>
using namespace std;
int find(int * ,int ,int); //查找出处带有第二行的个数,并将其放在前几个位置
int main()
{
int arr[10],i =0;
char c ='0';
while (c !='\n')
{
cin>> arr[i];
i++;
cin.get(c); //处理单行输出的enter,
}
int number;
cin>> number;
int count =find (arr,i,number);
sort(arr,arr+count); //排序
for(int i =0 ;i <count ; i++)//输出
cout<<arr[i]<<ends;
}
int find (int * arr,int n, int number)
{
int max =10;
int tem =number ;
while (tem /= 10)//查找出第二行对应的上限 53-> 100,123->1000
max *= 10;
int count = 0;
for(int dex = 0 ;dex < n ;dex++){
tem= arr[dex];
while( tem )//判断这个数是否包含要查找的书,如果是统计个数,并且交换位置
{
if (tem % max == number){
swap(arr[count],arr[dex]);
count++;
break;
}
else
tem /=10;
}
}
return count;
}