2 条题解

  • 3
    @ 2024-11-19 21:03:33

    老朱想买笔,有4元的,有5元的,还有6元的。 老朱想买更多的笔,该怎么办。 这道题非常简单,把你脑子里的for循环套for循环给忘掉,让我们先来分析这道题。 想买更多的笔,那应该买更多4元的笔。 题目要求,不要有余钱,那该怎么办? 假如余1元,我们可以退1支4元的笔,买一支5元的笔; 假如余2元,我们可以退1支4元的笔,买一支6元的笔; 假如余3元,我们可以退2支4元的笔,买一支5元的笔和一支6元的笔。 用if语句就可以解决。

    #include<bits/stdc++.h>
    using namespace std;
    int main ()
    {
    	int x,x4,x5,x6,a;\\x是钱数,x4是4元钱的笔的数量,以此类推。a是剩余钱数。 
    	cin>>x;
    	a=x%4;\\求剩余钱数 
    	x4=x/4;\\能买几支4元的笔 
    	if(a==0)
    	{
    		cout<<"0 0 "<<x4;
    	}
    	if(a==1)
    	{
    		cout<<"0 1 "<<x4-1;
    	}
    	if(a==2)
    	{
    		cout<<"1 0 "<<x4-1;
    	}
    	if(a==3)
    	{
    		cout<<"1 1 "<<x4-2;
    	}
    	return 0;
    }
    
  • 1
    @ 2025-7-15 9:52:35
    #include <bits/stdc++.h> 
    using namespace std;
    int main ()
    {
    	int x,x4,x5,x6,a;
    	cin>>x;
    	a=x%4;
    	x4=x/4;
    	if(a==0)
    	{
    		cout<<"0 0 "<<x4;
    	}
    	if(a==1)
    	{
    		cout<<"0 1 "<<x4-1;
    	}
    	if(a==2)
    	{
    		cout<<"1 0 "<<x4-1;
    	}
    	if(a==3)
    	{
    		cout<<"1 1 "<<x4-2;
    	}
    	return 0;
    }
    
    • 1

    信息

    编号
    2316
    时间
    1000ms
    内存
    256MiB
    难度
    3
    标签
    递交数
    118
    已通过
    53
    上传者