getchar

gets()和puts()函数与getchar()与putchar()函数的区别之C语言

ⅰ亾dé卋堺 提交于 2020-01-07 14:38:15
gets() 函数:将接收输入的 整个字符串 直到回车为止。gets(s)函数与scanf("%s", &s)相似, 但不完全相同, 使用scanf("%s", &s) 函数输入字符串时存在一个问题, 就是如果输入了 空格会认为输入字符串结束 , 空格后的字符将作为下一个输入项处理。 puts()函数:用来向标准输出设备(屏幕)写字符串并 自动换行 , 其调用格式为: puts(s); getchar函数:只能用于 单个字符 的输入,一次输入一个字符。程序的功能是输入一个字符,显示一个字符,回车换行,再输入并显示一个字符。而运行时字符是连续输入的,运行结果却是正确的,这是因为输入字符后,它们暂存于键盘的缓冲区中,然后由getchar函数从键盘缓冲区中一个一个的取出来。 putchar函数:只能用于 单个字符 的输出,且一次只能输出一个字符。 来源: CSDN 作者: 小智解说 链接: https://blog.csdn.net/qq_42680327/article/details/103872089

Hello 2020 题解

心已入冬 提交于 2020-01-07 08:37:25
新年第一场。 开场 2min 写了 A,再 5min 写了 B,再 4min 写了 C。 然后由于脑子一抽,没想清楚就开始码 D,花了 18min 写完。 然后发现好友列表里的人好像切四题的不多,还能玩。 然后开始想 E。 一开始先去问了个是否必须是凸多边形和一个组成凹多边形(会有三种情况)的点集要被算多少次。 看到三种情况只能算一次,心态就崩了…… 5min 后才发现如果合法,恰好一种是合法的,白白浪费了一堆时间…… 然后开码。比想象中的好码。但由于计算几何,所以还是用了很长时间,大概 1h。 然后 WA9。 对拍也不太想写,就静态查错,死都查不出来。 弃疗了,水群去了。 …… 然后天真的以为 long double 就够了,又送一发罚时。 最后还是重构成了叉积+判象限的写法,终于在 2:09 过掉了。 E 的得分比 C 还少。 看起来发挥还不错,上 IM 了。 (戏剧性的一幕:tourist 最后 2min 切了 G 翻上了 rk1,predictor 显示 dls 将仍是榜二,然后 tourist 的 G FST 了) (祝贺 dls 登基,今年是 MiFaFaOvO 元年) A 入门模拟。 代码 #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int>

线段树练习3

我的梦境 提交于 2020-01-05 03:06:33
题目传送: 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

编译原理DFA(有限确定自动机)的构造

﹥>﹥吖頭↗ 提交于 2020-01-05 01:37:58
CODE: https://github.com/pxjw/Principles-of-Compiler/tree/master/consDFA 原题: 1、自己定义一个简单语言或者一个右线性正规文法 示例如( 仅供参考 ) G[S]:S→aU|bV U→bV|aQ V→aU|bQ Q→aQ|bQ|e 2、构造其有穷确定自动机,如 3、利用有穷确定自动机M=(K,Σ,f, S,Z)行为模拟程序算法,来对于任意给定的串,若属于该语言时,该过程经有限次计算后就会停止并回答“是”,若不属于,要么能停止并回答“不是” K:=S; c:=getchar; while c<>eof do {K:=f(K,c); c:=getchar; }; if K is in Z then return (‘yes’) else return (‘no’) 开始编程! 1.状态转换式构造类: current——当前状态 next——下一状态 class TransTile { public: char current; char next; char input; TransTile(char C,char I,char Ne){ current = C; next = Ne; input = I; } }; 2.DFA的构造类 此处包括DFA的数据集,字母表,以及过程P的定义。 包括了初始化,遍历转换

Are getchar() and putchar() functions or macros?

