【Educoder作业】C&C++基本输入输出
Fairing Winds and Following Seas ——一路顺风
T0 Hello World!
每一段伟大的旅途都是从 H e l l o W o r l d ! Hello\ World! Hello World!开始的
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "Hello World!" << endl ;
return 0;
}
T1 重要的事情说三遍
分成三遍 p u t c h a r putchar putchar即可。
// 包含标准输入输出函数库
#include <stdio.h>
// 定义main函数
int main()
{
// 请在下面编写将字符输出三遍的程序代码
/********** Begin *********/
char c = getchar();
putchar(c), putchar(c), putchar(c), putchar('!');
/********** End **********/
return 0;
}
T2 整数四则运算表达式的输出格式控制
这就是 s c a n f scanf scanf和 p r i n t f printf printf的标准输入输出格式,留以参考。
//包含标准输入输出函数库
#include <stdio.h>
int main()
{
//声明两个整型变量,用于存储输入的两个整数
int x,y;
//请在Begin-End之间添加你的代码,按要求格式输出四则运算式子
/********** Begin *********/
scanf("%d%d", &x, &y);
printf("%5d + %-5d = %10d\n", x, y, x + y);
printf("%5d - %-5d = %10d\n", x, y, x - y);
printf("%5d * %-5d = %10d\n", x, y, x * y);
printf("%5d / %-5d = %10d\n", x, y, x / y);
/********** End **********/
return 0;
}
T3 你好,生日
按照要求输出即可,记得加空格。
// 包含I/O流库iostream
#include <iostream>
// 加载名字空间std
using namespace std;
int main()
{
// 声明三个变量,分别用来存储年、月、日
int y, m, d;
// 请在Begin-End之间添加你的代码,输入你的生日,并按指定格式输出信息。
/********** Begin *********/
cin >> y >> m >> d ;
cout << "Hello! " << m << ' ' << d << ' ' << y << endl ;
/********** End **********/
return 0;
}
T4 不同精度的PI
我是第一次见流控制,因为在程序设计中 c i n cin cin和 c o u t cout cout往往用的比较少,一是大多的输出都是需要标准化的,二是这俩哥们常熟很大,非常慢。
这个题有一个问题,就是 n n n如果是 0 0 0的话,按照题目描述里面的 s e t p r e c i s i o n setprecision setprecision的方式输出的话需要进行 i f if if的判断,可能有更好的方法。
#include <iostream>
// 包含流操作算子库
#include <iomanip>
using namespace std;
// 定义常量PI,后面可以直接用PI代替后面的数值
#define PI 3.14159265358979323846
int main()
{
int n;
// 请在Begin-End之间添加你的代码,输入n,按不同的精度输出 PI。
/********** Begin *********/
cin >> n ;
n ++ ;
if (n == 1) cout << 3 << endl ;
else cout << setiosflags(ios :: showpoint) << setprecision(n) << PI << endl ;
cout << setiosflags(ios :: showpoint) << setprecision(n + 1) << PI << endl ;
cout << setiosflags(ios :: showpoint) << setprecision(n + 2) << PI << endl ;
cout << setiosflags(ios :: showpoint) << setprecision(n + 3) << PI << endl ;
cout << setiosflags(ios :: showpoint) << setprecision(n + 4) << PI << endl ;
/********** End **********/
return 0;
}