1001-1002-1003-1004-1005

阅读量: 371 编辑

1001-Hello,World!

编写一个能够输出“Hello,World!”的程序。

//爱码岛编程 
#include <iostream>
using namespace std;

int main() {
  cout << "Hello,World!";
  return 0;
}

1002-输出第二个整数

输入三个整数,将第二个整数输出

//爱码岛编程 
#include <iostream>
using namespace std;

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  cout << b;
  
  return 0;
}

1003-对齐输出

读入三个整数,按每个整数占8个字符的宽度,右对齐输出它们,按照格式要求依次输出三个整数,之间以一个空格分开。

//爱码岛编程 
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
  cout.setf(ios::right);

  int a, b, c;
  cin >> a >> b >> c;
  cout << setw(8) << a << " " << setw(8) << b << " " << setw(8) << c;

  return 0;
}

或者用 C 语言格式化输出

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  printf("%8d %8d %8d", a, b, c);
  return 0;
}

1004-字符三角形

给定一个字符,用它构造一个底边长5个字符,高3个字符的等腰字符三角形。

//爱码岛编程 
#include <iostream>
using namespace std;

int main() {
  char c;
  cin >> c;

  cout << "  " << c << endl;
  cout << " " << c << c << c << endl;
  cout << c << c << c << c << c << endl;

  return 0;
}

或者用 C 语言格式化输出

#include <cstdio>
#include <iostream>
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", c, c, c, c, c);
  return 0;
}

1005-地球人口承载力估计

假设地球上的新生资源按恒定速度增长。照此测算,地球上现有资源加上新生资源可供 x 亿人生活 a 年,或供 y 亿人生活 b 年。为了能够实现可持续发展,避免资源枯竭,地球最多能够养活多少亿人?

//爱码岛编程
#include <iostream>
using namespace std;

int main() {
  cout.flags(ios::fixed);
  cout.precision(2);
  
  int x, a, y, b;
  double z;
  cin >> x >> a >> y >> b;
  z = (1.0 * a * x - b * y) / (1.0 * a - b);
  cout << z;

  return 0;
}

或者用 C 语言格式化输出

//爱码岛编程
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int x, a, y, b;
  double z;
  cin >> x >> a >> y >> b;
  z = (1.0 * a * x - b * y) / (1.0 * a - b);
  printf("%.2f", z);

  return 0;
}

爱码岛编程公众号
试卷资料
爱码岛编程小程序
在线刷题
苏ICP备13052010号
©2023 南京匠成信息科技有限公司