題目連接:https://vjudge.net/contest/279567#overview 題目密碼:190118html
Calculate A + B.ios
Input contains multiple test cases。 Each test case contains two integers: A(1 ≤ A ≤ 100) and B(1 ≤ B ≤ 100).git
For each test case output the sum A + B.數組
1 2 3 4
3 7
簡單的EOF輸入,多行重複輸入後再進行求和。網絡
代碼以下:ide
#include <stdio.h> int main() { int a, b; while (scanf("%d %d", &a, &b) != EOF) { printf("%d", a + b); } return 0; }
給定兩個正整數,計算這兩個數的最小公倍數。函數
輸入包含多組測試數據,每組只有一行,包括兩個不大於1000的正整數.測試
對於每一個測試用例,給出這兩個數的最小公倍數,每一個實例輸出一行。spa
10 14
70
運用Wiki百科上已有關於最小公倍數和最大公約數的函數,進行編寫程序。(我的函數庫——最大公約數和最小公倍數).net
代碼以下:
#include <stdio.h> int GCD(int a, int b); int LCM(int a, int b); int main() { int a, b; while (scanf("%d %d", &a, &b) != EOF) { printf("%d\n", LCM(a, b)); } return 0; } int GCD(int a, int b) { if (b) while ((a %= b) && (b %= a)); return a + b; } int LCM(int a, int b) { return a * b / GCD(a, b); }
Given two dates, please calculate the number of February 29 between the two dates inclusively.
Only a leap year contains February 29. The year satisfying one of the following conditions is a leap year.
\1. The year number can be divided by 4 but cannot be divided by 100.
\2. The year number can be divided by 400.
The first line contains an integer T, indicating the number of test cases.
Each test case contains two lines which are both in the format "Month Day, Year", indicating the dates. Month is a string contained in {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"}. Day and Year are two integers.
We guarantee that all the dates are valid. The first date comes before the second date or the two dates are the same.
For each test case, output a line containing "Case #X: Y". X is the test case number starting from 1, and Y is the answer.
4 January 12, 2012 March 19, 2012 August 12, 2899 August 12, 2901 August 12, 2000 August 12, 2005 February 29, 2004 February 29, 2012
Case #1: 1 Case #2: 0 Case #3: 1 Case #4: 3
這裏的核心思想是:分別計算從公元1年到起始年和到結束年的閏年個數,而後用差值的方式計算出兩年之間的閏年個數。
可是值得注意的是,在首尾兩端的年份,咱們須要判斷它的日期是否包含2月29日,不然即便它是閏年也沒用。
而後我在這裏使用了一個C++中的用法:string
。這也是一種數據類型,用於字符和字符串。下面是三種用於聲明字符和字符串數組的方法:
char Array1[5] = { 'a','b','c','d','e' }; char Array2[5][10] = { "one","two","three","four","five" }; string Array3[5] = { "one","two","three","four","five" };
咱們都知道數組的下標嚴格控制數據的個數,在第一種數組中就是嚴格的5個數組。
第二種數組中,咱們使用的是二維數組,最開始數組裏面都是空,而後在錄入的過程當中會在字符結束的時候會將 '\0'
錄到結尾,可是不會顯示。也就是說本來5個的儲存空間的會變成6個,這是字符串數組,具體的請查詢專業書籍,這裏只是點出。
可是這會又一個很大的問題,那就是會浪費內存空間,並且這樣會聲明不少子數組,致使地址的複雜,可是若是是 string
數組的話,就不會浪費內存,並且他會在字符串錄入結束後自動賦予 '\0'
這無疑是一種很好的方法,而且 string
數組只有一個地址,那就是數組名的初地址,這與一維數組的應用是一致的,更方便使用和理解。
代碼以下:
#include <cstdio> #include <string> string months[12]= { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December" }; bool isLeapYear(int year); int Calculation(int year, int mon, int day); int main() { int T; scanf("%d", &T); for (int t = 1; t <= T; t++) { char CharStartMonth[10], CharEndMonth[10]; int StartDay, StartYear, StartMonth, EndDay, EndYear, EndMonth; scanf("%s %d, %d", &CharStartMonth, &StartDay, &StartYear); scanf("%s %d, %d", &CharEndMonth, &EndDay, &EndYear); for (int i = 0; i < 12; i++) // 將字符串對應的年份換算成數字年份 { if (CharStartMonth == months[i]) StartMonth = i + 1; if (CharEndMonth == months[i]) EndMonth = i + 1; } int num1 = Calculation(StartYear, StartMonth, StartDay); int num2 = Calculation(EndYear, EndMonth, EndDay); int res = num2 - num1; // 閏年差,得出兩個年份之間的閏年個數 if (isLeapYear(StartYear) && StartMonth == 2 && StartDay == 29) // 若是正好是 2月29日 的時候,則該年算是閏年 res++; printf("Case #%d: %d\n", t, res); } return 0; } bool isLeapYear(int year) // 閏年判斷函數 { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) return true; else return false; } int Calculation(int year, int mon, int day) { int sum = year / 4 + year / 400 - year / 100; // 計算出從 公元1年 至目前全部的閏年個數 if (isLeapYear(year) && (mon < 2 || mon == 2 && day < 29)) // 判斷起始年或終止年是否包含 2月29日 sum--; return sum; }
Given a positive integer N, you should output the most right digit of N^N.
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains a single positive integer N(1<=N<=1,000,000,000).
For each test case, you should output the rightmost digit of N^N.
2 3 4
7 6
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
題目的要求是 (1 <= N <= 1,000,000,000)
這麼一個範圍是絕對的會超出目前咱們全部的數據類型,甚至硬盤放不放不下都是一個問題。可是根據題意,咱們只須要求出最後一位的數就行。
那麼咱們經過初步的草稿運算能夠得出如下的規律:
7*7=49 7*7=49……9 49*7=343 7*9=63……3 343*7=2401 7*3=21……1 ……
那就是若是每次乘冪以前對10取餘,也就是隻取最後一位數字,而後繼續進行乘冪運算。這樣的話對最後一位的數字並無什麼改變。咱們的代碼也是源於這樣的一個思路而展開的。
值得注意的是咱們採用的是 long
而不是 int
,由於可能仍是在運算過程當中會有超出 int
範圍的數據出現,好比任何大於或等於 2
的數乘以 1,000,000,000
,則會超出 int
的範圍。因此爲了不這種事情的發生,咱們採用 long
。
代碼以下:
#include <stdio.h> long long Pow(long long base, long long index); int main() { int len = 0, num = 0; scanf("%d", &len); while (len--) { cin >> num; printf("%d\n", Pow(num, num)); } return 0; } long long Pow(long long base, long long index) { int temp = 1; base = base % 10; while (index > 0) { if (index % 2 == 1) temp = (temp * base) % 10; index = index / 2; base = (base * base) %10; } return temp; }
"都8102年了"是2018年網絡流行語,8102是將今年年份2018的各位數字倒排的結果,經常使用來表達「都這個年頭了,怎麼還有某某事情發生"之意。
這個梗的關鍵在於將年份各位數字倒排以後,獲得一個很是遙遠的將來年份。小Hi想知道,哪些年份適合使用這個梗。具體來講,一個年份Y,倒排以後獲得年份X,若是X-Y>=1000,咱們就認爲Y適合這個梗。
給定起止年份,請你判斷這段時間內有幾個年份適合這個梗。
兩個正整數表明年份,A和B。
1000 <= A <= B <= 9999
一個整數表明答案
2018 2020
2
這裏的思路十分簡單,只需將一個四位數致使而後減去原數大於等於1000便可。若是出現 1000 -> 0001
狀況,則看爲 1
便可。
代碼以下:
#include <iostream> #include <cstdio> using namespace std; int main() { int StartYear, EndYear, AllYear = 0; scanf("%d %d", &StartYear, &EndYear); for (int i = StartYear; i <= EndYear; i++) { int year = i, sum = 0; for (int j = 0; j < 4; j++) // 數據轉置 { sum = sum * 10 + year % 10; year /= 10; } if (sum - i >= 1000) AllYear++; } printf("%d", AllYear); return 0; }