注意:使用 printf 时最好添加头文件 #include
#include
#include
using namespace std;int main()
{printf("HELLO WORLD!");return 0;
}
| 类型 | 格式 | 说明 |
|---|---|---|
| int | %d | 整型 |
| float | %f | 默认保留 6 位小数 |
| double | %lf | 默认保留 6 位小数 |
| char | %c | 回车也是一个字符,用’\n’表示 |
#include
#include
using namespace std;int main()
{int a = 3;float b = 3.12345678;double c = 3.12345678;char d = 'y';printf("%d\n", a);printf("%f\n", b);printf("%lf\n", c);printf("%c\n", d);return 0;
}
#include
#include
using namespace std;int main()
{int a = 3;float b = 3.12345678;double c = 3.12345678;char d = 'y';printf("int a = %d,float b = %f\n,double c = %lf, char d = %c\n",a, b, c, d);return 0;
}
#include
#include
using namespace std;int main()
{char c;cin >> c;printf(" %c\n",c);printf(" %c%c%c\n",c,c,c);printf("%c%c%c%c%c\n",c,c,c,c,c);printf(" %c%c%c\n",c,c,c);printf(" %c\n",c);return 0;
}
#include
#include
using namespace std;int main()
{int t;cin >> t;int hours = t / 3600;int minutes = t % 3600 / 60;int seconds = t % 60;printf("%d:%d:%d\n",hours, minutes, seconds);return 0;
}
#include
#include
using namespace std;int main()
{float b = 3.12345678;double c = 3.12345678;printf("%.4f\n",b);printf("%.3lf\n",c);return 0;
}
#include
#include
using namespace std;int main()
{int a = 3;float b = 3.12345678;double c = 3.12345678;printf("%5d\n",a);printf("%8.4f\n",b);printf("%7.3lf\n",c);return 0;
}
#include
#include
using namespace std;int main()
{int a = 3;float b = 3.12345678;double c = 3.12345678;printf("%-5d\n",a);printf("%-8.4f\n",b);printf("%-7.3lf\n",c);return 0;
}
#include
#include
using namespace std;int main()
{int a = 3;float b = 3.12345678;double c = 3.12345678;printf("%05d\n",a);printf("%08.4f\n",b);printf("%07.3lf\n",c);return 0;
}
#include
#include
using namespace std;int main()
{int a = 3;cin >> a;if(a > 5){printf("%d is big!\n",a);printf("%d + 1 = %d\n",a ,a+1);}else{printf("%d is small!\n",a);printf("%d - 1 = %d\n",a ,a-1);}return 0;
}
#include
#include
using namespace std;int main()
{int a = 3;cin >> a;if(a > 5){printf("%d is big!\n",a);printf("%d + 1 = %d\n",a ,a+1);}return 0;
}
#include
#include
using namespace std;int main()
{int a = 3;cin >> a;if(a > 5)printf("%d is big!\n",a);elseprintf("%d is small!\n",a);return 0;
}
#include
#include
using namespace std;int main()
{int x;cin >> x;if(x > 0) cout << x << endl;else cout << -x << endl;return 0;
}
#include
#include
using namespace std;int main()
{int a, b;cin >> a >> b;if(a > b)cout << a << endl;elsecout << b <
#include
#include
using namespace std;int main()
{int a, b, c;cin >> a >> b >> c;if(a > b){if (a > c) cout << a << endl;else cout << c << endl;}else{if (b > c) cout << b << endl;else cout << c << endl;}return 0;
}
| 含义 | 符号 |
|---|---|
| 大于 | > |
| 小于 | < |
| 大于等于 | >= |
| 小于等于 | <= |
| 等于 | == |
| 不等于 | != |
#include
#include
using namespace std;int main()
{int s;cin >> s;if(s >= 85){printf("A");}else if (s >= 70){printf("B");}else if(s >= 60){printf("C");}else{printf("D");}return 0;
}

#include
#include
using namespace std;int mian()
{int year;cin >> year;if(year % 100 == 0){if(year % 400 == 0) cout << "yes" << endl;else cout << "no" endl;}else{if(year % 4 == 0) cout << "yes" << endl;else cout << "no" endl;}
}
return 0;
| 含义 | 符号 |
|---|---|
| 与 | && |
| 或 | || |
| 非 | ! |
#include
#include
using namespace std;int main()
{int a, b, c;cin >> a >> b >> c;if(a > b && a > c) cout << a << endl;else if(b > a && b > c) cout << b << endl;else cout << c << endl;}return 0;
}
#include
#include
using namespace std;int mian()
{int year;cin >> year;if(year % 100 == 0 && year % 400 == 0 || year % 4 == 0 && year % 100 != 0) cout << "yes" << endl;else cout << "no" endl;}
}
return 0;
上一篇:一篇读懂|Linux系统平均负载
下一篇:梯度多云管理技术架构的优势