- problem:
給定正整數(shù)a,b,c。求不定方程 ax+by=c 關于未知數(shù)x和y的所有非負整數(shù)解組數(shù)。
- input:
一行,包含三個正整數(shù)a,b,c,兩個整數(shù)之間用單個空格隔開。每個數(shù)均不大于1000。
- output:
一個整數(shù),即不定方程的非負整數(shù)解組數(shù)。
- input demo:
2 3 18
- output demo:
4
package com.fantJ.ACM;
import java.util.Scanner;
/**
* Created by Fant.J.
* 2017/12/3 21:37
*/
public class A不定方程求解 {
public static void main(String []args){
Scanner scanner = new Scanner(System.in);
String data = scanner.nextLine();
String []datas = data.split(" ");
Integer a = Integer.valueOf(datas[0]);
Integer b = Integer.valueOf(datas[1]);
Integer c = Integer.valueOf(datas[2]);
int count = 0;
// System.out.println(c/a);
//拿到了a、b、c 的值,問題的關鍵是減少遍歷次數(shù)
for (int i = 0;i<= (c/a) ;i++){
for (int j = 0;j<= (c/b); j++){
if (a*i+b*j==c){
count++;
// System.out.println("i="+i+";j="+j);
}
}
}
System.out.print(count);
}
}