首页 > 试题广场 >

Number Steps

[编程题]Number Steps
  • 热度指数:5139 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
Starting from point (0,0) on a plane, we have written all non-negative integers 0,1,2, ... as shown in the figure. For example, 1, 2, and 3 has been written at points (1,1), (2,0), and (3, 1) respectively and this pattern has continued.
You are to write a program that reads the coordinates of a point (x, y), and writes the number (if any) that has been written at that point. (x, y) coordinates in the input are in the range 0...5000.

输入描述:
each line, there is x, and y representing the coordinates (x, y) of a point.


输出描述:
For each point in the input, write the number written at that point or write No Number if there is none.
示例1

输入

3
4 2
6 6
3 4

输出

6
12
No Number
规律其实特别简单
#include <iostream>

using namespace std;

int main() {
  int a, b;
  while(cin >> a >> b) {
    if(a != b && b+2 != a) {
        cout << "No Number" << endl;
        continue;
    }
    if(a%2 == 0) {
      cout << a+b << endl;
    }
    else {
      cout <<a+b-1 << endl;
    }
  }

  return 0;
}

发表于 2019-01-09 17:16:18 回复(1)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        int N,x,y;
        Scanner sc=new Scanner(System.in);
        N=sc.nextInt();
        for (int i=1;i<=N;i++){
             x=sc.nextInt();
             y=sc.nextInt();
             if ((x-y==2)||(x-y==0)){
                     if (x%2==0){
                         System.out.println(x+y);
                     }
                     else {
                         System.out.println(x+y-1);
                     }
             }
             else {
                  System.out.println("No Number");
             }
         }             
    }
}

发表于 2017-05-22 02:21:11 回复(1)
//主要在于找规律
//1.x=y或者x-2=y的时候有数
//2.x为偶数时目标值为x+y,x为奇数的时候目标值为x+y-1
#include<stdio.h>
int main()
{
    int x,y,s,n;
    scanf("%d%d",&x,&y);
    if(y!=x&&y!=x-2)
        printf("No Number\n");
    else
    {
        if(x%2==0)//x是偶数
            printf("%d\n",x+y);
        else
            printf("%d\n",x+y-1);
    }
}

发表于 2020-04-06 20:49:17 回复(1)
找规律(很简单的规律)
1.如果x+y是奇数或者(x-y!=0&&x-y!=2)输出no number。
2.如果不满足上面条件则,y是奇数输出x+y-1,否则输出x+y。
#include<iostream>
#include<vector>
using namespace std;
int main(){
	int x,y;
	while(cin>>x>>y){
		if((x+y)%2==0){
			if(y%2!=0){
				cout<<x+y-1<<endl;
			}else{
				cout<<x+y<<endl;
			}
		}else{
			cout<<"No Number"<<endl;
		}
	}
	return 0;
}


编辑于 2020-03-05 13:06:57 回复(0)

哪位仁兄能看懂我这清奇的思路哈哈哈!
#include <bits/stdc++.h>
using namespace std;
bool isXY(int x,int y){
	if(y==x||y==x-2)
	return true;
	else{
		cout<<"No Number";
		return false;
	}
	
}
int main(){
	int n,a[10000],b[10000],a0,b0,j=0,k=0,l=0,count,x,y;
	
	for(int i=0;i<=11000;i++){
		if(j==2){
			b[k++]=i;
			count++;
			if(count!=2)
			continue;
			else{
				j=0;
				continue;
			}
			
		}
		a[l++]=i;
		j++;
		count=0;
		
	}


		cin>>x>>y;
		if(isXY(x,y)){
			if(y==x){
				cout<<a[x];
			}
			else {
				cout<<b[x-2];
				
			}
		}

	return 0;
}


发表于 2020-02-26 16:48:08 回复(0)
#include<iostream>
#include<cstring>
using namespace std;
int main(){
    int x,y;
    while(cin>>x>>y){
        if(x==y){
            if(x%2==0)cout<<2*x<<endl;
            else cout<<2*x-1<<endl;
        }else if(y==x-2){
            if((x-1)%2==0)cout<<2*(x-1)-1<<endl;
            else cout<<2*(x-1)<<endl;
        }else cout<<"No Number"<<endl;
    }
    return 0;
}

发表于 2018-05-05 21:57:28 回复(0)
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct temp{
    int x;
    int y;
    int z;
};
int main()
{
    int b,c;
    struct temp a[10000];
    cin>>b>>c;
    a[0].x=0,a[0].y=0,a[0].z=0;
    a[1].x=1,a[1].y=1,a[1].z=1;
    for(int i=2;i<10000;i++)
    {
        if(i%2!=0)
        {
             a[i].x=a[i-1].x+1;
             a[i].y=a[i-1].y+1;
             a[i].z=i;
        }
        else if(i%2==0&&(a[i-1].x==a[i-1].y))
        {
            a[i].x=a[i-2].x+2;
            a[i].y=a[i-2].y;
            a[i].z=i;
        }
        else if(i%4==0&&(a[i-1].x!=a[i-1].y))
        {
            a[i].x=a[i-2].x;
            a[i].y=a[i-2].y+2;
            a[i].z=i;
        }
        if(b==a[i].x&&c==a[i].y)
        {
            cout<<a[i].z<<endl;
            break;
        }
        else if(abs(b-c)>2)
        {
            cout<<"No Numeber"<<endl;
            break;
        }
    }
    return 0;
}
 
发表于 2018-04-19 19:14:18 回复(0)
题给的测试用例有误,第一个数字3没有用,提交时不会给有多少行的。
#include<iostream>

using namespace std;

