迅雷编程

题目一 方程表达式计算



#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);     }

}

#迅雷#
全部评论
第二题ac了吗?
点赞 回复 分享
发布于 2017-09-19 20:45
package com.my; import java.util.LinkedList; import java.util.Scanner; public class AAA {     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) {          Scanner in = new Scanner(System.in);          int n = in.nextInt();         int m = in.nextInt();         findSum(m, n);         System.out.println(c);     } } 整理了下,是对的。。。。收下我的膝盖
点赞 回复 分享
发布于 2017-09-19 20:54
import java.util.Scanner; public class A {     public static void main(String[] args) { Scanner in = new Scanner(System.in);      int n = in.nextInt();       int m = in.nextInt();         System.out.println(sum(n, m));     }          static int sum(int n, int m) {         if (m < 0)             return 0;         if (m == 0)             return 1;         if (m > 0 && n <= 0)             return 0;         int sum = 0;         for (int i = 1; i <= n; i++) {             sum += sum(i - 1, m - i);         }         return sum;     } }
点赞 回复 分享
发布于 2017-09-19 22:26

相关推荐

M_bao:换个排版吧哥们,看着费劲
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务