首页 > 试题广场 >

快到碗里来

[编程题]快到碗里来
  • 热度指数:3430 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
小喵们很喜欢把自己装进容器里的(例如碗),但是要是碗的周长比喵的身长还短,它们就进不去了。

现在告诉你它们的身长,和碗的半径,请判断一下能否到碗里去。

输入描述:
输入有多组数据。

每组数据包含两个整数n (1≤n≤2^128) 和r (1≤r≤2^128),分别代表喵的身长和碗的半径。

圆周率使用3.14。


输出描述:
对应每一组数据,如果喵能装进碗里就输出“Yes”;否则输出“No”。
示例1

输入

6 1
7 1
9876543210 1234567890

输出

Yes
No
No

python solution

import sys
for i in sys.stdin.readlines():
    a,b=map(int,i.strip().split())
    print("Yes" if a<2*b*3.14 else "No")
发表于 2017-11-16 12:08:24 回复(0)
long double防止溢出。
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <exception>

using namespace std;

int main(int argc, char** argv)
{
	long double n,r;
	while (cin >> n >> r)
	{
		if (2 * r * 3.14 >= n) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	return 0;
}

发表于 2017-07-08 15:44:17 回复(0)
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			Double n = sc.nextDouble();
			Double r = sc.nextDouble();
			if(n < 2 * 3.14 * r) System.out.println("Yes");
			else System.out.println("No");
		}
	}
}

发表于 2016-10-15 23:14:26 回复(1)
#include <stdio.h>
#include <stdlib.h>

#define P  3.14
#define CIRCUMFERENCE(r)  (2*(P)*(r))  

int is_completion(const int height, const int radius)
{
	int res = 0;

	res = ((CIRCUMFERENCE(radius) < height) ? (1 : 0));

	return res;
}


int main(int argc, char const *argv[])
{
	/* code */
	int res;
	int height, radius;

	printf("please input the height of cat, and input the radius:");
	scanf("%d,%d", &height, &radius);


	res = is_completion(&height, &radius);
	if (res)
	{
		/* code */
		printf("NO\n");
	}
	else{
		printf("YES\n");
	}

	return 0;
}

编辑于 2015-08-31 17:04:54 回复(0)
正确的做法是使用大数乘法,明显n和r的精度已经超出了内置类型的表示范围了。
#include <iostream>
#include <string>
using namespace std;

string times628(string & str)
{
	string ret(str.length() + 3, 0);
	string str628 = "628";
	int strL = str.length();
	int retL = ret.length();
	int c = 0;

	for (int k = 2; k >= 0; --k)
		for (int i = strL - 1, j = retL + k - 3; i >= 0; --i, --j)
			ret[j] += (str[i] - '0') * (str628[k] - '0');
	
	for (int i = retL; i >= 0; --i)
	{
		ret[i] += c;
		c = ret[i] / 10;
		ret[i] = ret[i] % 10 + '0';
	}

	int index = 0;
	while (ret[index] == '0') ++index;

	return string(ret.begin() + index, ret.end() - 2);
}

int compare(string & a, string & b)
{
	if (a.length() != b.length()) 
		return (a.length() > b.length() ? 1 : -1);
	else
	{
		for (int i = 0; i < a.length(); ++i)
			if (a[i] == b[i]) continue;
			else return (a[i] > b[i] ? 1 : -1);
		return 0;
	}
}

int main()
{
	string n, r;
	while (cin >> n >> r)
	{
		r = times628(r);
		if (compare(n, r) == 1) cout << "No" << endl;
		else cout << "Yes" << endl;
	}

	return 0;
}

发表于 2015-12-14 19:27:16 回复(1)
#include <iostream>
using namespace std;
int main(){
    double n,r;
    while(cin>>n>>r){
        if(n>2*r*3.1415)
            cout<<"No"<<endl;
        else
            cout<<"Yes"<<endl;
    }
}

发表于 2018-09-18 00:22:14 回复(4)
#include<iostream>
using namespace std;
int main()
{
    long double n;
    long double r;
    while(cin>>n>>r)
    {
        if(n<=2*3.14*r)
        {
            cout<<"Yes"<<endl;
        }
        else
        {
            cout<<"No"<<endl;
        }
    }
}

发表于 2018-09-06 14:34:28 回复(0)
还是我实诚,手写了一个链表实现乘法

import java.util.LinkedList;
import java.util.Scanner;

public class Main {

