cout

Educational Codeforces Round 77 (Rated for Div. 2)

微笑、不失礼 提交于 2019-12-06 01:51:41
传送门 感觉最近写代码的状态有点迷...还好这次最后两分钟过了D,不然就掉分了QAQ。 A. Heating 签到。 Code /* * Author: heyuhhh * Created Time: 2019/11/27 21:53:49 */ #include <iostream> #include <algorithm> #include <vector> #include <cmath> #include <set> #include <map> #include <iomanip> #define MP make_pair #define fi first #define se second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define INF 0x3f3f3f3f #define Local #ifdef Local #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0) void err() { std::cout << '\n'; } template<typename T, typename...Args> void err(T a, Args...args) {

print double with precision 4 using cout [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-05 17:00:33
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Convert a double to fixed decimal point in C++ Suppose , I have double a = 0 and I want to print it as 0.0000 . I've tried this : cout.precision(4) ; cout<<a<<endl ; but it gaves 0 as the output. 回答1: Just try: #include <iomanip> ... cout << fixed << setprecision(4); cout << a << endl; See here. 回答2: #include <iomanip> #include <iostream.h> int main() { double a = 0.00; // print a double, 2 places of precision

选择排序

拈花ヽ惹草 提交于 2019-12-05 15:06:20
直接插入排序 目录 简述 步骤 代码 back 简述 直接插入排序是一种简单的排序,基本操作就是,从一堆数中选择一个数,然后插入到排好序的列表中(本列表)。这种方法也称归位。直接插入排序,每一次选择数字,都是一种归位。 举个例子,就好比如,你在打扑克,然后你抽到了一堆扑克牌,你要对它进行排序,你会很直接地,从一边或者中间选择一张扑克牌,然后根据数值大小,进行插入排序。选出的牌,你会很自然的插入到比它小的前面。不一会,你的手中的扑克牌就完成了排序。 算法的步骤 以下是《数据结构C语言版》的算法步骤: 设待排序的记录存放在数组r[1..n]中,r[1]是一个有序序列。 循环n-1c次,每次使用顺序查找法,查找r[i] (i=2,...n)在已排好序的序列r[1...i-1]中的插入位置,然后将r[i]插入表长为i-1的有序序列r[1...i-1],直到r[n]插入表长为n-1的有序序列r[1...n-1],最后得到一个表长为n的有序序列。 我的理解: 按要求选出一个最大或者最小的值,然后放到最前面,再从剩下的几个选出最大或者最小的,重复刚刚的操作,一直到整个数组有序 特性 直接插入法的平均时间复杂度是O(n^2^),其空间复杂度是S(1)。 主要特点是: 是稳定的 算法比较简便,而且容易书写 适用于链表的结构 对基本有序的情况的话,更为适合使用。但是,如果基数非常大,那么复杂度比较高

Correctly pad negative integers with zeros with std::cout

亡梦爱人 提交于 2019-12-05 14:03:10
问题 I found this question already asked, but the answer everybody gives is std::cout << std::setw(5) << std::setfill('0') << value << std::endl; which is fine for positive numbers, but with -5, it prints: 000-5 Is there a way to make it print -0005 or to force cout to always print at least 5 digits (which would result in -00005) as we can do with printf? 回答1: std::cout << std::setw(5) << std::setfill('0') << std::internal << -5 << '\n'; // ^^^^^^^^ Output: -0005 std::internal Edit: For those

C++ print value of a pointer

可紊 提交于 2019-12-05 13:20:45
I have an array of double pointers, but every time I try do print one of the values the address gets printed. How do I print the actual value? cout << arr[i] ? cout << &arr[i] ? they both print the address Does anyone know? If it's really an array of (initialized) double pointers, i.e.: double *arr[] = ... // Initialize individual values all you need is: cout << *arr[i]; cout << *(arr[i]) will print the value. cout << *(arr[i]); If "arr" is declared as double* arr[..]; Then you would use: cout << *(arr[i]) 来源: https://stackoverflow.com/questions/2485565/c-print-value-of-a-pointer

string中的erase()函数

我只是一个虾纸丫 提交于 2019-12-05 10:52:52
erase()是对string类型的字符串进行删除元素操作的函数 1、erase(int index) 删除下标从index开始直到字符串结尾的元素 1 string s = "1232157"; 2 s.erase(3); 3 cout << s;//123 2、erase(int index,int num) 删除下标从index开始的num个元素 string s = "1234567"; s.erase(3,2); cout << s;//12367 3、 erase(string::iterator it) 删除迭代器指向的元素,函数的返回值是指向删除元素的下一个元素的迭代器 string::iterator it; string s = "1234567"; it=s.erase(s.begin()+1); cout << s << endl;//134567 cout << *it;//3 4、erase(string::iterator it1,string::iterator it2) 删除[it1,it2)区域的元素,函数的返回值是指向删除元素的下一个元素的迭代器 string::iterator it; string s = "1234567"; it=s.erase(s.begin()+1,s.end()-1); cout << s << endl;//17

Simple Detached pthread does not cancel! (cout blocks and interleaves even if mutexed)

ぃ、小莉子 提交于 2019-12-05 08:15:05
问题 I have a hard problem here, which I can not solve and do not find the right answer on the net: I have created a detached thread with a clean up routing, the problem is that on my Imac and Ubuntu 9.1 (Dual Core). I am not able to correctly cancel the detached thread in the fallowing code: #include <iostream> #include <pthread.h> #include <sched.h> #include <signal.h> #include <time.h> pthread_mutex_t mutex_t; using namespace std; static void cleanup(void *arg){ pthread_mutex_lock(&mutex_t);

Visual C++ 2012 cout crashes during run time

荒凉一梦 提交于 2019-12-05 06:12:20
I decided to try Visual Studio 2012 Express today. First thing to do was to write "Hello world!" application, however, I couldn't make it work. I created a Windows console application project, wrote standard code and it resulted in a run-time error. Here's my code: #include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; system("pause"); return 0; } Looks like something is broken (maybe I missed something?). It gets a runtime error at line 7: http://img443.imageshack.us/img443/7497/coutbroken.png Any help please? :) Your CPU is in urgent need of being junked. Your

Calls to flush cout are ineffective

≡放荡痞女 提交于 2019-12-05 04:17:46
I am attempting to have the cout buffer flush to view a string before I manipulate it. Ive attempted to flush the buffer with calls to both std::flush() and std::cout.flush() but neither actually flush my output. Only a call to std::endl has successfully flushed the buffer for me. Here is my code std::istringstream stm (game.date()); int day, month, year; char delim = '/'; std::cout << "Date before: " << game.date() << std::flush; // first flush attempt std::cout.flush(); // second flush attempt doesnt work //std::cout << std::endl; // if this is executed the buffer will flush // Split date

HDU 1880 Hash table with chaining

巧了我就是萌 提交于 2019-12-05 03:56:06
HDU 1880 #include <bits/stdc++.h> #define DBG(x) cerr << #x << " = " << x << endl using namespace std; typedef long long LL; const unsigned int KEY = 137; const int BUCKET_NUM = 120000; struct Hash { vector<pair<string, string>> h[BUCKET_NUM]; unsigned int get_hash(const string &s) { unsigned int hs = 0; for (auto c : s) hs = (hs * KEY + c) % BUCKET_NUM; return hs; } void insert(const string &k, const string &v) { unsigned int hs = get_hash(k); h[hs].push_back(make_pair(k, v)); } string *get(const string &key) { unsigned int hs = get_hash(key); for (auto &kv : h[hs]) { if (kv.first == key)