getchar

C++快读快写

十年热恋 提交于 2020-01-11 15:51:12
一般scanf,printf可以应对大部分的题,但一道题目数据量特别大,就要用到快读快写模板 通过读入字符而后来转成数字,而原理就是读入字符比数字快。 快读: int read() { int x=0,f=1; char c=getchar(); while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();} while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); return x*f; } 快写: void write(int x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } 来源: CSDN 作者: 在路上Ven 链接: https://blog.csdn.net/Ven21959/article/details/103935553

How does the EOF macro work with getchar?

岁酱吖の 提交于 2020-01-11 13:58:50
问题 #include<stdio.h> int main(void) { FILE *fp; int ch; fp = fopen("input.txt", "w"); printf("Enter data"); while ((ch = getchar()) != EOF) { putc(ch, fp); } fclose(fp); fp = fopen("input.txt", "w"); while ((ch = getc(fp)) != EOF) { printf("%c", ch); } fclose(fp); } How does the first while loop stop inputting from user? Since there is EOF present as a condition. Or else do I need to use for loop? 回答1: EOF is a value, not a function. It is essentially defined as a macro. For reference, from C11

C - getchar() in a loop?

给你一囗甜甜゛ 提交于 2020-01-11 12:49:08
问题 How I can use getchar() in a loop? Now I have... for (p=0; p<n_players; p++) { ... fflush(stdin); getchar(); } But it doesn't work... if n_players is 3, it execute getchar 2 times only at the end... for (p=0; p<n_players; p++) { blank_start(); ascii_art_title(); printf("%s, tocca a te...\n",player_info[p].player_name); srand(time(NULL)); random_speed = MIN_WHEEL_SPEED + rand()%MAX_WHEEL_SPEED; move_wheel_pointer(random_speed, &pointer); if (player_points(&wheel[pointer]) == 0){ player_info[p]

std::cout<<\"Goodbye 2019\"<<\" \"<<\"Hello 2020\"<<'\\n';

会有一股神秘感。 提交于 2020-01-11 08:00:44
#include<iostream> #include<cstring> #include<string> #include<cstdio> #include<algorithm> inline void read(int &T) { int x=0;bool f=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=!f;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} T=f?-x:x; } int main() { std::cout<<"Goodbye 2019"<<" "<<"Hello 2020"<<'\n'; return 0; } 转眼间2019年已经过去了,2020年已经到来了。 来源: https://www.cnblogs.com/poi-bolg-poi/p/12178854.html

Luogu P2178 [NOI2015]品酒大会

让人想犯罪 __ 提交于 2020-01-10 17:48:24
题意:对于每个 \(k\) ,求 \(S[p,p+k-1]==S[q,q+k-1],(p,q)为无序二元组\) 的方案数,并求出在 \(k\) 一定时, \(vl[p]\times vl[q]\) 的最大值。 \(SA\) 并查集:我们把 \(ht[]\) 当做边,合并时计算两个集合产生的贡献; 好像还可以单调栈+ST表做吧(没写。 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define ll long long #define R register int using namespace std; namespace Luitaryi { inline int g() { R x=0,f=1; register char s; while(!isdigit(s=getchar())) f=s=='-'?-1:f; do x=x*10+(s^48); while(isdigit(s=getchar())); return x*f; } const int N=300010; int n,a[N],sa[N],rk[N],ht[N],c[N],x[N],y[N]; char s[N]; inline void init() { R m=122; for(R i=1;i<=n;+

线段树练习3

痴心易碎 提交于 2020-01-10 04:12:06
题目传送: http://codevs.cn/problem/1082/ 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和。 输入描述 Input Description 第一行一个正整数n,接下来n行n个整数, 再接下来一个正整数Q,每行表示操作的个数, 如果第一个数是1,后接3个正整数, 表示在区间[a,b]内每个数增加X,如果是2, 表示操作2询问区间[a,b]的和是多少。 pascal选手请不要使用readln读入 输出描述 Output Description 对于每个询问输出一行一个答案 样例输入 Sample Input 3 1 2 3 2 1 2 3 2 2 2 3 样例输出 Sample Output 9 数据范围及提示 Data Size & Hint 数据范围 1<=n<=200000 1<=q<=200000 分类标签 Tags 点此展开 代码 堆 #include<cstdio> #include<iostream> using namespace std; #define N 801000 #define mid ((l+r)>>1) #define lc (k

【Codevs1288】埃及分数

风流意气都作罢 提交于 2020-01-09 16:54:06
Position: http://codevs.cn/problem/1288/ Description   在古埃及,人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数。 如:2/3=1/2+1/6,但不允许2/3=1/3+1/3,因为加数中有相同的。 对于一个分数a/b,表示方法有很多种,但是哪种最好呢? 首先,加数少的比加数多的好,其次,加数个数相同的,最小的分数越大越 好。 如: 19/45=1/3 + 1/12 + 1/180 19/45=1/3 + 1/15 + 1/45 19/45=1/3 + 1/18 + 1/30, 19/45=1/4 + 1/6 + 1/180 19/45=1/5 + 1/6 + 1/18. 最好的是最后一种,因为1/18比1/180,1/45,1/30,1/180都大。 给出a,b(0 < a < b < 1000),编程计算最好的表达方式。 Input a b Output 若干个数,自小到大排列,依次是单位分数的分母。 Sample Input 19 45 Sample Output 5 6 18 Solution 精简版本 给定一个分数 A/B,要将其转换为单位分数之和。要求单位分数数量最少,且每个分数都不同。 Source 普通搜索枚举哪个数可以填显然会TLE,它会不断搜下去,爆LL,爆栈。 那么怎么解决呢

【模板】文艺平衡树

无人久伴 提交于 2020-01-09 00:58:59
如题,是一个模板。。。 Splay: 1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <cctype> 6 7 inline void read(int & x) 8 { 9 x = 0; 10 int k = 1; 11 char c = getchar(); 12 while (!isdigit(c)) 13 if (c == '-') c = getchar(), k = -1; 14 else c = getchar(); 15 while (isdigit(c)) 16 x = (x << 1) + (x << 3) + (c ^ 48), 17 c = getchar(); 18 x *= k; 19 } 20 21 const int MAXN = 101015; 22 int n, m, x, y; 23 24 int a[101010]; 25 26 struct SplayHello 27 { 28 int rt = 0, tot = 0; 29 int cnt[MAXN]; 30 int siz[MAXN]; 31 int tag[MAXN]; 32 int val[MAXN]; 33 int faz[MAXN];

吉首大学第九届"新星杯"大学生程序设计大赛

ぃ、小莉子 提交于 2020-01-08 23:58:39
A: 直接打表所有可以到达的点就可以了 1 #include <math.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <algorithm> 6 #include <string> 7 #include <string.h> 8 #include <vector> 9 #include <map> 10 #include <stack> 11 #include <set> 12 #include <queue> 13 14 #define LL long long 15 #define INF 0x3f3f3f3f 16 #define ls nod<<1 17 #define rs (nod<<1)+1 18 19 const int maxn = 5e5 + 10; 20 const int MOD = 433494437; 21 22 template<class T>inline void read(T &res) 23 { 24 char c;T flag=1; 25 while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0'; 26 while((c=getchar())>='0'&&c<='9'

【AtCoder】ARC067

一世执手 提交于 2020-01-08 00:48:30
ARC067 C - Factors of Factorial 这个直接套公式就是,先求出来每个质因数的指数幂,然后约数个数就是 \((1 + e_{1})(1 + e_{2})(1 + e_{3})\cdots(1 + e_k)\) #include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >=