getchar

getchar() or putchar() keeps eating the first character of my input

狂风中的少年 提交于 2019-12-08 12:37:25
问题 edit: this question is solved. thank you for all answers This is my program: #include <stdio.h> int main(){ printf("write something : \n"); int c = getchar(); while((c = getchar()) != EOF){ if (c == ' ' || c == '\t') printf(" \n"); else putchar(c) } return 0; } everytime i run it, it works fine, but eats the first character of my input for example when i run the program the output looks like this: write something : this is a sentence. his is a sentence. the "t" is missing. why is that

多项式全家桶

限于喜欢 提交于 2019-12-08 09:29:25
多项式全家桶(更新至快速幂) 开始爆肝多项式 1. FFT快速傅里叶变换 流程: 将多项式 \(\Theta (nlog_n)\) 转成点值表示形式 进行卷积, 再 \(\Theta (nlog_n)\) 转回来 离散傅里叶变换: 朴素转为点值, 需要将一个一个x带入, 而这里傅里叶搞到了几个可以优化的复数根 利用复数, 在复平面上画出一个单位圆, 将单位圆n等分, 每一个等分点为$\omega_n^i $, 接下来将 \(\omega^1_n \omega^2_n\cdots\omega^n_n\) n个根带入, 其中 \(\omega_n^i = cos(\frac{k}{n}2\pi) + i * sin(\frac{k}{n}2\pi)\) 稍等, 还要几个小性质才可以: 性质一: \(\omega^{2k}_{2n} = \omega_n^k\) 性质二: \(\omega_n^{k+\frac{n}{2}} = - \omega_n^k\) 接下来开始快速傅里叶变换: 设A(x) = \(a_0 + a_1x + a_2x^2 + \cdots + a_{n-1}x^{n-1}\) 利用分治, 将A按x的指数分为奇偶两部分 \(A(x)=(a_0+a_2x^2+\cdots + a_{n-2}x^{n-2})+(a_1x+a_3x^3+\cdots\) $ a_{n-1

getchar not working in switch case (c)

╄→尐↘猪︶ㄣ 提交于 2019-12-07 15:47:27
Using a very simple calculator program that prompts a user for an operation to perform, followed by a prompt for two integers on which to perform this operation. The program is supposed to loop after these operations, except in the case where the user enters the character 'q', at which point the program is supposed to quit. #include <stdio.h> int main (void) { char c; int number[2], num1, num2, result; double num1d, num2d, resultd; int done=1; while(done) { printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n"); c = getchar(); printf("\tplease enter a

Input string with getchar

只谈情不闲聊 提交于 2019-12-07 07:23:39
问题 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];

CF1265B Beautiful Numbers

一笑奈何 提交于 2019-12-06 19:42:27
题意 给一个长度为 \(n\) 的排列 \(P\) ,求对于 \(1 - n\) 中的每个数 \(m\) ,是否能找到一段长度为 \(m\) 的区间使得区间内的数是一个 \(1 - m\) 的排列 输出一个 \(01\) 串,其中第 \(i\) 位表示是否能找到一段长度为 \(i\) 的区间使得区间内的数是一个 \(1 - i\) 的排列 \(n \leq 2e5\) 分析 如果对于某个数能找到一段区间使它合法,那么这个区间一定是唯一且连续的 考虑从小到大对于每个数查找它的位置并维护当前所找到的位置的最小值和最大值,即维护一下区间的左端点和右端点 当这个区间连续时,这个区间的长度 \(len\) 是合法的 于是现在变成了在一个无序的排列上查找一个数的位置,主席树+二分即可 代码 #include<bits/stdc++.h> using namespace std; inline int read() { int cnt = 0, f = 1; char c = getchar(); while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();} while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();} return cnt * f

读入优化

只谈情不闲聊 提交于 2019-12-06 15:16:52
int read( ){ int ans=0,op=1; char c; c=getchar(); for(;(c<'0'||c>'9')&&c!='-';c=getchar()); if(c=='-') op=-1,c=getchar( ); for(;(c>='0'&&c<='9');c=getchar( )) ans*=10,ans+=c-'0'; return ans*op; } c^48==c-'0' n=read(); 使用getchar() 来源: https://www.cnblogs.com/liuziwen0224/p/11992368.html

第二天打卡 (昨天cf炸了,写到一半上不去了,今天重新开一套写,晚上会补上今天改写的第三套)Educational Codeforces Round 72 (Rated for Div. 2)

旧城冷巷雨未停 提交于 2019-12-06 15:00:58
题目出的挺好的,数学知识较多。虚拟rk:801 A.Creating a Character 题意:给出力量和敏捷两个属性,现在给出技能点数,在必须使用完技能点数的情况下,有多少种情况,力量属性严格大于敏捷。 wa了4次。 细节蛮多的。可以推导出来 (力量-敏捷)+技能点数/2>点在敏捷上的技能点数 就能满足题意。 不过需要特判,敏捷额外的点数不能超过所有技能点数,而且不能为负数。 #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<climits> #include<stack> #include<vector> #include<queue> #include<set> #include<bitset> #include<map> //#include<regex> #include<cstdio> #define up(i,a,b) for(int i=a;i<b;i++) #define dw(i,a,b) for(int i=a;i>b;i--) #define upd(i,a,b) for(int i=a;i<=b;i++) #define dwd(i,a,b) for(int i=a;i>=b;i--) //#define

C++ 快读写模板

醉酒当歌 提交于 2019-12-06 12:11:12
这是目前我知道的最快的快读写了,但是我写的只有 int,要更长的话要改 long long 下面是 可以判断负数 的版本 #include<bits/stdc++.h> using namespace std; inline int read() { char c=getchar(); int x=0,f=1; while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar(); return x*f; } void write(int a) { if(a<0) putchar('-'),a=-a; if(a>=10)write(a/10); putchar(a%10+48); } int main() { int m=read(); write(m); return 0; } 下面是 不能判断负数 的版本,会更快 #include<bits/stdc++.h> using namespace std; inline int read() { char c=getchar(); int x=0; while(!isdigit(c))c=getchar(); while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c

getchar() and reading line by line

限于喜欢 提交于 2019-12-06 11:08:44
For one of my exercises, we're required to read line by line and outputting using ONLY getchar and printf. I'm following K&R and one of the examples shows using getchar and putchar. From what I read, getchar() reads one char at a time until EOF. What I want to do is to read one char at a time until end of line but store whatever is written into char variable. So if input Hello, World!, it will store it all in a variable as well. I've tried to use strstr and strcat but with no sucess. while ((c = getchar()) != EOF) { printf ("%c", c); } return 0; You will need more than one char to store a line

[题解][Codeforces]Codeforces Round #602 (Div. 1) 简要题解

主宰稳场 提交于 2019-12-06 10:36:42
orz d jq_cpp lgm A 题意 给定一个分别含有 \(\frac n2\) 个左括号和右括号的括号序列 每次可以将序列的一个区间翻转 求一个不超过 \(n\) 次的操作方案,使得操作完之后的序列是合法括号序列,并且恰好有 \(k\) 个前缀是合法括号序列 多组数据,所有数据的 \(n\) 之和不超过 \(2000\) 做法:构造 随便生成一个合法的目标序列,如 \(k-1\) 个 () 相接再接上 \(\frac n2-k+1\) 个 ( 和 \(\frac n2-k+1\) 个 ) 依次从左往右检查,对于位置 \(i\) 如果和目标序列不一样则在右边找一个 \(j\) 使得当前序列第 \(j\) 个元素和目标序列第 \(i\) 个元素相等,并翻转区间 \([i,j]\) \(O(n^2)\) 代码 #include <bits/stdc++.h> template <class T> inline void read(T &res) { res = 0; bool bo = 0; char c; while (((c = getchar()) < '0' || c > '9') && c != '-'); if (c == '-') bo = 1; else res = c - 48; while ((c = getchar()) >= '0' && c <= '9'