首页 > 试题广场 >

连线游戏

[编程题]连线游戏
  • 热度指数:2681 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
某一天,Alice 比较无聊,于是她为自己发明了一个游戏玩。首先她在纸上画了一个圆,然 后从这个圆的圆弧上均匀地取出 n 个点,这 n 个点将圆 n 等分。接下来,Alice 每次从这 n 个点中选取两个点,在这两个点之间画一条线段,但是要求这条线段不能与已有的线段相交 (允许在端点处相交)。为了能打发更多的时间,Alice 希望能画尽量多的线段,请你告诉她 最多她能画出几条线段?

数据范围:

输入描述:

第一行包含一个整数𝑛,表示从圆弧上取出的点数。



输出描述:
输出对应的答案。
示例1

输入

2

输出

1
示例2

输入

4

输出

5
找规律得到通项公式:2*n-3
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        System.out.println(2*n - 3);
    }
}


发表于 2021-02-03 12:34:45 回复(0)

观察规律就可以,直接等差数列:

import java.util.Scanner;

public class Main {

    /**
     * 思路:
     * 1.通过写几个测试用例可以发现这就是一个等差数列
     * 2.a1=1,d=2
     * 3.所以公式就是:a1+(n-1)*d
     * 4.但是这里的n是从2开始的,所以需要多-1
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int a1 = 1;
        int result = a1 + (n - 2) * 2;
        System.out.println(result);
    }
}



发表于 2020-08-01 11:33:10 回复(0)

看到一个输入,一个输出的题目要联想到打表法。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(2 * (n - 1) - 1);
    }
}
发表于 2019-06-16 15:17:38 回复(2)
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n; cin >> n; int ans = 0;
    ans = (n == 2 ? 1 : n);
    if (n > 3) {
        ans += n -3;
    }
    cout << ans << endl;
    return 0;
}
发表于 2019-04-23 13:48:53 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author wylu
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println(2 * Integer.parseInt(br.readLine()) - 3);
    }
}

发表于 2019-03-13 15:19:39 回复(0)
#include <bits/stdc++.h>

using namespace std;

int main()
{     long n;     while(cin>>n){         cout<<n+n-3<<endl;     }     return 0;
}

发表于 2019-01-29 02:35:51 回复(0)
print(3+(int(input())-3)*2)

发表于 2019-04-25 10:25:57 回复(0)
package main

import (
    "fmt"
)

func main() {
    var n,ans int
    fmt.Scan(&n)
    if n==2{
        ans=1
    }else if n==3{
        ans=3
    }else{
        ans=n+(n-3)
    }
    fmt.Print(ans)
}

发表于 2023-06-04 14:27:22 回复(0)
#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    cout<<2*n-3;  
}

发表于 2022-04-16 00:11:24 回复(0)
while (line = readline()) {
        print(2*line-3)  
}
//我还以为是return呢
发表于 2021-10-16 11:39:44 回复(0)
n = int(input())

print(2*(n-1)-1)
发表于 2021-09-20 17:50:08 回复(0)
#include <stdio.h>
#include <stdlib.h>
int func(int x)
{
    if(x<4)
        return 0;
    else if(x==4)
        return 1;
    else
        return x/2+func(x/2+x%2);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n<2)
            printf("0\n");
        else if(n==2)
            printf("1\n");
        else
        {
            printf("%d\n",n+func(n));
        }
    }
    return 0;
}

发表于 2021-08-20 21:57:30 回复(0)
import java.util.*;
public class Main{
    public static void main(String[]args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int res=helper(n);
        System.out.println(res);
    }
    
    public static int helper(int n){
        return 2*n-3;
    }
}

发表于 2021-03-11 08:24:22 回复(0)
#include<iostream>
int main()
{
    long long n;
    while(std::cin>>n)
    {
        long long a=2*n-3;
        std::cout<<a<<std::endl;
    }
    return 0;
}
发表于 2020-08-29 15:51:16 回复(0)
我对这些笔试题,感到有点无语。。😓
发表于 2020-08-27 11:08:01 回复(0)
n = int(input())

print(2*n - 3)

发表于 2020-06-27 10:54:00 回复(1)
var n=parseInt(readline())
// 规律 1 3 5 7
console.log(2*(n-1)-1)

发表于 2020-04-19 23:54:46 回复(0)
#include <stdio.h>
int main()
{
	int n;
	scanf("%d",&n);
	printf("%d\n",1+((n-2)<<1));
	return 0;
}

发表于 2019-08-12 11:51:49 回复(0)
while True:
    try:
        n=int(input().strip())
        result=2*n-3
        print(result)
    except:
        break
发表于 2019-07-23 21:18:34 回复(0)
import sys 

def get_result(number):
    if numbers == 2:
        return 1
    else:
        return number+(number-3)

for line in sys.stdin:
    a = line.split()
    numbers = int(a[0])
    print(get_result(numbers))

拿到题后发现这应该是一道找数学规律的题目,遇到这种题目往往我会根据题目要求多写几组答案出来例如本题就是从2开始一直到n结束,那我会写出2,3,4,5,6各对应的答案为1,3,5,7,9,而且通常这类题目的首位元素或末位元素需要单独处理因此考虑到这里就不难发现规律为 res = number+(number-3)
发表于 2019-06-25 04:00:22 回复(0)

问题信息

难度:
27条回答 3645浏览

热门推荐

通过挑战的用户

查看代码