不羁岁月 提交于 2020-01-05 00:49:40
问题 I referred to two reliable sources for the information and both seems to have different definitions of the same thing: http://www.cplusplus.com/reference/clibr%E2%80%A6 http://www.ocf.berkeley.edu/~pad/tigcc/doc/html/stdio_fputchar.html The first source says putchar() is a function, as is getchar() , but in the second link it says putchar() is a macro. My book says getchar() is a macro. Which is correct? 回答1: getchar and putchar are functions, but may additionally be defined as macros.

Are getchar() and putchar() functions or macros?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-05 00:48:58
问题 I referred to two reliable sources for the information and both seems to have different definitions of the same thing: http://www.cplusplus.com/reference/clibr%E2%80%A6 http://www.ocf.berkeley.edu/~pad/tigcc/doc/html/stdio_fputchar.html The first source says putchar() is a function, as is getchar() , but in the second link it says putchar() is a macro. My book says getchar() is a macro. Which is correct? 回答1: getchar and putchar are functions, but may additionally be defined as macros.

[USACO07OPEN]便宜的回文Cheapest Palindrome/ [USACO15OPEN]回文的路径Palindromic Paths

ⅰ亾dé卋堺 提交于 2020-01-03 10:13:42
字串S长M,由N个小写字母构成。欲通过增删字母将其变为回文串,增删特定字母花费不同,求最小花费。 题目描述见上 显然 这是一道区间DP 从两头DP,枚举长度啥的很套路了。具体细节请参见代码qwq #include<iostream> #include<cstdio> #include<cstring> #define re register int #define maxn 2000+5 #define maxn1 3000+5 int val[maxn],dp[maxn1][maxn1],val1,val2; char ch[maxn1],temp[3]; using namespace std; inline int read() { int x=0,f=1; char ch=getchar(); while(!isdigit(ch)) { if(ch=='-') f=-1; ch=getchar(); } while(isdigit(ch)) { x=(x<<3)+(x<<1)+ch-'0'; ch=getchar(); } return x*f; } int main() { int n,m; memset(dp,0x3f,sizeof(dp));//必须要初始化 n=read(); m=read(); scanf("%s",ch+1); for(re i=1;i<=n;i

变治法

﹥>﹥吖頭↗ 提交于 2020-01-02 23:03:57
链接:https://ac.nowcoder.com/acm/challenge/terminal 来源:牛客网 题目描述 现在有n个数,每次随机取出两个数x,y,然后加入一个数为(x+y)/2,问最后剩下的那个数的期望是多少? 输入描述: 有多组输入数据,第一行为一个数字T,代表有T组输入数据 (0<T≤20)。 接下来为T组数据。 每组测试数据分2行: 第一行为n,表示有n个数(1≤n≤100) 接下来的一行有n个正整数ai,表示初始的n个数(1≤ai≤10000,1≤i≤n)。 输出描述: 对于每组数据,在一行上输出最后剩下数的期望值的整数部分。 示例1 输入 复制 2 3 1 1 1 2 2 3 输出 复制 1 2 思路 变治法。最后剩下数的数学期望值,即是这n个数的平均值。 # pragma GCC optimize(2) #include<bits/stdc++.h> using namespace std ; inline int read ( ) { int x = 0 , f = 1 ; char c = getchar ( ) ; while ( c != '-' && ( c < '0' || c > '9' ) ) c = getchar ( ) ; if ( c == '-' ) f = - 1 , c = getchar ( ) ; while ( c

Hdu 1062 Text Reverse

て烟熏妆下的殇ゞ 提交于 2020-01-01 22:55:31
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30766 Accepted Submission(s): 12153 Problem Description Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them. Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains a single line with several words. There will be at most 1000 characters in a line. Output For

scanf/getchar working correctly only first time through loop? [duplicate]

夙愿已清 提交于 2020-01-01 19:43:30
问题 This question already has answers here : Why doesn't getchar() wait for me to press enter after scanf()? (10 answers) Closed 9 months ago . I'm trying to have the user enter in a number as many times as they want (and create a linked list node for each of the numbers). However, I've tried multiple method of clearing the character input buffer but to no avail. Strangely, the code will execute once through but not execute correctly the second. For example, with the code below, the terminal