根据给定条件从无限类型的无限项目中找到第 N 个项目
给定一个正整数N ,任务是找到当有无限种类型的无限项时给出的第N项,使得这些项以下列方式给出:
- 第 1 天:送出 1 件 Type-I。
- 第 2 天:发放 2 件 II 类物品和 1 件 I 类物品。
- 第 3 天:发放 3 件 III 类物品、2 件 II 类物品和 1 件 I 类物品。
- 第 4 天:发放 IV 类 4 项、III 类 3 项、II 类 2 项和 I 类 1 项。
- 等等…
例子:
Input: N = 10
Output: 1
Explanation:
Following are the orders of the items given out:
- Day 1: 1 item of Type-I are given out. The sequence is {1}.
- Day 2: 2 items of the Type-II and 1 item of Type-I are given out. The sequence is {1, 2, 2, 1}.
- Day 3: 3 items of the Type-III, 2 items of the Type-II, and 1 item of Type-I are given out. The sequence is {1, 2, 2, 1, 3, 3, 3, 2, 2, 1}.
From the above order of items removed, the Nth(= 10th) item given out is 1. Therefore, print 1.
Input: N = 399
Output: 11
方法:解决给定问题的最简单方法是按照给定的顺序跟踪每天给出的天数和物品数量,并打印在第 N 轮分发的物品.
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the type of the
// item given out according to the
// given rules
int itemType(int n)
{
// Stores the count of item given
// out at each step
int count = 0;
// Iterate over the days from 1
for (int day = 1;; day++) {
// Iterate over type of item
// on that day
for (int type = day; type > 0; type--) {
count += type;
// Count of items given out
// should exceed n
if (count >= n)
return type;
}
}
}
// Driver Code
int main()
{
int N = 10;
cout << itemType(N);
return 0;
}
Java
// Java Program for the above approach
import java.io.*;
class GFG
{
// Function to find the type of the
// item given out according to the
// given rules
static int itemType(int n)
{
// Stores the count of item given
// out at each step
int count = 0;
// Iterate over the days from 1
for (int day = 1;; day++) {
// Iterate over type of item
// on that day
for (int type = day; type > 0; type--) {
count += type;
// Count of items given out
// should exceed n
if (count >= n)
return type;
}
}
}
// Driver Code
public static void main (String[] args) {
int N = 10;
System.out.println( itemType(N));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python3 program for the above approach
# Function to find the type of the
# item given out according to the
# given rules
def itemType(n):
# Stores the count of item given
# out at each step
count = 0
# Iterate over the days from 1
day = 1
while(True):
# Iterate over type of item
# on that day
for type in range(day, 0, -1):
count += type
# Count of items given out
# should exceed n
if (count >= n):
return type
# Driver Code
N = 10
print(itemType(N))
# This code is contributed by ShubhamSingh10
C#
//C# code for the above approach
using System;
public class GFG{
// Function to find the type of the
// item given out according to the
// given rules
static int itemType(int n)
{
// Stores the count of item given
// out at each step
int count = 0;
// Iterate over the days from 1
for (int day = 1;; day++) {
// Iterate over type of item
// on that day
for (int type = day; type > 0; type--) {
count += type;
// Count of items given out
// should exceed n
if (count >= n)
return type;
}
}
}
// Driver Code
static public void Main ()
{
int N = 10;
Console.WriteLine( itemType(N));
}
}
// This code is contributed by shubhamsingh10.
Javascript
// Javascript program for the above approach
// Function to find the type of the
// item given out according to the
// given rules
function itemType(n)
{
// Stores the count of item given
// out at each step
let count = 0;
// Iterate over the days from 1
for (let day = 1; ; day++)
{
// Iterate over type of item
// on that day
for (let type = day; type > 0; type--)
{
count += type;
// Count of items given out
// should exceed n
if (count >= n) return type;
}
}
}
// Driver Code
let N = 10;
document.write(itemType(N));
// This code is contributed by _saurabh_jaiswal.
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the type of the
// item given out according to the
// given rules
int itemType(int n)
{
// Stores the count of item given
// out at each step
int count = 0;
int day = 1;
// Iterate to find the Nth day
// present is given out
while (count + day * (day + 1) / 2
< n) {
// Find the number of presents
// given on day is day*(day+1)/2
count += day * (day + 1) / 2;
day++;
}
for (int type = day; type > 0; type--) {
// Iterate over the type
count += type;
// Return the resultant type
if (count >= n) {
return type;
}
}
}
// Driver Code
int main()
{
int N = 10;
cout << itemType(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the type of the
// item given out according to the
// given rules
static int itemType(int n)
{
// Stores the count of item given
// out at each step
int count = 0;
int day = 1;
// Iterate to find the Nth day
// present is given out
while (count + day * (day + 1) / 2
< n) {
// Find the number of presents
// given on day is day*(day+1)/2
count += day * (day + 1) / 2;
day++;
}
for (int type = day; type > 0; type--)
{
// Iterate over the type
count += type;
// Return the resultant type
if (count >= n) {
return type;
}
}
return 0;
}
// Driver Code
public static void main(String[] args)
{
int N = 10;
System.out.println( itemType(N));
}
}
// This code is contribured by shivanisinghss2110
Python3
# Python 3 program for the above approach
# Function to find the type of the
# item given out according to the
# given rules
def itemType(n):
# Stores the count of item given
# out at each step
count = 0
day = 1
# Iterate to find the Nth day
# present is given out
while (count + day * (day + 1) // 2 < n):
# Find the number of presents
# given on day is day*(day+1)/2
count += day * (day + 1) // 2;
day += 1
type = day
while(type > 0):
# Iterate over the type
count += type
# Return the resultant type
if (count >= n):
return type
type -= 1
# Driver Code
if __name__ == '__main__':
N = 10
print(itemType(N))
# This code is contributed by bgaangwar59.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the type of the
// item given out according to the
// given rules
static int itemType(int n)
{
// Stores the count of item given
// out at each step
int count = 0;
int day = 1;
// Iterate to find the Nth day
// present is given out
while (count + day * (day + 1) / 2
< n) {
// Find the number of presents
// given on day is day*(day+1)/2
count += day * (day + 1) / 2;
day++;
}
for (int type = day; type > 0; type--)
{
// Iterate over the type
count += type;
// Return the resultant type
if (count >= n) {
return type;
}
}
return 0;
}
// Driver Code
public static void Main(String[] args)
{
int N = 10;
Console.Write ( itemType(N));
}
}
// This code is contribured by shivanisinghss2110
Javascript
输出:
1
时间复杂度: O(N)
辅助空间: O(1)
有效方法:上述方法也可以通过使用以下事实进行优化:在给定的特定日期D给出的项目数是从1到D的数字之和,并且从第 1天到那一天的数字之和应该更少大于等于N 。请按照以下步骤解决问题:
- 初始化变量,比如count为0来存储给定的项目数, day来存储当天的项目数。
- 迭代一个循环,直到(count + day*(day + 1))/2的值小于N并执行以下步骤:
- 将day*(day + 1)/2的值添加到变量count中。
- 将一天的值增加1 。
- 使用变量类型迭代范围[day, 0]并执行以下任务:
- 将type的值添加到变量count 。
- 如果count的值大于等于N ,则打印type的值作为结果答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the type of the
// item given out according to the
// given rules
int itemType(int n)
{
// Stores the count of item given
// out at each step
int count = 0;
int day = 1;
// Iterate to find the Nth day
// present is given out
while (count + day * (day + 1) / 2
< n) {
// Find the number of presents
// given on day is day*(day+1)/2
count += day * (day + 1) / 2;
day++;
}
for (int type = day; type > 0; type--) {
// Iterate over the type
count += type;
// Return the resultant type
if (count >= n) {
return type;
}
}
}
// Driver Code
int main()
{
int N = 10;
cout << itemType(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the type of the
// item given out according to the
// given rules
static int itemType(int n)
{
// Stores the count of item given
// out at each step
int count = 0;
int day = 1;
// Iterate to find the Nth day
// present is given out
while (count + day * (day + 1) / 2
< n) {
// Find the number of presents
// given on day is day*(day+1)/2
count += day * (day + 1) / 2;
day++;
}
for (int type = day; type > 0; type--)
{
// Iterate over the type
count += type;
// Return the resultant type
if (count >= n) {
return type;
}
}
return 0;
}
// Driver Code
public static void main(String[] args)
{
int N = 10;
System.out.println( itemType(N));
}
}
// This code is contribured by shivanisinghss2110
Python3
# Python 3 program for the above approach
# Function to find the type of the
# item given out according to the
# given rules
def itemType(n):
# Stores the count of item given
# out at each step
count = 0
day = 1
# Iterate to find the Nth day
# present is given out
while (count + day * (day + 1) // 2 < n):
# Find the number of presents
# given on day is day*(day+1)/2
count += day * (day + 1) // 2;
day += 1
type = day
while(type > 0):
# Iterate over the type
count += type
# Return the resultant type
if (count >= n):
return type
type -= 1
# Driver Code
if __name__ == '__main__':
N = 10
print(itemType(N))
# This code is contributed by bgaangwar59.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the type of the
// item given out according to the
// given rules
static int itemType(int n)
{
// Stores the count of item given
// out at each step
int count = 0;
int day = 1;
// Iterate to find the Nth day
// present is given out
while (count + day * (day + 1) / 2
< n) {
// Find the number of presents
// given on day is day*(day+1)/2
count += day * (day + 1) / 2;
day++;
}
for (int type = day; type > 0; type--)
{
// Iterate over the type
count += type;
// Return the resultant type
if (count >= n) {
return type;
}
}
return 0;
}
// Driver Code
public static void Main(String[] args)
{
int N = 10;
Console.Write ( itemType(N));
}
}
// This code is contribured by shivanisinghss2110
Javascript
输出:
1
时间复杂度: O(sqrt(N))
辅助空间: O(1)