1、输入形式为:1,2,3,-4,5 将每个整数存入数组中
C++版
#include<iostream>
#include<vector>
#include<sstream>
using namespace std;
int main()
{
string str;
cin >> str;
vector<int>arr;
string tmp;
stringstream str_1(str);
while (getline(str_1, tmp, ','))
{
arr.push_back(atoi(tmp.c_str()));
}
}
Java版
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String[] strings = cin.nextLine().split(",");
int[] nums = new int[strings.length];
for (int i = 0;i<strings.length;i++){
nums[i] = Integer.parseInt(strings[i]);
}
}