stringstream字符串流的妙用

现在有一个数组,其值为从1到10000的连续增长的数字。出于某次偶然操作,导致这个数组中丢失了某三个元素,同时顺序被打乱,现在需要你用最快的方法找出丢失的这三个元素,并且将这三个元素根据从小到大重新拼接为一个新数字,计算其除以7的余数。 例:丢失的元素为336,10,8435,得到的新数字为103368435,除以七的余数为2。

/*顺序被打乱的意思是这个数组的剩余的9997个数不再是递增的而是无序的,在剩余的9997个数字
对应的本来的位置若存在则置为1,不存在则依旧是初始值0,然后再做一个查找,把值为0的三个位置找出,得到
丢失的三个数*/
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
    int temp;
    int num[10001]={0};
    string str=" ",s;
    long long sum=0;//防止新数字溢出
    for(int i=1;i<=9997;i++)//因为题目已经说了值从1道10000所以0也就没必要了
    {
        cin>>temp;
        num[temp]=1;
    }
    for(int j=1;j<=10000;j++)//同上
    {
        if(num[j]==0)
        {
            stringstream s_temp;
            s_temp<<j;//类型转换从int到string
            s_temp>>s;
            str+=s;
        }
    }
    stringstream s_temp;
    s_temp<<str;//类型转换从string到long long
    s_temp>>sum;
    cout<<sum%7<<endl;
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/wuyepeng/p/9595289.html