笔试中输入数据常见格式总结

参考:
https://blog.csdn.net/sophia1224/article/details/53054698
https://blog.csdn.net/daa20/article/details/50890211
https://blog.csdn.net/zzl913657644/article/details/52431011
http://doc.okbase.net/cc_again/archive/71796.html

一、给定元素个数,依次输入

比如:

5
1 2 3 4 5

数据不存储时

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{

    int n;
    int m;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
         cin >> m;
         cout<< m<<"_";
    }
 return 0;
}

数据存储时


    int n;
    cin >> n;
    int *arrsrc=new int [n];
    for(int i=0;i<n;i++)
        cin>>arrsrc[i];

    for(int i=0;i<n;++i)
        cout<<arrsrc[i]<<" ";
    cout<<endl;

    delete []arrsrc;

有时候题目会给你一个二维的数组,m*n
比如:

4   4
*000 
00*0 
00** 
0*00

输入第一行为二维数组的实际大小,之后每一行为二维数组的实际内容。

#include <iostream>
#include <vector> 
using namespace std;
//打印二维vector
void printVec(vector<vector<char> >  vec)
{
    vector<char> vec_tmp;

    for (vector<vector<char> >::iterator iter = vec.begin(); iter != vec.end(); iter++)
    {
        vec_tmp = *iter;
        for (vector<char>::iterator it = vec_tmp.begin(); it != vec_tmp.end(); it++)
            cout << *it << " ";
        cout << endl;
    }
}

int main(int argc,char* argv[])
{

    int m,n;
    cin>>m>>n;
    //给定行数、列数的二维vector初始化
    vector<vector<char> > a(m,vector<char>(n));
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            cin>>a[i][j];
        }
    }

    printVec(a);
    return 0;
}

二、循环读入若干数据,并以回车结束

输入多行字符串

string s;
while(cin>>s){
//具体处理过程;
}

输入数字

一行整数序列,数字用空格分隔,如:1 4 -1 3 5 6 2

(1)C语言

#include <stdio.h>
int main(int argc, char* argv[])
{
    int array[10] = {0};
    int i=0;
    int index = 0;
    while ((scanf("%d",&array[index]) != EOF) && getchar() != '\n')
    {
        index++;
    }
    printf("index = %d\n",index);
    for(i=0;i<=index;i++)     //注意这里的i<=index ,是有等号的
    {
        printf("%dth :  %d\n",i,array[i]);
    }
    return 0;
}

(2)C++语言

当输入未知元素个数,必须放入arr中时:

#include <iostream>
#include <cstdio> 
using namespace std;

int main(int argc,char* argv[])
{

 int index = 0;
 int array[10];
 while((cin.peek()!=EOF)&&(cin.peek()!='\n'))   
 {
  cin>>array[index];
  index++;
 }
 for(int i=0;i<index;i++)
 {
  cout << array[i] <<" ";

 }
 cout << endl;
 return 0;
}

当输入元素个数未知,放入到vector中时:

int tmp;
vector<int> v;
while(cin>>tmp){
    v.push_back(tmp);
}

(3)另外一种思路,该方法读取如下数据格式:
3
11 2 4 -1 -10 2
11 2 4 -1 -10 2

代码:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cstdio>

std::string buff;

#define MAX_N 1003
#define MAX_W 1003

int v[MAX_N];
int w[MAX_W];


int W;

int main() {
    std::string buf;
    std::cin >> W;
    std::cout<<W<<std::endl;

    getchar();
    std::stringstream ss;
    getline(std::cin, buf);

    ss << buf;

    int cnt = 0;
    int tmp;
    while (ss >> tmp) 
        std::cout<<tmp<<" ";
     std::cout<<std::endl;

    ss.clear();

    getline(std::cin, buf);

    ss << buf;
    cnt = 0;
    while (ss >> tmp)
        std::cout<<tmp<<" ";
    std::cout<<std::endl;

    return 0;
}


补充:stringstream的使用方法

