voidroot2(double a, double b, double c) { double x = ((-b) + sqrt(pow(b, 2) - 4 * a * c)) / 2 * a; std::cout << x << std::endl; }
voidroot3(double a, double b, double c) { std::cout << "无解" << std::endl; }
intmain() { double a, b, c; std::cout << "输入a,b,c的值:"; std::cin >> a >> b >> c; if ((b * b - 4 * a * c) > 0) root1(a, b, c); if ((b * b - 4 * a * c) == 0) root2(a, b, c); if ((b * b - 4 * a * c) < 0) root3(a, b, c); return0; }
externintpower(int a,int b); intmain() { int a, b = 4; int m; std::cin >> a >> m; std::cout << "a*b=" << a * b << std::endl; std::cout << "a^m=" << power(a, m) << std::endl; return0; }
使用headfile:
#include<iostream> #include"func.h"
intmain() { int a, b = 4; int m; std::cin >> a >> m; std::cout << "a*b=" << a * b << std::endl; std::cout << "a^m=" << power(a, m) << std::endl; return0; }
intmain() { char a = 'A', b = 'B', c = 'C'; int n; std::cin >> n; Hanoi(n, a, b, c); return0; }
函数文件
头文件:
#pragma once
intpower(int a, int b); intprime(int n); voidgoldbach(int n); inthcf(int x, int y); intlcd(int x, int y); intDigisum(int n); voidHanoi(int n, char a, char b, char c);
voidgoldbach(int n) { for (int i = 2; i <= n / 2; i++) { for (int j = 2; j <= n; j++) { if (i + j == n) if (prime(i) && prime(j)) { std::cout << n << "=" << i << "+" << j << std::endl; } } } }
voidHanoi(int n, char a, char b, char c) { if (n == 0) std::cout << a << "->" << c << std::endl; else { Hanoi(n - 1, a, c, b); std::cout << a << "->" << c << std::endl; Hanoi(n - 1, b, a, c); } }
inthcf(int x, int y) { if (y == 0) return x; returnhcf(y, x % y); }
intlcd(int x, int y) { return x * y / hcf(x, y); }
intpower(int a, int b) { int sum = 1; for (int i = 1; i <= b; i++) { sum *= a; } return sum; }
intprime(int n) { if (n <= 1) return0; for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) return0; } return1; }