迅雷编程
题目一 方程表达式计算
#include <iostream>
#include <string>
using namespace std;
// ax + b 的形式
struct Value
{
int v1; // a
int v0; // b
};
bool isnum(char c)
{
return c >= '0' && c <= '9';
}
int toint(char c)
{
return c - '0';
}
int main()
{
string eq;
//eq = "6x-5-x=2-2x";
cin >> eq;
// 消除空x
for (int i = 0; i < eq.size(); i++)
{
if (eq[i] == 'x' && (i == 0 || !isnum(eq[i - 1])))
eq.insert(i, 1, '1');
}
//cin >> eq;
Value values[2]{0};
for (int i = 0, j = 0; i < eq.size(); i++)
{
char c = eq[i];
if (c == '=')
{
j = 1;
continue;
}
int num = 0;
if (isnum(c))
num = toint(c);
else if (c == '+')
num = toint(eq[++i]);
else if (c == '-')
num = -toint(eq[++i]);
if (eq[i + 1] == 'x')
{
i++;
values[j].v1 += num;
}
else
values[j].v0 += num;
}
// solve equetion
double A = values[0].v1 - values[1].v1;
double B = values[1].v0 - values[0].v0;
if (A != 0) // 唯一解
cout << "x=" << (int)(B / A) << endl;
else if (A == 0 && B == 0)
cout << "Infinite solutions" << endl;
else if (A == 0 && B != 0)
cout << "No solution" << endl;
}
题目二 和组合数
import java.util.LinkedList;
import java.util.Scanner;
public class Main { private static LinkedList<Integer> list = new LinkedList<Integer>(); static int c = 0; static void myPrint() { c++; } public static void findSum(int sum, int n) { if (n >= 0 && sum == 0) { myPrint(); return; } if (n >= 1 && sum == 1) { list.push(1); myPrint(); list.pop(); return; } if (n == 1 && sum > 1) return; if (sum >= n) { list.push(n); findSum(sum - n, n - 1); list.pop(); findSum(sum, n - 1); } else { findSum(sum, sum); } return; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); findSum(m, n); System.out.println(c); }
}
#迅雷#