校招在线笔试做编程题的时候,输入的要求常常是不同的,记录每一次的输入,等以后在线笔试的时候就不慌了,噗哈哈
1、每次输入一个数字,当输入的不是数字的时候,循环结束
(注:当输入 回车、空格、tab键的时候,程序不会退出)
int main() {
//数据输入接口
int input = 0;
while (1) {
//如果input不是数字,则跳出循环
cin >> input;
if (cin.fail()) {
//not a number
cout << "当前输入非数字,程序退出" << endl;
break;
}
//number]
}
return 0;
}
2、输入一个string,判断string中是否全都是数字,如果存在非数字,则要求用户重新输入
#include <cctype>
#include<string>
#include<iostream>
using namespace std;
int main(void)
{
string str;
bool shuzi;
do
{
shuzi=true;
cout<<"请输入数字:"<<endl;
cin>>str;
for(string::iterator iter=str.begin();iter!=str.end();iter++)
{
if(!isdigit(*iter))
{
shuzi=false;
cout<<"输入含有非数字字符,请从新输入。"<<endl;
break;
}
}
}while(!shuzi);
system("pause");
return 0;
}
3、通过ascii码判断输入的char类型元素是否为数字
个位数的ascii码为 48(数字 0 的ascii码)到57(数字 9 的ascii码)之间。
#include<string>
#include<iostream>
using namespace std;
int main(void)
{
string str;
bool shuzi;
do
{
shuzi = true;
cout << "请输入数字:" << endl;
cin >> str;
for (string::iterator iter = str.begin(); iter != str.end(); iter++)
{
if (*iter < 48 || *iter > 57)
{
shuzi = false;
cout << "输入含有非数字字符,请从新输入。" << endl;
break;
}
}
} while (!shuzi);
system("pause");
return 0;
}
4、一行输入两个数字,分别赋值给两个变量
输入:
1 2
cin >> a >> b;
5、一行数据,其中第一个数据 N 用于申请一个长度为 N 的数组。
示例
输入:
7 7.1 2.8 -10 13 2 -1 7
参考链接(写的特别好):https://blog.csdn.net/juzihongle1/article/details/77642926
先来一个输入一行数字,把数字一次性的存入一个数组中的实现代码:
#include "pch.h"
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<double> a;
double i = 0;
do {
cin >> i;
a.push_back(i);
} while (getchar() != '\n');
return 0;
}
本题实现代码:
#include "pch.h"
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<double> a;
double i = 0;
cin >> i;
int len = i;
do {
cin >> i;
a.push_back(i);
} while (getchar() != '\n');
return 0;
}
6、输入正整数一行数,存入数组中
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
vector<int>nums;
string input;
int temp=0;
while(getline(cin,input)){
for(auto i:input){
if(i>='0'&&i<='9'){
temp=temp*10+(i-'0');
}else{
if(temp){
nums.push_back(temp);
temp=0;
}
}
}
//不写这个,输入的最后一个数就存不到数组中
if(temp){
nums.push_back(temp);
temp=0;
}
for(auto i:nums)
cout<<i<<endl;
nums.clear();
}
return 0;
}
7、输入一个字符串,字符串中的每个数字以空格分隔,将数字存入数组中
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void makeinput(string input, vector<int>&vec){
if(input.length()==0) return;
//以 ‘ ’作为标记,否则会丢一个数字的
input.push_back(' ');
int num=0;
vector<char>tool;
for(int i=0; i<input.length(); i++){
if(input[i] !=' ')
tool.push_back(input[i]);
else{
if(tool.empty()) continue;
for(auto i:tool){
//把每一位转成数字存入num中
num=num*10+(i-'0');
}
//清空tool
tool.clear();
vec.push_back(num);
num=0;
}
}
}
int main(){
vector<int>vec;
string input;
while(getline(cin,input)){
//拆分以空格分隔的字符串,将数字依次存入数组中
makeinput(input,vec);
for(auto i:vec){
cout<<i<<endl;
}
vec.clear();
}
return 0;
}
8、输入以空格分隔的字符串,拆分出各个数字
int main(){
vector<int>nums;
string input;
int temp=0;
//输入以空格分隔的字符串,拆分出各个数字
while(getline(cin,input)){
for(auto i:input){
if(i>='0'&&i<='9'){
temp=temp*10+(i-'0');
}else{
if(temp){
nums.push_back(temp);
temp=0;
}
}
}
if(temp){
nums.push_back(temp);
temp=0;
}
nums.clear();
}
return 0;
}