百马百瓦问题(Java版,c++版)

(时间限制:3000MS 内存限制:32768KB)

描述

共有100匹马驮100块瓦,大马驮m块,小马驮n块,两个马驹驮一块。大马、小马、马驹的匹数会有多种方案,请问共有多少种方案?

输入

输入数据有多组,在一行上输入两个正整数m和n(0<m,n<10)。

输出

在一行上输出合理方案的个数,若不存在则输出"no solution"。

难度

入门

输入示例

3 2
1 1
2 2

输出示例

7
101
no solution

分析思路:

 JavaAC代码:

import java.util.Scanner;


public class test2022 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
      Scanner sc=new Scanner(System.in);
    
     while (sc.hasNextInt()) {
    	int n=sc.nextInt();
		int m=sc.nextInt(); 
		 int j,i,count=0,k;
   for(i=0;i<=100;i++)
   {
	  for(j=0;j<=100;j++)
	  {
		k=200-2*n*i-2*m*j;
		if(i+j+k==100) {
			count++;}
	  }
   }
   if(count!=0)
      System.out.printf("%d\n",count);
   if(count==0)
	System.out.printf("no solution\n");
}
      
	}
}

c++AC代码:

#include <iostream>

using namespace std;

int main()
{
    int m,n;
    while(cin>>m>>n)
    {
        int count=0,k;
        for(int i=0; i<=100; i++)
        {
            for(int j=0; j<=100; j++)
            {
                k=200-2*n*i-2*m*j;
                if(i+j+k==100)
                {
                    count++;
                }
            }
        }
        if(count!=0)
            cout<<count<<endl;
        else
            cout<<"no solution"<<endl;
    }
    return 0;
}

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值