    //小猫咪钻罐子
     public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            //猫
            String s1 = scanner.next();
            //罐子
            String s2 = scanner.next();
            LinkedList<Integer> list1 = new LinkedList<>();
            LinkedList<Integer> list2 = new LinkedList<>();
            for (int i = 0; i < s1.length(); i++) {
                list1.add(s1.charAt(i)-'0');
            }
            for (int i = 0; i < s2.length(); i++) {
                list2.add(s2.charAt(i)-'0');
            }
            int size = list2.size()-1;//对应最后一个元素得下标
            for (int i = size; i >= 0; i--) {
                //将size下标得值*2
                list2.set(i,list2.get(i)*2);

            }
            //再全部进数
            for (int i = size; i >= 0 ; i--) {
                if(list2.get(i) >= 10){
                    //进位,将前面得值+1
                    if(i == 0){
                        //如果是头插就插入1
                        list2.addFirst(1);
                        list2.set( 1 ,list2.get(1) % 10);
                    }else {
                        //如果不是头插就修改值
                        list2.set( i-1 ,list2.get(i-1)+1);
                        //进位后将当前值%10
                        list2.set( i ,list2.get(i) % 10);

                    }
                }
            }
          //  System.out.println(list2);
            size = list2.size()-1;//重新更新下size的值
            //Πd,此时d处理了,就差*Π了
            //*314
            LinkedList<Integer> list = new LinkedList<>();
            int[]pai = {3,1,4};
            int n1 = 0;//记录相加
            int n2 = 0;//记录相乘
            int z = 0;//一直记录倒数第二个位置
            for (int i = 2; i >= 0 ; i--) {
                n1++;
                n2 = n1;
                for (int j = size; j >= 0 ; j--) {
                    z = list.size()-n2;
                    int p = list2.get(j);
                    //说明此时没有对应的值
                    if(z == -1){
                        list.addFirst(p * pai[i]);
                        z++;
                    }else {
                        int tmp = list.get(z);
                        list.set(z,p * pai[i]+tmp);
                    }
                    if(list.get(z) >= 10){
                        //如果为0,说明需要头插
                        if(z == 0){
                            list.addFirst(list.get(z)/10);
                            z++;
                        }else {
                            //如果不为0,就给前面的值加上进的位数
                            int tmp = list.get(z-1);
                            list.set(z-1,list.get(z) / 10+tmp);
                        }
                        list.set(z,list.get(z) % 10);
                    }
                    n2++;

                }
            }
          //  System.out.println(list);
            //做个标记,记录这个数组从哪里开始是小数
            int len = list.size()-2;
            //从后往前数,去二位0
            int i = 0;
            while (i < 2){
                //获取后数两位
                //如果是0就删除
                if(list.get(list.size()-1) == 0){
                    list.pollLast();

                }
                i++;
            }
            size = 0;//记录次数
            //类似于 12   111
            if(list1.size() > len){
                System.out.println("No");
            }else if(list1.size() < len){
                System.out.println("Yes");
            }else {
                //两个一样长时

                while (list1.size() != 0 && list.size() !=0){
                    //如果猫的身长小于碗的周长
                    if(list1.peekFirst() < list.peekFirst()){
                        System.out.println("Yes");
                        break;
                    }else if(list1.peekFirst() > list.peekFirst()){
                        System.out.println("No");
                        break;
                    }else {
                        list1.pollFirst();
                        list.pollFirst();
                    }
                    size++;
                }
                if(list1.size() == 0 && (list.get(0)!=0 || list.get(1)!=0))
                    System.out.println("Yes");
            }
        }

    }
}

发表于 2023-04-18 16:45:48 回复(0)
这里需要注意的是变量的类型,使用double,如果使用int太短,若使用long long虽可以转换为double但  乘的是3.14无法计算浮点数
#include <iostream>
using namespace std;
int main(){
    double l, r;
    while(cin >> l >> r){
        if(l <= 3.14 * 2 * r){
            cout << "Yes" << endl;
        }
        else{
            cout << "No" << endl;
        }
    }
    return 0;
}

发表于 2020-07-13 15:23:31 回复(0)
while True:
    try:
        n,r = map(int,input().split())
        peri = 3.14 * 2 * r 
        if (peri < n):
            print("No")
        else:
            print("Yes")
    except:
        break 

发表于 2019-12-06 11:32:21 回复(0)
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
void getcomp(string a, string b) {
    if (a.length() < b.length()) {
        cout << "Yes" << endl;
    } else if (a.length() == b.length()) {
        auto lt = a.begin();
        auto lb = b.begin();
        while (lt != a.end()) {
            if (*lt > *lb) {
                cout << "No" << endl;
                break;
            }
            if(*lt<*lb){
                cout << "Yes" << endl;
                break;
            }
            lt++;
            lb++;
            if(lt==a.end()&&lb==b.end()){
                cout<<"Yes"<<endl;
            }
        }
    } else
        cout << "No" << endl;
}
string getsum(string a, string b) {
    auto la = a.begin();
    auto lb = b.begin();
    string str;
    int adv = 0;
    while (la != a.end() && lb != b.end()) {
        int tmp = (*la - '0') + (*lb - '0') + adv;
        str += (tmp % 10 + '0');
        adv = tmp / 10;
        la++;
        lb++;
    }
    while (la != a.end()) {
        int tmp = (*la - '0')  + adv;
        str += (tmp % 10 + '0');
        adv = tmp / 10;
        la++;
    }
    while (lb != b.end()) {
        int tmp =  (*lb - '0') + adv;
        str += (tmp % 10 + '0');
        adv = tmp / 10;
        lb++;
    }
    return str;//倒序
}
void constr(string a, string b) {
    a += "00";
    reverse(b.begin(), b.end());//小在前
    int ar[3] = { 8, 2, 6 };
    vector<string>str(3);

    for (int i = 0; i < 3; i++) {
        auto la = b.begin();
        int adv = 0;
        while (la != b.end()) {
            int tmp = ar[i] * (*la - '0') + adv;
            str[i] += (tmp % 10 + '0');
            adv = tmp / 10;
            la++;//小数在高位 倒序
        }
        if (adv != 0)
            str[i] += (adv + '0');
        //reverse(str[i].begin(), str[i].end());
        int j = i;
        while (j) {
            str[i].insert(str[i].begin(), '0');
            j--;
        }
    }
    string abs;
    abs = getsum(str[0], str[1]);
    abs = getsum(abs, str[2]);
    reverse(abs.begin(), abs.end());
    getcomp(a, abs);
}
int main() {
    string st1, st2;
    while (cin >> st1 >> st2 ) {      
        constr(st1, st2);
    }
    return 0;
}

