【gets getline的用法 char[]转化为str】poj 2418
http://poj.org/problem?id=2418
【注意】
1. 输入有空格,用
char str[maxn];
while(gets(str)){
str[]!='\0';
}
或
string str;
while(getline(cin,str)&&!(str=='')){ }
2.
char str[maxn];
string s;
s=str;
而不是
char str[maxn];
string s;
int len=strlen(str);
for(int i=;i<len;i++) s[i]=str[i];
【Accepted】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<map> using namespace std;
const int maxn=;
char str[maxn];
map<string, int> mp;
map<string, int>::iterator it;
int main(){
int cnt=;
while(gets(str)){
if(str[]=='\0') break;
string s;
/*
//错误写法
int len=strlen(str);
for(int i=0;i<len;i++){
s[i]=str[i];
}
*/ //正确写法
s=str;
if(mp.find(s)!=mp.end()){
mp[s]+=;
}else{
mp[s]=;
}
cnt++;
}
for(it=mp.begin();it!=mp.end();it++){
cout<<it->first<<" ";
printf("%.4f\n",*it->second*1.0/cnt);
} return ;
}
【gets getline的用法 char[]转化为str】poj 2418的更多相关文章
- c++中字符输入函数cin.getline在输入char与string时的不同
cin.getline在输入char时: using namespace std; ; char name[ArSize]; char dessert[ArSize]; cout << & ...
- C++:cin、cin.getline()、getline()的用法
主要内容: 1.cin用法 2.cin.getline()用法 3.getline()用法 3.注意的问题 一.cin>> 用法1:输入一个数字或字符 #include <iostr ...
- C++ cin.get及getline的用法
1.cin.get() 从指定的输入流中提取一个字符,函数的返回值就是这个字符.文件结束符会返回EOF,一般以-1代表EOF. #include<iostream> using names ...
- C++中getline的用法
在看紫皮书的时候看到getline,然后查了查具体用法,记录下来. #include"iostream" #include"string" using name ...
- pandas格式化str为时间,pandas将int转化为str
code_300['HISTORY_DATE'] = code_300['HISTORY_DATE'].map(str)code_300['HISTORY_DATE'] = pd.to_datetim ...
- c/c++读取一行可以包含空格的字符串(getline,fgets用法)
1.char[]型 char buf[1000005]; cin.getline(buf,sizeof(buf)); 多行文件输入的情况: while(cin.getline(buf,sizeof(b ...
- awk中的system和getline的用法
system只能对命令的输出结果输出到终端. getline在awk中可以使命令的输出结果传到一个变量中保存. # awk 'BEGIN{system("date")|getlin ...
- 【转载】C++ getline函数用法
https://www.cnblogs.com/xiaofeiIDO/p/8574042.html 摘要: 通过getline()函数一个小小的实例,那么把getline()函数作为while的判断语 ...
- django自定义标签,int转化为str类型
1.在app中创建templatetags目录,目录名必须为templatetags 2.在目录templatetags中创建一个.py文件,例如 strFilter.py strFilter.py ...
随机推荐
- hihoCoder #1079 : 离散化 (线段树,数据离散化)
题意:有一块宣传栏,高一定,给出长度,再给出多张海报的张贴位置,问还能见到几张海报(哪怕有一点被看到)?假设海报的高于宣传栏同高. 思路:问题转成“给出x轴上长为L的一条线段,再用n条线段进行覆盖上去 ...
- 洛谷 P1340 兽径管理
题目描述 约翰农场的牛群希望能够在 N 个(1<=N<=200) 草地之间任意移动.草地的编号由 1到 N.草地之间有树林隔开.牛群希望能够选择草地间的路径,使牛群能够从任一 片草地移动到 ...
- OpenStack安装keyston 错误BError: (pymysql.err.InternalError) (1071, u‘Specified key was too long; max key length is 767 bytes‘) [SQL: u‘\nCREATE TABLE migrate_ver
折腾了两天的错误,BError: (pymysql.err.InternalError) (1071, u‘Specified key was too long; max key length is ...
- wxwidgets编译及环境配置
wxwidgets编译及环境配置 安装步骤: 到www.CodeBlocks.org下载并安装CodeBlocks,最好下载MinGW版本的,可以省掉安装和配置GCC的麻烦. 到www.wxWidge ...
- XDU——受教了
存在的问题还是很多的 GG 突然觉得刷题的目的并不是追求A.我们应该在那个过程中提高代码能力和建立模型解题能力 会的算法会巧妙应用才是王道 吐槽自己两句,写高数了
- 背包问题2 (lintcode)
这里: for(int j = 1;j <= m;j++) result[0][j] = 0x80000000; 不能从0开始,result[0][0]是可以取到的,是0.其他情况取不到才用最小 ...
- SpringBoot入门,新建SpringBoot项目
一.在Spring Initializr中创建初始化项目 https://start.spring.io/ 二.通过maven导入Idea中(解压后的项目) 解压文件 黄色的为项目需要的真正的代码 , ...
- House of Spirit(fastbin)
0x01 fastbin fastbin所包含chunk的大小为16 Bytes, 24 Bytes, 32 Bytes, … , 80 Bytes.当分配一块较小的内存(mem<=64 Byt ...
- js正则函数match、exec、test、search、replace、split使用集合
match 方法 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) 参数 stringObj 必选项.对其进行查找的 String 对 ...
- Spring框架中的aop操作之二 通过配置文件实现增强
aop表达式写法 配置文件代码: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...