代码卡纸商店信用

时间:2016-01-28 09:15:05

标签: c#

问题 您在当地商店收到信用卡C,并想购买两件商品。您首先浏览商店并创建所有可用商品的清单L.从此列表中,您可以购买两个项目,这些项目可以累计信用的全部价值。您提供的解决方案将包含两个整数,表示列表中项目的位置(首先是较小的数字)。

输入 第一行输入给出了案例数,N.N测试案例如下。对于每个测试用例,将有: •一行包含值C,即您在商店的信用额度。 •一行包含值I,即商店中的商品数量。 •一行包含空格分隔的I整数列表。每个整数P表示商店中商品的价格。 •每个测试用例都只有一个解决方案。

输出 对于每个测试用例,输出一行包含" Case #x:"其次是价格加起来商店信用的两个商品的指数。应首先输出较低的索引。 范围 5≤C≤1000 1≤P≤1000 小数据集 N = 10 3≤I≤100 大数据集 N = 50 3≤I≤2000

示例数据 输入

3 100 3 5 75 25 200 7 150 24 79 50 88 345 3 8 8 2 1 9 4 4 56 90 3

输出 案例#1:2 3 案例#2:1 4 案例#3:4 5

1 个答案:

答案 0 :(得分:-1)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ReadCreaditCard
{
    class Program
    {
        static void Main(string[] args)
        {
            //ask for input file path.
            Console.WriteLine("Please Enter the input file path:");
            //@"C:\Users\NIRBHAY\Document\Store Credit-small.txt";
            string path = Console.ReadLine();

            //validate the file path.
            while (!File.Exists(path))
            {
                Console.WriteLine("Please Enter a valid file path:");
                path = Console.ReadLine();
            }

            //open the file to read the data.
            using (StreamReader tr = new StreamReader(path))
            {
                //read the no of cases.
                string noOfCases = tr.ReadLine();
                int caseCounter = 0;

                //validate if first line of input file is valid numeric value.
                while (!int.TryParse(noOfCases, out caseCounter))
                {
                    Console.WriteLine("First line of input file is not a numeric value.");
                    Console.WriteLine("Please enter a numeric value:");
                    noOfCases = Console.ReadLine();
                }

                int caseNo = 1;
                //read all the lines of data and show it in reverse order.
                while (caseCounter-- > 0)
                {
                    #region Read credit value
                    string creditStr = tr.ReadLine();
                    if (creditStr == null)
                    {
                        Console.WriteLine("Invalid input file.");
                        break;
                    }
                    int creditValue = 0;
                    if (!int.TryParse(creditStr, out creditValue))
                    {
                        Console.WriteLine("Invalid input file.");
                        break;
                    }
                    #endregion

                    #region Read items count
                    string itemsCountStr = tr.ReadLine();
                    if (itemsCountStr == null)
                    {
                        Console.WriteLine("Invalid input file.");
                        break;
                    }

                    int itemsCount = 0;
                    if (!int.TryParse(itemsCountStr, out itemsCount))
                    {
                        Console.WriteLine("Invalid input file.");
                        break;
                    }
                    #endregion

                    string itemsPriceStr = tr.ReadLine();
                    if (itemsPriceStr == null)
                    {
                        Console.WriteLine("Invalid input file.");
                        break;
                    }
                    string[] arrayOfCosts = itemsPriceStr.Split(' ');
                    PrintItems(caseNo, creditValue, itemsCount, arrayOfCosts);
                    caseNo++;
                }
            }
            Console.WriteLine("\n \n Press any key to exit :)");
            Console.ReadKey();
        }

        /// <summary>
        /// Prints the indexes of selected items.
        /// </summary>
        /// <param name="caseNo"></param>
        /// <param name="creditValue"></param>
        /// <param name="itemsCount"></param>
        /// <param name="arrayOfCosts"></param>
        private static void PrintItems(int caseNo, int creditValue, int itemsCount, string[] arrayOfCosts)
        {
            for (int i = 0; i < itemsCount; i++)
            {
                int firstItemCost = int.Parse(arrayOfCosts[i]);
                for (int j = i + 1; j < itemsCount; j++)
                {
                    int secondItemCost = int.Parse(arrayOfCosts[j]);
                    if (firstItemCost + secondItemCost == creditValue)
                    {
                        Console.WriteLine("Case#{0}: {1}, {2}", caseNo, i, j);
                        return;
                    }
                }
            }
        }
    }
}