stringstream类同时可以支持C风格的串流的输入输出操作。

这里写图片描述

istringstream是由一个string对象构造而来,从一个string对象读取字符。
ostringstream同样是有一个string对象构造而来,向一个string对象插入字符。
stringstream则是用于C++风格的字符串的输入输出的。

代码测试:

#include<iostream>
#include <sstream> 
using namespace std;
int main(){
    string test = "-123 9.87 welcome to, 989, test!";
    istringstream iss;//istringstream提供读 string 的功能
    iss.str(test);//将 string 类型的 test 复制给 iss,返回 void 
    string s;
    cout << "按照空格读取字符串:" << endl;
    while (iss >> s){
        cout << s << endl;//按空格读取string
    }
    cout << "*********************" << endl;

    istringstream strm(test); 
    //创建存储 test 的副本的 stringstream 对象
    int i;
    float f;
    char c;
    char buff[1024];

    strm >> i;
    cout <<"读取int类型:"<< i << endl;
    strm >> f;
    cout <<"读取float类型:"<<f << endl;
    strm >> c;
    cout <<"读取char类型:"<< c << endl;
    strm >> buff;
    cout <<"读取buffer类型:"<< buff << endl;
    strm.ignore(100, ',');
    int j;
    strm >> j;
    cout <<"忽略‘,’读取int类型:"<< j << endl;
    return 0;
}

栗子2:

   string inputstr;
   getline(cin,inputstr);
   //inputstr:i  am a  student
   cout<<inputstr<<endl;

   istringstream iss;
   iss.str(inputstr);
   string tempstr;
   //根据空格分割读取string
   while(iss>>tempstr)
   {
       cout<<tempstr<<endl;
   }

如何使用stringstream?

栗子:将数字转换为字符串


    vector<int> num{ 3,32,321 };
    vector<string> strNum;
    for (int i = 0; i<num.size(); ++i)
    {
        stringstream ss;
        ss << num[i];
        cout << "string:" << ss.str() << endl;
    }

总结:
1)在istringstream类中,构造字符串流时,空格会成为字符串参数的内部分界;
2)istringstream类可以用作string与各种类型的转换途径
3)ignore函数参数:需要读取字符串的最大长度,需要忽略的字符

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java高级开发常见笔试题主要包括以下几个方面:多线程、集合框架、IO流、异常处理以及设计模式。 多线程方面,常见的问题有如何创建线程、线程安全、线程间通信等。例如,可以通过继承Thread类或实现Runnable接口来创建线程,可以使用synchronized关键字或Lock接口来实现线程安全,可以使用wait()、notify()、notifyAll()方法来实现线程间的通信。 集合框架方面,常见问题包括ArrayList和LinkedList的区别、HashMap和HashTable的区别、如何使用迭代器等。例如,ArrayList和LinkedList的区别在于前者适用于随机访问,后者适用于插入、删除多的场景;HashMap和HashTable的区别在于前者非线程安全,后者线程安全;迭代器可以使用Iterator接口来遍历集合元素。 IO流方面,常见问题涉及输入输出流的分类、字节流和字符流的区别、File类的使用等。例如,输入输出流可以分为字节流和字符流,字节流适用于二进制文件,字符流适用于文本文件;字节流以字节为单位读写数据,字符流以字符为单位读写数据;File类可以用来操作文件和目录。 异常处理方面,常见问题有如何处理异常、自定义异常等。例如,可以使用try-catch语句来处理异常,可以使用throw关键字来抛出异常,还可以自定义异常类来满足特定需求。 设计模式方面,常见问题包括单例模式、工厂模式、观察者模式等。例如,单例模式可以通过私有构造函数和静态方法来确保类只有一个实例;工厂模式可以通过工厂类来创建对象,避免直接创建对象;观察者模式可以通过定义观察者和被观察者接口,实现对象间的通知和更新。 以上只是一些常见笔试题,实际上Java高级开发的内容非常广泛,需要掌握的知识也比较多。希望以上回答能够帮助到您。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值