唯一要注意的地方是 getcomp这个比较函数

从高位到低位 只要有a[i]>b[i ]就是No;只要有a[i]< b[i]就是Yes; 两判断条件缺一不可

发表于 2023-08-08 09:38:38 回复(0)
import java.math.BigDecimal;
import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //方法1:使用double来做 
        //while(in.hasNext()){
            double n = in.nextDouble();//double 范围 -1.7 E—308~1.7E-308 是1.7*10的308次方
            double r = in.nextDouble();
            if(n > (2*3.14*r)){
                System.out.println("No");
            }else{
                System.out.println("Yes");
            }
        }//
        //方法2:BigDecimal来做,BigDecimal可以用来算大数(任意精度的十进制数(浮点数))
        while(in.hasNext()){
            String n =in.next();
            String r =in.next();
            BigDecimal a =new BigDecimal(n);//a是身长
            BigDecimal b = new BigDecimal(r);//b是半径
            BigDecimal c = new BigDecimal(6.28);//3.14*2
            b=b.multiply(c);//周长:b*c
            if(a.compareTo(b)>0){//若a>b 返回1
                System.out.println("No");
            }else{
                System.out.println("Yes");
            }
        }
    }
}

发表于 2023-04-19 16:39:29 回复(0)
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            BigInteger[] integers = new BigInteger[2];
            integers[0] = scanner.nextBigInteger();//喵的身长
            integers[1] = scanner.nextBigInteger();//碗的半径
            if ((integers[1].doubleValue() * 2 * 3.14) < integers[0].doubleValue()){
                System.out.println("No");
            }else {
                System.out.println("Yes");
            }
        }
    }
}

发表于 2023-03-30 20:05:03 回复(0)
#include <iostream>
using namespace std;

int main()
{
    double n,r;
    while(cin >> n >> r)
    {
        if(2*3.14*r > n){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }
    return 0;
}

import java.util.*;
import java.io.*;
import java.math.BigDecimal;

public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            BigDecimal n = scan.nextBigDecimal();
            BigDecimal r = scan.nextBigDecimal();
            BigDecimal len = new BigDecimal("6.28").multiply(r);
            System.out.println(n.compareTo(len) == 1?"No":"Yes");
        }
    }
}

发表于 2023-02-09 16:53:34 回复(0)
身长 2^128 的猫,麻烦给俺也整一个
编辑于 2022-10-18 15:14:30 回复(0)
import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
                while (in.hasNext()) {
                    double n = in.nextDouble();
                    double r = in.nextDouble();
                if(n >= 2 * 3.14 * r){
                    System.out.println("No");
                }else{
                System.out.println("Yes");
            }
        }
    }
}

发表于 2022-05-04 19:51:54 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            Double cat = scanner.nextDouble();
            Double perimeter = scanner.nextDouble();
            if(cat < 2 * 3.14 * perimeter){
                System.out.println("Yes");
            }
            else {
                System.out.println("No");
            }
        }
    }
}

发表于 2022-03-21 21:37:55 回复(0)
import java.util.*;
import java.math.*;
public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            String str1 = scanner.next();
            String str2 = scanner.next();
            BigDecimal bigInteger1 = new BigDecimal(str1);
            BigDecimal bigInteger2 = new BigDecimal(str2);
            BigDecimal bigInteger3 = new BigDecimal("3.14");
            bigInteger2 = bigInteger2.multiply(bigInteger3.multiply(new BigDecimal("2")));
            int num = bigInteger1.compareTo(bigInteger2);
            if(num == -1){
                System.out.println("Yes");
            }else if(num == 0){
                System.out.println("Yes");
            }else if(num == 1){
                System.out.println("No");
            }
        }
    }
}

发表于 2021-05-19 19:57:52 回复(0)
#include <iostream>
//#include <math.h>
using namespace std;
int main()
{
    float n,r;
    while(cin>>n>>r)
    {
        n<=(2*3.14*r) ? cout<<"Yes"<<endl : cout<<"No"<<endl;
    }
    return 0;
}

发表于 2021-03-22 10:41:18 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main() {
    double c, r;
    while (cin >> c >> r) {
        6.28 * r >= c ? cout << "Yes\n" : cout << "No\n";
    }
    return 0;
}

发表于 2018-02-20 21:51:10 回复(0)