整型数据类型存储空间的大小。
#include <iostream> #include <cstdio> using namespace std; int main(){ char a; cout << "char size is " << sizeof(a) << " bytes." << endl; short b; cout << "short size is " << sizeof(b) << " bytes." << endl; int c; cout << "int size is " << sizeof(c) << " bytes." << endl; long d; cout << "long size is " << sizeof(d) << " bytes." << endl; long int e; cout << "long int size is " << sizeof(e) << " bytes." << endl; long long f; cout << "long long size is " << sizeof(f) << " bytes." << endl; long long int g; cout << "long long int size is " << sizeof(g) << " bytes." << endl; return 0; }
大小写字母转换。
#include <iostream> #include <cstdio> using namespace std; int main(){ char c1='a'; char c2='A'; cout << c1 << " " << c2 << endl; c1 = c1 -32; c2 = c2 + 32; cout << c1 << " " << c2 << endl; return 0; }
#include<iostream> using namespace std; int main() { int i; short s; cout << sizeof(i)<< " " << sizeof(s) << endl; return 0; }
#include<iostream> using namespace std; int main() { float f; double d; cout << sizeof(f)<< " " << sizeof(d) << endl; return 0; }
#include<iostream> using namespace std; int main() { bool b; char c; cout << sizeof(b)<< " " << sizeof(c) << endl; return 0; }
#include<iostream> using namespace std; int main() { int a = 32768; short b = a; cout << "D C" <<endl; //printf("%d %d\n", a, b); return 0; }
#include<iostream> using namespace std; int main() { double a = 1.000000001; float b = a; cout << "F E" <<endl; //printf("%.9f %.9f\n", a, b); return 0; }
#include <iostream> using namespace std; int main(){ float a; cin >> a; cout << (int)a << endl; return 0; }
#include<iostream> using namespace std; int main() { char c; scanf("%c",&c); cout << (int)c << endl; return 0; }
#include<iostream> using namespace std; int main() { int c; cin >> c; cout << (char)c << endl; return 0; }
#include<iostream> using namespace std; int main() { int i; cin >> i; bool b = i; int j = b; cout << j << endl; return 0; }
#include<iostream> using namespace std; int main() { cout << sizeof("Hello, World!") << endl; return 0; }
利用getchar函数接收键盘输入
#include <iostream> #include <cstdio> using namespace std; int main(){ char ch = getchar(); cout << "input=" << ch << endl; return 0; }
利用putchar函数输出字符
#include <iostream> #include <cstdio> using namespace std; int main(){ char c='B'; putchar(c); putchar('\x42'); putchar(0x42); putchar(66); return 0; }
某幼儿园里,有5个小朋友编号为1、2、3、4、5,他们按自己的编号顺序围坐在一张圆桌旁。
他们身上都有若干个糖果(键盘输入),现在他们做一个分糖果游戏。从1号小朋友开始,将自己的糖果均分三份(如果有多余的糖果,则立即吃掉),自己留一份,其余两份分给他的邻居的两个小朋友。接着2号,3号,4号,5号小朋友同样这么做。问一轮后,每个小朋友手上分别有多少糖果?
#include <iostream> #include <cstdio> using namespace std; int main(){ int a,b,c,d,e; scanf("%d%d%d%d%d",&a,&b,&c,&d,&e); a=a/3;b=b+a;e=e+a; b=b/3;c=c+b;a=a+b; c=c/3;d=d+c;b=b+c; d=d/3;e=e+d;c=c+d; e=e/3;a=a+e;d=d+e; printf("%5d%5d%5d%5d%5d\n",a,b,c,d,e); return 0; }
#include <iostream> using namespace std; int main() { float a; cin >> a ; printf("%.3f",a); return 0; }
#include <iostream> using namespace std; int main() { double a; cin >> a ; printf("%.12f",a); return 0; }
#include <iostream> using namespace std; int main() { char a; int b; float c; double d; cin >> a >> b >> c >> d; printf("%c %d %f %f",a,b,c,d); return 0; }
#include <iostream> using namespace std; int main() { double a; cin >> a ; printf("%f\n",a); printf("%.5f\n",a); printf("%e\n",a); printf("%g\n",a); return 0; }
#include <iostream> int main() { char ch; scanf("%c", &ch); printf(" %c\n",ch); printf(" %c%c%c\n",ch,ch,ch); printf("%c%c%c%c%c\n",ch,ch,ch,ch,ch); printf(" %c%c%c\n",ch,ch,ch); printf(" %c\n",ch); return 0; }
#include <iostream> using namespace std; int main(){ cout << " ********" << endl; cout << " ************" << endl; cout << " ####....#." << endl; cout << " #..###.....##...." << endl; cout << " ###.......###### ### ### ### ###" << endl; cout << " ........... #...# #...# #...# #...#" << endl; cout << " ##*####### #.#.# #.#.# #.#.# #.#.#" << endl; cout << " ####*******###### #.#.# #.#.# #.#.# #.#.#" << endl; cout << " ...#***.****.*###.... #...# #...# #...# #...#" << endl; cout << " ....**********##..... ### ### ### ###" << endl; cout << " ....**** *****...." << endl; cout << " #### ####" << endl; cout << " ###### ######" << endl; cout << "############################################################## ##################################" << endl; cout << "#...#......#.##...#......#.##...#......#.##------------------# #...#......#.##------------------#" << endl; cout << "###########################################------------------# ###############------------------#" << endl; cout << "#..#....#....##..#....#....##..#....#....##################### #..#....#....#####################" << endl; cout << "########################################## #----------# ############## #----------#" << endl; cout << "#.....#......##.....#......##.....#......# #----------# #.....#......# #----------#" << endl; cout << "########################################## #----------# ############## #----------#" << endl; cout << "#.#..#....#..##.#..#....#..##.#..#....#..# #----------# #.#..#....#..# #----------#" << endl; cout << "########################################## ############ ############## ############" << endl; return 0; }