int main(){
        int x,y;
        while(cin>>x>>y){
            int answer=0;
            if(x==y){
                if(x%2==0)
                    answer=x*2;
                else
                    answer=x*2-1;
                cout<<answer<<"\n";
            }
            else if(y==x-2){
                if(x%2==0)
                    answer=x*2-2;
                else
                    answer=x*2-3;
                cout<<answer<<"\n";
            }
            else
                cout<<"No Number"<<"\n";
        }     
   return 0;
}

发表于 2020-03-14 18:11:12 回复(0)
我要吐槽……要求不存在的时候“write No Number”,那就不能给“No Number”加个引号么……到底是不输出还是输出“No Number”不就有歧义了么……
#include <stdio.h>
#define N 12000

int main()
{
    int i, x, y, findit;
    int a[N][2];//a[i][0]代表x,a[i][1]代表y
    a[0][0]=0;
    a[0][1]=0;
    for(i=1; i<N; i++)
    {
        switch(i%4){
            case 1:
                a[i][0]=a[i-1][0]+1;
                a[i][1]=a[i-1][1]+1;
                break;
            case 2:
                a[i][0]=a[i-1][0]+1;
                a[i][1]=a[i-1][1]-1;
                break;
            case 3:
                a[i][0]=a[i-1][0]+1;
                a[i][1]=a[i-1][1]+1;
                break;
            case 0:
                a[i][0]=a[i-1][0]-1;
                a[i][1]=a[i-1][1]+1;
                break;
            default: break;
        }
    }
    while(scanf("%d %d", &x, &y)!=EOF)
    {
        findit=0;//表示没找到
        for(i=0; i<N; i++)
        {
            if(x==a[i][0]&&y==a[i][1])
            {
                printf("%d\n", i);
                findit=1;
                break;
            }
        }
        if(findit==0) printf("No Number\n");
    }
    return 0;
}

发表于 2018-02-07 08:31:09 回复(0)
这个测试用例无语死了。。居然是假的
#include <iostream>
using namespace std;

int main() {
    int x,y;
    while(cin>>x>>y){
        if((x!=y&&x!=(y+2))||x<0||y<0){
            cout<<"No Number"<<endl;
            continue;
        }
        cout<<2*x-(x%2)-(x-y)<<endl;     
    }
}


发表于 2025-03-08 11:13:12 回复(0)
#n = int(input())
自测数据和提交数据不一样
a = 2
b = 0
c1 = 0
c = 0
#for  i in range(n):
x, y = list(map(int, input().split()))
if x == 0 and y == 0:
    print(0)
elif x == 2 and y == 0:
    print(2)
    
elif x-2 == y:
    while y:
        y -= 1
        c += 1
        if c % 2 != 0:
            a += 1
        else:
            a += 3
    print(a)
elif x == y:
        # y -= 1
    while y:
        y -= 1
        c1 += 1
        if c1 % 2 != 0:
            b += 1
        else:
            b += 3
    print(b)
else:
    print("No Number")
    

发表于 2025-02-17 00:16:34 回复(0)
b题
发表于 2025-01-18 11:59:08 回复(0)
// 示例有坑!! 示例那个3没用 直接输入坐标(x, y )  !!
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int main() {
    
        int x, y,count=0,x1,y1;
        scanf("%d %d", &x, &y);
         if (2>=(x-y)&&(x-y)>=0)
        {
            x1 = x % 2;
            y1 = y % 2;
            if (x1 + y1 == 0)
            {
                printf("%d\n", x + y);
            }
            else if (x1 + y1 == 1)
            {
                printf("No Number\n");
            }
            else
            {
                printf("%d\n", x + y - 1);
            }
        }     
        else
        {
            printf("No Number\n");
        }
    
    return 0;
}

编辑于 2024-03-04 11:56:09 回复(0)
题给的测试用例有误,第一个数字3没有用,提交时不会给有多少行的。
#include <iostream>
using namespace std;

int main() {
    int x, y;
    while (cin >> x >> y) {
        if (x == y || x - y == 2) {
            cout << x + y / 2 * 2 << endl;
        } else {
            cout << "No Number" << endl;
        }
    }
    return 0;
}


编辑于 2024-03-01 12:40:54 回复(0)
测试用例不讲武德
发表于 2023-05-04 22:41:04 回复(0)
//看了三遍才看明白,其实就是找规律。给你一个点的坐标(x,y),输出此坐标所表示的值。
//eg:(2,0)为2而(3,0)不存在所以为No Number。所以就是找规律,规律也很好找。
//若x为偶数,则为x+y(注意判断不存在数的点)。x若为奇数,则为x+y-1(注意判断不存在数的点).
#include "stdio.h"

void oddNum(int x,int y){//偶数的规律
    if(y > x || y < x-2 || y == x-1)
        printf("No Number\n");
    else
        printf("%d\n",x+y);
}

void evenNum(int x,int y){//奇数的规律
    if(y > x || y < x-2 || y == x-1)
        printf("No Number\n");
    else
        printf("%d\n",x+y-1);
}

int main(){
    int x,y;
    while (scanf("%d%d",&x,&y)!=EOF){
        if(x%2 == 0)
            oddNum(x,y);
        else
            evenNum(x,y);
    }
}

发表于 2023-03-11 16:43:21 回复(0)
有点像周期函数,挺简单的.
发表于 2023-02-28 13:50:10 回复(0)
答案有误,第一个测试案例没使用
发表于 2022-03-03 22:04:51 回复(0)
脑_残题目,示例和输入描述根本不一样,搞人心态来的
发表于 2022-02-01 20:43:02 回复(0)
这格式咋回事,示例一的第一行不是行数吗,这格式根本没法过,换成while的输入就能过了?这不是坑爹吗
发表于 2021-04-30 11:07:43 回复(0)