getchar

Educational Codeforces Round 77 (Rated for Div. 2)

落爺英雄遲暮 提交于 2019-12-05 23:31:20
A. Heating (CF 1260 A) 题目大意:n组数据,每组数据有两个数c i 和sum i ,选择c i 个非负数a i ,使得$\sum ^{c_{i}}_{k=1}a_{k}\geq sum_{i}$,且最小化$\sum ^{c_{i}}_{k=1}a^{2}_{k}$,求最小值。 由均值不等式可知当每个a k =$\dfrac {sum_{i}}{c_{i}}$时有最小的$\sum ^{c_{i}}_{k=1}a^{2}_{k}$,但a i 需要整数,那我们先对前x个数取a+1,直到剩下的c i -x个数都取a时有$\sum ^{c_{i}}_{k=1}a_{i}=sum_{i}$,此时$\sum ^{c_{i}}_{k=1}a^{2}_{k}$最小。 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 template <typename T> 5 void read(T &x) { 6 int s = 0, c = getchar(); 7 x = 0; 8 while (isspace(c)) c = getchar(); 9 if (c == 45) s = 1, c = getchar(); 10 while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^

The C Programming Language, Ch.1 Exercise 1.10 (Getchar and Putchar)

戏子无情 提交于 2019-12-05 22:16:00
问题 I've been working on this for 2 hours and I am stuck... I found the answer online, but that isn't going to help me learn the concept that I'm obviously missing. Prompt: Write a program to copy its input to its output, replacing each tab by \t , each backspace by \b , and each backslash by \\ . This makes tabs and backspaces visible in an unambiguous way. Here's what I came up with, it doesn't replace a tab or \ with the indicated putchar , it just adds it in front of it.(I didn't do backspace

Input string with getchar

末鹿安然 提交于 2019-12-05 13:23:22
I am trying to read a string into a char array with a length chosen by the user. The problem is that getchar() doesn't stop reading until the user manually enters a newline by pressing enter, based on my code. I have read through other's threads, and I understand why I'm not able to do it this way, it's just completely contradictory to my assignment handout. int chPrompt(int nchars); void strInput(char str[], int nchars); int main(void) { int nchars = chPrompt(nchars); char str[nchars]; strInput(str, nchars); return 0; } int chPrompt(int nchars) { printf("How many chars do you need to input? >

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3)

女生的网名这么多〃 提交于 2019-12-05 12:13:30
A.Math Problem(CF 1262 A) 题目大意:给定n条线段,求一条线段,使得这个线段能够跟所有给定的线段都相交(端点值一样也算相交),最小化它的长度,可以是0. 很显然找出这n条线段的左端点最大值和右端点的最小值,它们的差和0的最大值即为答案。 1 #include <bits/stdc++.h> 2 #define MIN(a,b) (((a)<(b)?(a):(b))) 3 #define MAX(a,b) (((a)>(b)?(a):(b))) 4 using namespace std; 5 6 template <typename T> 7 void read(T &x) { 8 int s = 0, c = getchar(); 9 x = 0; 10 while (isspace(c)) c = getchar(); 11 if (c == 45) s = 1, c = getchar(); 12 while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); 13 if (s) x = -x; 14 } 15 16 template <typename T> 17 void write(T x, char c = ' ') { 18 int b[40], l = 0; 19

「杂录」CSP-S 2019 爆炸记&题解

谁说胖子不能爱 提交于 2019-12-05 09:13:08
考试状况 \(Day1\) \(8:30\) 解压,先打个含头文件和 \(freopen\) 的模板程序,准备做题。 \(8:35\) 开题,心想着按顺序做吧,毕竟难度一般是按顺序排的。 第一题,一眼看过去。 标题:格雷码 描述:格雷码是 \(balabala\) ,有个方法可以生成格雷码 \(balabala\) 数据范围: \(long\ long\) 内 求 \(n\) 位格雷码第 \(k\) 项?第一位看一下在前半还是后半,第二位递归下去……复杂度 \(O(n)\) ,没什么大问题,直接开打。 \(8:45\) 打完了,测小样例、大样例,手造两个小数据,测一下极限数据……等等,极限数据是 \(63\) 还是 \(64\) 位?看一下数据范围, \(n<=64\) ,可以等于……那就不能用 \(long\ long\) 了,赶紧改成 \(unsigned\ long\ long\) ,发现不知道怎么读入……改成 \(cin\) 。测一下最大数据,能过,好的下一道。 \(8:50\) 第二题好像蛮套路的,树上做合法括号序列……考虑到括号序列的合法判断,只需要把 \((\) 当成 \(1\) , \()\) 当成 \(-1\) ,求前缀和,满足没有任何一个位置小于 \(0\) ,并且最后的位置等于 \(0\) 就一定合法。那么考虑在树上 \(Dfs\) 维护前缀和,统计方案

【比赛题解】CSP2019 简要题解

廉价感情. 提交于 2019-12-05 04:58:15
D1T1 code 签到题,大家都会。 可以从高位往低位确定,如果遇到 \(1\) ,则将排名取反一下。 注意要开 unsigned long long 。 #include <bits/stdc++.h> typedef unsigned long long u64; const int MaxN = 100; u64 n, K; bool ans[MaxN]; inline void solve(u64 dep, u64 k) { if (dep == 0) return; u64 lsze = 1ull << (dep - 1); if (k < lsze) { ans[dep] = false; solve(dep - 1, k); } else { ans[dep] = true; solve(dep - 1, lsze - (k - lsze) - 1); } } int main() { freopen("code.in", "r", stdin); freopen("code.out", "w", stdout); std::cin >> n >> K; solve(n, K); for (int i = n; i >= 1; --i) putchar(ans[i] ? '1' : '0'); return 0; } D1T2 brackets 简单题,大家都会。

String input using getchar()

僤鯓⒐⒋嵵緔 提交于 2019-12-05 02:15:22
问题 The following code uses getchar() to accept a line of input. #include <stdio.h> #include <stdlib.h> int main() { char *rawString = (char *)malloc(200*sizeof(char)); char *rawStringInitial = rawString; char c; c=getchar(); while(c!='\n') { *rawString=c; rawString++; c=getchar(); } *rawString='\0'; printf("\n[%s]\n",rawStringInitial); return(0); } While typing, if I press backspace, shouldn't it also be received by getchar() & stored in the rawString-pointed location? However the output simply

C/C++.控制台输入(cin/getchar)

戏子无情 提交于 2019-12-04 23:35:13
1、类似的函数有:cin、getchar、fgetc 等 2、问题:   最后的"\n"都不取出来...  2.1、对策:(ZC:下面是 我自己使用后的感受)   (1)fflush(stdin) ==> 没反应...    _flushall();也没反应   (2) int _kbhit( void ); ==> 结果不准  <conio.h>    网上的资料:getchar,scanf,getch,这些,如果用户没有输入就会阻塞,如果不希望阻塞,应该怎么办?网上搜了一下,Linux可以用 fcntl ,但 Windows 就不行了(可以用 GetAsyncKeyState 检查按键,但是不能知道stdin 是否有内容)  有人答复:Windows有的。用_kbhit()(因为是立即返回的,你可能要放到循环里),判断是不是有键按下,然后用_getch()取走。   (3)cin.peek() ==> 输入缓冲区里面有数据的时候,判断结果准确的;输入缓冲区 为空的时候 该函数也会阻塞...  2.2、上面的方式 都不咋地...现在用下面的 这种方式:   每次调用完 cin、getchar、fgetc 等函数后,自己手动加上 一句"getchar()",把最后的"\n"取走...   举例子: (cin >> bufIn).get(); 3、 4、 5、 来源: https:/

各种方式输入字符串时间长短比较

让人想犯罪 __ 提交于 2019-12-04 23:25:54
测试代码: #include<bits/stdc++.h> using namespace std; const int N=1e8+50; char a[N]; inline void solve1() { gets(a); } inline void solve2() { scanf("%s",&a); } inline void solve3() { char ch; while(ch!='\n') ch=getchar(); } inline void solve4() { int i=0; while(~scanf(" %c",&a[i])) i++; } int main() { freopen("judge.txt","r",stdin); clock_t s,t; s=clock(); //solve1(); //solve2(); //solve3(); solve4(); t=clock(); cout<<t-s<<endl; return 0; } (大部分测试内容于 评测鸭 上进行) 因为cin过于鸡肋,所以并未进行测试 以下为测试内容:(单位:ms) 数据大小:10000 (gets字符串):0.040394 (scanf字符串):0.051836 (getchar单字符):0.057788 (scanf单字符):0.533744 数据大小:100000

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

混江龙づ霸主 提交于 2019-12-04 20:11:45
This question already has an answer here: Why doesn't getchar() wait for me to press enter after scanf()? 10 answers 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 reads: would you like to enter an integer? y Enter an integer: 4 would you like to enter an integer? y **program terminates** And