C++输入输出格式问题
来源:互联网 发布:亚马逊德国站情况知乎 编辑:程序博客网 时间:2024/06/05 23:02
一)
为什么c++中输入一个整数后,再输入一个带空格的字符串会输入不了字符串?
因为cin通过空格、换行符、制表符来确定结束位置,所以只有1被附给data,如果你要将“1 2 3 4 55”作为一个字符串输出,你可以这样写:
c++怎么求一个带空格字符串长度
getline(cin,s);s.length();
int i = 0;
string data;
cin >> i;
cin.get(); //这应该懂吧,读取换行符,因为换行符在你按回车时还在输入队列中
getline(cin, data); //正因为cin.get()将换行符读取了,这行代码可以读取一行,否则将直接读取换行符,这样子这一行的内容都存进data字符变量中
cout << i << endl << data;
system("pause");
return 0;
实例,输入一行字符串,输出重复部分,并且要求第一个按顺序保留!!
int main()
{
string str;
getline(cin, str);
int num = str.size();
int hash[260] = { 0 };
for (int i = 0; i < num; i++)
{
hash[str[i]]++;
}
for (int i = num - 1; i >= 0; i--)
{
if (hash[str[i]]>1)
{
hash[str[i]]--;
str.erase(i, 1);
// str.erase(i);
}
}
cout << str << endl;
system("pause");
return 0;
}
、---------------------------------------------------
string str = "112233";
str.erase(0, 4);
cout << str <<endl;
删掉了从第0个开始的4个字符,剩下输出为33.
----------------------------------------------------------------
int main()
{
string str;
set<string> deletstr;
while (cin >> str)
{
deletstr.insert(str);
}
string str1;
map<string, int> strlist;
cin.clear();
while (cin >> str1)
{
if (!deletstr.count(str1))
{
strlist[str1]++;
}
}
system("pause");
return 0;
}
两个while 之间加cin.clear() 否则第二个while不执行!
三)
输入一串字符串,有数字和字母组成,将分割的数字找出来,并找出在最大的数字字符串!
int main()
{
char s[100];
char ns[100][100] = {0};
//cin >> s;
cin.getline(s, 100);
int n = strlen(s);
int j = 0;
int i = 0;
int index = 0;
int a = 0, b = 0, c = 0;
for (j = 0, i = 0,index=0; i <= n; i++)
{
if (s[i]>='0'&&s[i] <= '9')
{
if (a == 0&&b==1&&c==1)
{
index++;
}
ns[index][j] = s[i];
j++;
a = 1; b = 0,c=1;
}
else if (s[i]<'0' || s[i] > '9'||s[i]=='\0')
{
if (a == 1 && b == 0)
{
ns[index][j] ='\0';
}
j=0;
a = 0; b = 1;
}
}
cout << s<< endl;
for (int i = 0; i <= index; i++)
{
for (int j = 0; j < strlen(ns[i]); j++)
{
cout << ns[i][j];
}
cout << endl;
}
char *max = ns[0];
for (int i = 1; i <= index; i++)
{
if (strlen(ns[i]) > strlen(max))
{
max = ns[i];
}
else if (strlen(ns[i]) == strlen(max))
{
if (strcmp(ns[i], max))
max = ns[i];
}
}
cout << max << endl;
system("pause");
return 0;
}
四) 输入一行数字,保存到数组中!
参考了这篇博客,http://blog.csdn.net/qsyzb/article/details/21649805
这篇博客的第一种方法没有考虑输入的数字串中有负数的情况,稍加改动
方法一:
- #include<iostream>
- using namespace std;
- int main()
- {
- int a[50];
- int i = 0;
- char c;
- while((c=getchar())!='\n')
- {
- if(c!=' ')//把这句判断条件改动
- {
- ungetc(c,stdin);
- cin>>a[i++];
- }
- }
- for(int j=0;j<i;j++)
- {
- cout<<"a["<<j<<"]:"<<a[j]<<endl;
- }
- }
方法二:
- #include<iostream>
- using namespace std;
- int main()
- {
- int a[20];
- int i = 0;
- char c;
- cin>>a[i++];
- while((c=getchar())!='\n')
- {
- cin>>a[i++];
- }
- for(int j=0;j<i;j++)
- {
- cout<<"a["<<j<<"]:"<<a[j]<<endl;
- }
- }
输入 (输入中包含负数)
3 4 -6 9 -2 -4 8 5
输出
======================================
输入 (输入中不包含负数)
3 4 610 900 2 404 8 5
输出
五) 输入一行数字,不管是空格还是换行,以输入-2为结束标志, 将其保存到数组中!
主要考虑是大数的情况和结尾有0的情况!
方法一,利用string和stringstream (#include<sstream>)实现string和int转换
string arr[100];
int arr1[100] = {0};
string str;
int k = 0;
while (cin >> str&&str != "-2")
{
arr[k] = str;
stringstream stream;
stream << arr[k];
stream >> arr1[k];
k++;
}
方法二 更加通用,直接利用int数组
int ntmp;
int ntmp1[100];
int ind = 0;
while (cin >> ntmp&&ntmp != -2)
{
ntmp1[ind++] = ntmp;
}
六) 将一个整数翻转输出 ,反转一个整数,比如:x=1234 return 4321;x=-4567 return -7654
我的做法:
int main()
{
char numstr[100];
stringstream stream;
int num = -123;
stream << num;
stream >> numstr;
int len = strlen(numstr);
int i = 0, j = len-1;
if (num < 0)
i = 1;
while (i < j)
{
char c = numstr[i];
numstr[i] = numstr[j];
numstr[j] = c;
i++;
j--;
}
stream.str(" ");
stream.clear();
int second;
stream << numstr; //插入bool值
stream >> second; //提取出int
std::cout << second << std::endl;
方法2
int main()
{
int num1 = -1230000;
if (num1 != 0)
{
int num = num1;
if (num1<0)
num = -1 * num1;
string str = "";
while (num != 0)
{
str = str + char(num % 10 + '0');
num /= 10;
}
if (num1 < 0)
{
str = "-" + str;
}
cout << str << endl;
stringstream stream;
int second;
stream << str; //
stream >> second; //
std::cout << second << std::endl;
}
else
cout << 0 << endl;
- c语言输入输出格式
- c语言输入输出格式
- c语言输入输出格式
- C语言输入输出格式
- C++输入输出格式问题
- C语言输入输出格式符
- C语言输入输出格式符
- C++_输入输出格式控制
- C语言中的输入输出格式
- C语言输入输出格式控制
- C缓冲区(输入输出问题)
- c语言中输入输出格式集合
- C语言中输入输出格式控制
- C语言学习(二)格式输入输出
- C语言输入输出格式printf(),scanf()
- C语言中输入输出格式控制
- c语言中输入输出格式的控制
- C语言中输入输出格式控制
- SCUT Training 20170920 Problem F
- 1013. 数素数 (20)
- showCase(new String('A'));
- 等高布局
- Mac系统中Python3的安装与切换
- C++输入输出格式问题
- Android之Linerlayout、RelativeLayout、FrameLayout性能比较
- 学习笔记(五)装饰器、迭代器、匿名函数
- 操作系统之分段
- JavaScript数字
- 单身狗脱单算法 脱单了算你赢
- 数据库——SQL中like与通配符的使用
- 异常
- 9月21日