2018年江西理工大學C語言程式設計競賽(初級組)一
A: 逆序對
時間限制: 1 s 記憶體限制: 128 MB題目描述
1.整數序列中兩個相鄰的數,如果後面的數小於前面的數,則稱這兩個數值構成了一個逆序對。例如,整數序列10,4,16,8,21,18,9中包含了4個逆序對。從鍵盤上輸入n個由空格分隔的整數,程式設計輸出其中包含的逆序對的數量。
輸入
第一行輸入一個數字
第二行輸入n個由空格分隔的整數
輸出
輸出一個數字,表示逆序對的答案
樣例輸入
7
10 4 16 8 21 18 9
樣例輸出
4
果然就是比較相鄰麼
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices; public class Program { public static void Main() { int cunt = 0; int n = ri(); List<int> a = new List<string>(Console.ReadLine().Split()).ConvertAll(i => int.Parse(i)); for (int i = 1; i < n; i++) { if (a[i] < a[i - 1]) { cunt++; } } Console.WriteLine(cunt); Console.ReadKey(); } public static int ri() { return int.Parse(Console.ReadLine()); } static int[] rla(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e =>int.Parse(e)); } }
B: sky的圈圈
時間限制: 1 s 記憶體限制: 128 MB題目描述
最近小S不高興了,所以她就想畫圈圈,有大有小的。而現在她想讓你也畫圈圈了^_^。
大小為3的圈圈是,大小為4的圈圈是,大小為5的圈圈是,依次類推。
輸入
輸入一個數字n (3≤n≤100)
輸出
輸出你畫的圈圈。
樣例輸入
3
樣例輸出
*#*
#*#
*#*
畫圖畫圖。。
using System;
using System.IO;
using System.Linq;
public class Program
{
public static void Main()
{
var n = ri(); char[,] Map = new char[200,200]; Map[0, 0] = '*'; Map[0, n - 1] = '*'; Map[n - 1, 0] = '*'; Map[n - 1, n - 1] = '*'; for (int i = 1; i < n-1; i++) { Map[0, i] = '#'; Map[n - 1, i] = '#'; } for (int i = 1; i < n - 1; i++) { Map[i, 0] = '#'; Map[i, n-1] = '#'; } for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - 1; j++) { Map[i, j] = '*'; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(Map[i,j]); } Console.WriteLine(); } Console.ReadKey(); } public static int ri() { return int.Parse(Console.ReadLine()); } static int[] rla(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e =>int.Parse(e)); } }
C: 找零錢
時間限制: 1 s 記憶體限制: 128 MB題目描述
小明現在有x元,現在想買一件y (y≤x)元的物品,商店裡有五種貨幣,100元、20元、10元、5元、1元無限張,服務員會以最小的數量找零錢。問小明用x元買了一件y元的物品後找了多少張零錢。
輸入
輸入x和y兩個整數(1≤y≤x≤10000)
輸出
輸出找零錢的最小的數量
樣例輸入
101 66
樣例輸出
3
提示
用101元買了一件66元的物品,需要找35元,一張20元、一張10元和一張5元。
貪心,取模取模取模
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices; public class Program { public static void Main() { int cunt; int result = 0; List<int> a = new List<string>(Console.ReadLine().Split()).ConvertAll(i => int.Parse(i)); cunt = a[0] - a[1]; result += cunt / 100; cunt %= 100; result += cunt / 20; cunt %= 20; result += cunt / 10; cunt %= 10; result += cunt/5; cunt %= 5; result += cunt / 1; Console.WriteLine(result); Console.ReadKey(); } public static int ri() { return int.Parse(Console.ReadLine()); } static int[] rla(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e =>int.Parse(e)); } }
D: 程式語言
時間限制: 1 s 記憶體限制: 128 MB題目描述
現在的程式設計越來越多了,比如C、C++、Java、Python、C#、PHP等等。現在給定n種程式語言,每種語言還會給一個[1,n]之間的隨機值,保證不重複。現在讓你按隨機值從小到大排序,然後輸出對應的語言。
輸入
第一行輸入一個整數n (1≤n≤20)
接下來nn行,每行有一個字串和一個隨機值,字串表示一種語言,長度不超過20.隨機值範圍為[1,n]
輸出
輸出nn行,按隨機值從小到大輸出對應的語言
樣例輸入
4
Java 3
C 1
Python 4
C++ 2
樣例輸出
C
C++
Java
Python
排序排序排序排序
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
public class Program { public static void Main() { var n = ri(); ArrayList al = new ArrayList(); for (int i = 0; i < n; i++) { Language La = new Language(); var x = Console.ReadLine().Split(); La.xx = x[0]; La.yy = int.Parse(x[1]); al.Add(La); } Languagex languagex = new Languagex(); al.Sort(languagex); Language Laa = new Language(); for (int i = 0; i < n; i++) { Laa = (Language) al[i]; Console.WriteLine(Laa.xx); } Console.ReadKey(); } public class Language { public string xx; public int yy; } public class Languagex:IComparer { public int Compare(object x, object y) { return ((Language) x).yy.CompareTo(((Language) y).yy); } } public static int ri() { return int.Parse(Console.ReadLine()); } static int[] rla(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e =>int.Parse(e)); } }
E: 四邊形面積
時間限制: 1 s 記憶體限制: 128 MB題目描述
有一個四邊形,現在需要求它的面積
輸入
輸入四行,每行兩個數整數x, y (1≤x,y≤1000),四個點是按逆時針輸入的。
輸出
輸出四邊形的面積,保留3位小數點,
樣例輸入
0 0
10 0
1 1
0 11
樣例輸出
10.500
提示
C語言中保留3位小數用%.3lf 用法:printf("%.3lf",result)
四邊形分凸凹四邊形。
提示都告訴你了
#include<bits/stdc++.h>
using namespace std;
struct Point
{
float x, y;
};
float LinearIntegration(const Point &p1, const Point &p2)
{
return 0.5 * (p2.x - p1.x) * (p2.y + p1.y);
}
float ComputePolygonArea(const Point points[], int length) { if (points == NULL || length <= 0) return 0.0; float area = 0.0; for (int i = 0; i < length - 1; ++ i) { area += LinearIntegration(points[i], points[i + 1]); } area += LinearIntegration(points[length - 1], points[0]); return area >= 0.0 ? area : -area; } int main() { int n; Point a[4]; for(int i=0; i<4;i++) cin>>a[i].x>>a[i].y; float ans = ComputePolygonArea(a,4); printf("%.3f\n",ans); return 0; }
F: 進位制轉換
時間限制: 1 s 記憶體限制: 128 MB題目描述
給定一個區間[l, r],從l至r之間的所有數依次轉換成16進位制然後連在一起,接著再轉換成10進位制,最後再對15取模。
輸入
輸入兩個是l, r (1≤l≤r≤106)
輸出
輸出對15取模的結果。
樣例輸入
10 14
樣例輸出
0
提示
樣例說明:
10、11、12、13、14的16進位制分別是a、b、c、d、e。依次連在一起是abcde,轉換成10進位制是703710,對15取模為0。
一般的轉化。(a*16^n+b*16^(n-1)..)%15,展開,a%16*16^n%15,16%15就可以不考慮了,於是變成a%15+b%15..。於是簡化成a+b+c+d+e。注意取模
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit; using System.Runtime.InteropServices; public class Program { public static void Main() { var str = ""; List<int> a = new List<string>(Console.ReadLine().Split()).ConvertAll(i => int.Parse(i)); var sum = 0; for (int i = a[0]; i <= a[1]; i++) { var pa = Convert.ToString(i, 16); //var pa = Convert.ToInt32(x, 16).ToString(); var len = pa.Length; for (int j = 0; j < len; j++) { if (pa[j] == 'a') { sum += (10); sum %= 15; } else if (pa[j] == 'b') { sum += (11); sum %= 15; } else if (pa[j] == 'c') { sum += (12); sum %= 15; } else if (pa[j] == 'd') { sum += (13); sum %= 15; } else if (pa[j] == 'e') { sum += (14); sum %= 15; } else if (pa[j] == 'f') { sum += (15); sum %= 15; } else { sum += int.Parse(pa[j].ToString()); sum %= 15; } } } Console.WriteLine(sum%15); Console.ReadKey(); } public static int ri() { return int.Parse(Console.ReadLine()); } static int[] rla(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e =>int.Parse(e)); } }