設a、b、c均是0到9之間的數字,abc、bcc是兩個三位數,且有:abc+bcc=532。求知足條件的全部a、b、c的值。php
題目沒有任何輸入。ios
請輸出全部知足題目條件的a、b、c的值。
a、b、c之間用空格隔開。
每一個輸出佔一行。c++
#include <iostream> using namespace std; int main(){ for(int a = 0; a <= 9; ++a){ for (int b = 0; b <= 9 ; ++b) { for (int c = 0; c <=9 ; ++c) { if (a * 100 + b * 110 + c * 12 ==532 ){ printf("%d %d %d\n" , a, b, c); } } } } return 0; }
設N是一個四位數,它的9倍剛好是其反序數(例如:1234的反序數是4321)
求N的值git
程序無任何輸入數據。編程
輸出題目要求的四位數,若是結果有多組,則每組結果之間以回車隔開。api
#include <iostream> using namespace std; int Reverse(int x){ int revx = 0; while (x != 0){ revx *= 10; revx += x % 10; x /= 10; } return revx; } int main(){ for (int i = 1000; i <= 9999 ; ++i) { if (i * 9 == Reverse(i)){ printf("%d\n",i); } } return 0; }
打印全部不超過256,其平方具備對稱性質的數。如2,11就是這樣的數,由於22=4,1111=121。數組
無任何輸入數據less
輸出具備題目要求的性質的數。若是輸出數據不止一組,各組數據之間以回車隔開。函數
#include <iostream> using namespace std; int Reverse(int x){ int revx = 0; while (x != 0){ revx *= 10; revx += x % 10; x /= 10; } return revx; } int main(){ for (int i = 0; i <= 256; ++i) { if (i * i == Reverse(i * i)){ printf("%d\n",i); } } return 0; }
一個正整數,若是它能被7整除,或者它的十進制表示法中某個位數上的數字爲7, 則稱其爲與7相關的數.現求全部小於等於n(n<100)的與7無關的正整數的平方和。測試
案例可能有多組。對於每一個測試案例輸入爲一行,正整數n,(n<100)
對於每一個測試案例輸出一行,輸出小於等於n的與7無關的正整數的平方和。
21
2336
#include <iostream> using namespace std; bool judge(int n){ if(n % 7 == 0) return false; if(n % 10 == 7) return false; if(n / 10 == 7) return false; return true; } int main(){ int n; while(cin >> n){ int ret = 0; for(int i = 1; i <= n; i++){ if(judge(i)){ ret = ret + i * i; } } cout << ret << endl; } return 0;
用小於等於n元去買100只雞,大雞5元/只,小雞3元/只,還有1/3元每隻的一種小雞,分別記爲x只,y只,z只。編程求解x,y,z全部可能解。
測試數據有多組,輸入n。
對於每組輸入,請輸出x,y,z全部可行解,按照x,y,z依次增大的順序輸出。
40
x=0,y=0,z=100 x=0,y=1,z=99 x=0,y=2,z=98 x=1,y=0,z=99
#include <iostream> using namespace std; int main(){ int n; while(scanf("%d",&n)!=EOF){ for(int i = 0;i <= 100; ++i){ for(int j = 0;j <= 100-i; ++j){ int k=100-i-j; if(15*i+9*j+1*k<=3*n) printf("x=%d,y=%d,z=%d\n",i,j,k); } } } }
Among grandfather's papers a bill was found.
72 turkeys $_679_
The first and the last digits of the number that obviously represented the total price of those turkeys are replaced here by blanks (denoted_ ), for they are faded and are illegible. What are the two faded digits and what was the price of one turkey?
We want to write a program that solves a general version of the above problem.
N turkeys $_XYZ_
The total number of turkeys, N, is between 1 and 99, including both. The total price originally consisted of five digits, but we can see only the three digits in the middle. We assume that the first digit is nonzero, that the price of one turkeys is an integer number of dollars, and that all the turkeys cost the same price.
Given N, X, Y, and Z, write a program that guesses the two faded digits and the original price. In case that there is more than one candidate for the original price, the output should be the most expensive one. That is, the program is to report the two faded digits and the maximum price per turkey for the turkeys.
The first line of the input file contains an integer N (0<N<100), which represents the number of turkeys. In the following line, there are the three decimal digits X, Y, and Z., separated by a space, of the original price $XYZ.
For each case, output the two faded digits and the maximum price per turkey for the turkeys.
72 6 7 9 5 2 3 7 78 0 0 5
3 2 511 9 5 18475 0
#include<iostream> #include<cstring> using namespace std; int Solve(int N, int X, int Y, int Z) { bool find = false; int sum = 0; for (int i = 9; i >= 1; i--) { for (int j = 9; j >= 0; j--) { sum = i * 10000 + X * 1000 + Y * 100 + Z * 10 + j; if (sum%N == 0) { find = true; cout << i << " " << j << " " << (sum / N) << endl; return 0; } } } if (!find) { cout << 0 << endl; } return 0; } int main() { int N, X, Y, Z; while (cin >> N >> X >> Y >> Z) { Solve(N, X, Y, Z); } return 0; }
輸入一個高度h,輸出一個高爲h,上底邊爲h的梯形。
一個整數h(1<=h<=1000)。
h所對應的梯形。
4
#include <iostream> using namespace std; int main(){ int h; while(scanf("%d", &h) != EOF){ int row = h; int col = h + (h - 1) * 2; for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { if (j < col - (h + 2 * i)) { printf(" "); } else { printf("*"); } } printf("\n"); } } return 0; }
須要的時候,就把一個個大小差一圈的筐疊上去,使得從上往下看時,邊筐花色交錯。這個工做如今要讓計算機來完成,得看你的了。
輸入是一個個的三元組,分別是,外筐尺寸n(n爲知足0<n<80的奇整數),中心花色字符,外筐花色字符,後兩者都爲ASCII可見字符;
輸出疊在一塊兒的筐圖案,中心花色與外筐花色字符從內層起交錯相疊,多筐相疊時,最外筐的角老是被打磨掉。疊筐與疊筐之間應有一行間隔。
11 B A 5 @ W
AAAAAAAAA ABBBBBBBBBA ABAAAAAAABA ABABBBBBABA ABABAAABABA ABABABABABA ABABAAABABA ABABBBBBABA ABAAAAAAABA ABBBBBBBBBA AAAAAAAAA @@@ @WWW@ @W@W@ @WWW@ @@@
#include <iostream> #include <cstdio> using namespace std; char matrix[80][80]; int main(){ int n; char a,b; bool firstCase = true; while (scanf("%d %c %c", &n,&a,&b) != EOF){ if(firstCase == true){ firstCase = false; }else{ printf("\n"); } for (int i = 0; i <= n / 2; ++i) { int j = n - i - 1; int length = n - 2 * i; char c; if((n / 2 - i) % 2 == 0){ c = a; }else{ c = b; } for (int k = 0; k < length; ++k) { matrix[i][i + k] = c; matrix[i + k][i] = c; matrix[j][j - k] = c; matrix[j - k][j] = c; } } if( n != 1){ matrix[0][0] = ' '; matrix[0][n-1]= ' '; matrix[n-1][0]= ' '; matrix[n-1][n-1]= ' '; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { printf("%c", matrix[i][j]); } printf("\n"); } } return 0; }
Harmony is indispensible in our daily life and no one can live without it----may be Facer is the only exception. One day it is rumored that repeat painting will create harmony and then hundreds of people started their endless drawing. Their paintings were based on a small template and a simple method of duplicating. Though Facer can easily imagine the style of the whole picture, but he cannot find the essential harmony. Now you need to help Facer by showing the picture on computer.
You will be given a template containing only one kind of character and spaces, and the template shows how the endless picture is created----use the characters as basic elements and put them in the right position to form a bigger template, and then repeat and repeat doing that.
Here is an example.
The input contains multiple test cases.
The first line of each case is an integer N, representing the size of the template is N*N (N could only be 3, 4 or 5).
Next N lines describe the template.
The following line contains an integer Q, which is the Scale Level of the picture.
Input is ended with a case of N=0.
It is guaranteed that the size of one picture will not exceed 3000*3000.
For each test case, just print the Level Q picture by using the given template.
3 # # # # # 1 3 # # # # # 3 4 OO O O O O OO 2 0
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # OO OO O OO O O OO O OO OO OO OO O O O O O O O O OO OO OO OO O O O O O O O O OO OO OO OO O OO O O OO O OO OO
#include <iostream> #include <cstdio> #include <cmath> #include <string.h> using namespace std; char map[3003][3003]; char str[6][6]; int n; void dfs(int m, int x, int y){ if (m == 1){ for (int i = 0; i<n; i++) for (int j = 0; j<n; j++) map[x + i][y + j] = str[i][j]; return; } int size = (int)pow(n*1.0, m - 1); for (int i = 0; i<n; i++){ for (int j = 0; j<n; j++){ if (str[i][j] != ' ') dfs(m - 1, x + i*size, y + j*size); } } } int main(void){ n = 1; while (n){ cin >> n; getchar(); for (int i = 0; i < n; i++){ cin.getline(str[i],6); } int m; cin >> m; int size = (int)pow(n*1.0, m); for (int i = 0; i<size; i++){ for (int j = 0; j<size; j++) map[i][j] = ' '; map[i][size] = '\0'; } dfs(m, 0, 0); for (int i = 0; i<size; i++) cout<<map[i]<<endl; } return 0; }
Given any string of N (>=5) characters, you are asked to form the characters into the shape of U.
For example, "helloworld" can be printed as: h d e l l r lowo
That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters.
And more, we would like U to be as squared as possible -- that is, it must be satisfied that $n_1 = n_3 = max { k| k <= n_2 \ for\ all \ 3 \le n_2 \le N } $with n1 + n2 + n3 - 2 = N.
There are multiple test cases.Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.
For each test case, print the input string in the shape of U as specified in the description.
helloworld! www.nowcoder.com
h ! e d l l lowor w m w o w c . . n r owcode
#include<iostream> #include<string> using namespace std; int main(){ string s; while(cin>>s){ int len=s.size(); len += 2; int l = len/3 - 1;//(len+2)/3就是n1和n2的值 int mid=len-len/3*2; for(int i = 0;i < l; ++i){ cout<<s[i]; for(int j = 0;j < mid-2;++j){ cout<<" "; } cout<<s[s.size()-1-i]<<endl; } for(int i = l;i < l+mid; ++i){ cout<<s[i]; } } return 0; }
輸入年、月、日,計算該天是本年的第幾天。
包括三個整數年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。
輸入可能有多組測試數據,對於每一組測試數據,
輸出一個整數,表明Input中的年、月、日對應本年的第幾天。
1990 9 20 2000 5 1
263 122
#include <iostream> using namespace std; int daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, }; bool IsLeapYear(int year){ return (year % 4 ==0 && year % 100 != 0 ) || (year % 400 == 0); } int main(){ int year, month, day; while (scanf("%d %d %d", &year, &month, &day) != EOF){ int number = 0; int row = IsLeapYear(year); for (int i = 0; i < month; ++i) { number += daytab[row][i]; } number += day; printf("%d\n",number); } return 0; }
給出年份m和一年中的第n天,算出第n天是幾月幾號。
輸入包括兩個整數y(1<=y<=3000),n(1<=n<=366)。
可能有多組測試數據,對於每組數據,
按 yyyy-mm-dd的格式將輸入中對應的日期打印出來。
2000 3 2000 31 2000 40 2000 60 2000 61 2001 60
2000-01-03 2000-01-31 2000-02-09 2000-02-29 2000-03-01 2001-03-01
#include <iostream> using namespace std; int daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, }; bool IsLeapYear(int year){ return (year % 4 ==0 && year % 100 != 0 ) || (year % 400 == 0); } int main(){ int year, month, day; int number; while (scanf("%d %d", &year, &number) != EOF){ month = 0; int row = IsLeapYear(year); while (number > daytab[row][month]){ number -= daytab[row][month]; month++; } day = number; printf("%04d-%02d-%02d\n", year, month, day); } return 0; }
設計一個程序能計算一個日期加上若干天后是什麼日期。
輸入第一行表示樣例個數m,接下來m行每行四個整數分別表示年月日和累加的天數。
輸出m行,每行按yyyy-mm-dd的個數輸出。
1 2008 2 3 100
2008-05-13
#include <iostream> using namespace std; int daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, }; bool IsLeapYear(int year){ return (year % 4 ==0 && year % 100 != 0 ) || (year % 400 == 0); } int NumberofYear(int year){ if(IsLeapYear(year)){ return 366; } else{ return 365; } } int main(){ int year, month, day; int number; int caseNumber; scanf("%d", &caseNumber); while (caseNumber--){ scanf("%d %d %d %d", &year, &month, &day, &number); int row = IsLeapYear(year); for (int i = 0; i < month; ++i) { number += daytab[row][i]; } number += day; while (number > NumberofYear(year)){ number -= NumberofYear(year); year++; } month = 0; row = IsLeapYear(year); while (number > daytab[row][month]){ number -= daytab[row][month]; month++; } day = number; printf("%04d-%02d-%02d\n", year, month, day); } return 0; }
有兩個日期,求兩個日期之間的天數,若是兩個日期是連續的咱們規定他們之間的天數爲兩天
有多組數據,每組數據有兩行,分別表示兩個日期,形式爲YYYYMMDD
每組數據輸出一行,即日期差值
20110412 20110422
11
#include <iostream> #include <cstdio> using namespace std; int daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, }; bool IsLeapYear(int year){ return (year % 4 ==0 && year % 100 != 0 ) || (year % 400 == 0); } int NumberofYear(int year){ if(IsLeapYear(year)){ return 366; } else{ return 365; } } int Date(int year, int month, int day) { int dateth = 0; for (int i = 0; i < year; i++) { dateth += NumberofYear(i); } for (int i = 0; i < month; i++) { int row = IsLeapYear(year); dateth += daytab[row][i]; } dateth += day; return dateth; } int main(){ int time1,year1,month1,day1; int time2,year2,month2,day2; while(scanf("%d%d",&time1,&time2)!=EOF){ year1 = time1/10000; month1 = time1%10000/100; day1 = time1%100; year2 = time2/10000; month2 = time2%10000/100; day2 = time2%100; printf("%d\n", abs(Date(year1, month1, day1)-Date(year2, month2, day2))+1); } return 0; }
We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400. For example, years 2004, 2180 and 2400 are leap. Years 2005, 2181 and 2300 are not leap. Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.
There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.
Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.
Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
9 October 2001 14 October 2001
Tuesday Sunday
/** * @author: Qiuyue Zhang * @date: 2021/2/2 18:59 * @description: */ #include <cstdio> #include <cstring> int daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, }; bool IsLeapYear(int year){ return (year % 4 ==0 && year % 100 != 0 ) || (year % 400 == 0); } int NumberofYear(int year){ if(IsLeapYear(year)){ return 366; } else{ return 365; } } // 計算從公元1年1月1號到當前的天數 int Date(int year, int month, int day) { int dateth = 0; for (int i = 1; i < year; i++) { dateth += NumberofYear(i); } for (int i = 0; i < month; i++) { int row = IsLeapYear(year); dateth += daytab[row][i]; } dateth += day-1; return dateth; } char month_name[13][20]={ "","January","February","March","April","May","June","July","August", "September","October","November","December" }; char week_name[7][20]={ "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday" }; int main(){ int year, month, day; char Month[20]; while(scanf("%d%s%d", &day, &Month, &year)!=EOF){ for (int i = 1; i <= 12; ++i) { if(strcmp(Month,month_name[i])==0){ month = i; break; } } printf("%s\n",week_name[(Date(year,month,day))%7]); } return 0; }
編寫一個日期類,要求按xxxx-xx-xx 的格式輸出日期,實現加一天的操做。
輸入第一行表示測試用例的個數m,接下來m行每行有3個用空格隔開的整數,分別表示年月日。測試數據不會有閏年。
輸出m行。按xxxx-xx-xx的格式輸出,表示輸入日期的後一天的日期。
2 1999 10 20 2001 1 31
1999-10-21 2001-02-01
注意個位很多天期前面要有0。
#include<iostream> #include<cstring> using namespace std; const int month[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int fun(int year, int mon, int day) { if (mon == 12 && day == 31) { year++; mon = day = 1; } else { if (day < month[mon]) { day++; } else { mon++; day = 1; } } printf("%04d-%02d-%02d\n", year, mon, day); return 0; } int main() { int m, year, mon, day; while (cin >> m) { for (int i = 0; i < m; i++) { cin >> year >> mon >> day; fun(year, mon, day); } } return 0; }
有一個長度爲整數L(1<=L<=10000)的馬路,能夠想象成數軸上長度爲L的一個線段,起點是座標原點,在每一個整數座標點有一棵樹,即在0,1,2,...,L共L+1個位置上有L+1棵樹。 如今要移走一些樹,移走的樹的區間用一對數字表示,如 「100 200」 表示移走從100到200之間(包括端點)全部的樹。 可能有M(1<=M<=100)個區間,區間之間可能有重疊。如今要求移走全部區間的樹以後剩下的樹的個數。
兩個整數L(1<=L<=10000)和M(1<=M<=100)。
接下來有M組整數,每組有一對數字。
可能有多組輸入數據,對於每組輸入數據,輸出一個數,表示移走全部區間的樹以後剩下的樹的個數。
500 3 100 200 150 300 470 471
298
#include <iostream> #include <cstdio> using namespace std; const int MAXN = 10001; bool arr[MAXN]; int main(){ int l, m; while (scanf("%d%d", &l, &m) != EOF){ for (int i = 0; i <= l; ++i) { arr[i] = true; } int number = l + 1; while (m--){ int left, right; scanf("%d%d", &left, &right); for (int i = left; i <= right ; ++i) { if(arr[i]){ arr[i] = false; number -- ; } } } printf("%d\n",number); } }
按照手機鍵盤輸入字母的方式,計算所花費的時間 如:a,b,c都在「1」鍵上,輸入a只須要按一次,輸入c須要連續按三次。 若是連續兩個字符不在同一個按鍵上,則可直接按,如:ad須要按兩下,kz須要按6下 若是連續兩字符在同一個按鍵上,則兩個按鍵之間須要等一段時間,如ac,在按了a以後,須要等一下子才能按c。 如今假設每按一次須要花費一個時間段,等待時間須要花費兩個時間段。 如今給出一串字符,須要計算出它所須要花費的時間。
一個長度不大於100的字符串,其中只有手機按鍵上有的小寫字母
輸入可能包括多組數據,對於每組數據,輸出按出Input所給字符串所須要的時間
bob www
7 7
#include<iostream> #include<string> using namespace std; int main() { int key[26] = {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,4,1,2,3,1,2,3,4}; string str; while(cin>>str) { int count = key[str[0]-'a']; for(int i=1;i<str.size();++i) { count += key[str[i]-'a']; if(key[str[i]-'a']-key[str[i-1]-'a']==str[i]-str[i-1])//判斷是否在同一個按鍵上 count+=2; } cout<<cout<<endl; } }
對於一個數n,若是是偶數,就把n砍掉一半;若是是奇數,把n變成 3*n+ 1後砍掉一半,直到該數變爲1爲止。 請計算須要通過幾步才能將n變到1,具體可見樣例。
測試包含多個用例,每一個用例包含一個整數n,當n爲0 時表示輸入結束。(1<=n<=10000)
對於每組測試用例請輸出一個數,表示須要通過的步數,每組輸出佔一行。
3 1 0
5 0
#include <iostream> using namespace std; int main(){ int n; while (scanf("%d", &n) != EOF){ if(n == 0){ break; } int step = 0; while (n != 1){ if(n % 2 == 0){ n = n / 2; }else{ n = (3 * n + 1) / 2; } step++; } printf("%d\n",step); } }
Grading hundreds of thousands of Graduate Entrance Exams is a hard work. It is even harder to design a process to make the results as fair as possible. One way is to assign each exam problem to 3 independent experts. If they do not agree to each other, a judge is invited to make the final decision. Now you are asked to write a program to help this process. For each problem, there is a full-mark P and a tolerance T(<P) given. The grading rules are:
Each input file may contain more than one test case.
Each case occupies a line containing six positive integers: P, T, G1, G2, G3, and GJ, as described in the problem. It is guaranteed that all the grades are valid, that is, in the interval [0, P].
For each test case you should output the final grade of the problem in a line. The answer must be accurate to 1 decimal place.
20 2 15 13 10 18
14.0
爲成千上萬的研究生入學考試評分是一項艱苦的工做。評分儘量公平的就更難了。一種方法是將每一個考試題分配給3個獨立的專家。若是雙方意見不一致,請審判長做出最後決定。如今你被要求寫一個程序來幫助這個過程。對於每一個問題,都有一個滿分P和一個公差T(<P)。評分規則以下:
#include<iostream> #include<iomanip> #include<math.h> using namespace std; int main() { int p, t; int g1, g2, g3, g4; while (scanf("%d%d%d%d%d%d", &p, &t, &g1, &g2, &g3, &g4) != EOF){ double score; if (abs(g1 - g2) <= t) score = (double) (g1 + g2) / 2; else { if (abs(g3 - g1) <= t && abs(g3 - g2) > t) score = (double) (g3 + g1) / 2; else if (abs(g3 - g2) <= t && abs(g3 - g1) > t) score = (double) (g3 + g2) / 2; else if (abs(g3 - g2) <= t && abs(g3 - g1) <= t) { int temp = g3 > g2 ? g3 : g2; score = temp > g1 ? temp : g1; } else score = g4; } printf("%0.1f\n", score); } return 0; }
給你一串路徑,譬如: a\b\c a\d\e b\cst d\ 你把這些路徑中蘊含的目錄結構給畫出來,子目錄直接列在父目錄下面,並比父目錄向右縮一格,就像這樣: a b c d e b cst d 同一級的須要按字母順序排列,不能亂。
每一個測試案例第一行爲一個正整數n(n<=10)表示有n個路徑,當n爲0時,測試結束,接下來有n行,每行有一個字串表示一個路徑,長度小於50。
輸出目錄結構,每個測試樣例的輸出緊跟一個空行。
4 a\b\c a\d\e b\cst d\ 0
a b c d e b cst d
#include<iostream> #include<string> #include<algorithm> #include <vector> using namespace std; const int MAXN = 11; vector <string> vec[MAXN]; int n; string s; int main() { while (cin >> n && n != 0) { for (int i = 0; i < n; i++) { cin >> s; vec[i].clear();//清除上一次的 vec[i].push_back(s); } sort(vec, vec + n); for (int i = 0; i < n; i++) {//截取 把形如「a\b\c」的字符串分割成[「a」,「b」,「c」]這樣的字符串數組 s = vec[i][0]; vec[i].clear(); int j = 0, index; while (j < s.size()) { if ((index = s.find('\\', j)) != string::npos) { vec[i].push_back(s.substr(j, index - j));//substr截取函數(從j位開始,截取index-j位) j = index + 1; } else { vec[i].push_back(s.substr(j, s.size() - j)); break; } } } for (int i = 0; i < n; i++) {//打印 if (i == 0) {//第一個直接輸出 for (int j = 0; j < vec[i].size(); j++) { for (int k = 0; k < j; k++)cout << " "; cout << vec[i][j] << endl; } } else {//找到和前一個第一個不相等的位置 int j = 0; while (j < vec[i - 1].size() && j < vec[i].size() && vec[i][j] == vec[i - 1][j])j++; if (j == 0) {//若是第一個字母就不相同,直接輸出 for (int k = 0; k < vec[i].size(); k++) { for (int l = 0; l < k; l++)cout << " "; cout << vec[i][k] << endl; } } else {//找到了第一個不相同的位置j,從j開始輸出 for (int k = j; k < vec[i].size(); k++) { for (int l = 0; l < k; l++)cout << " "; cout << vec[i][k] << endl; } } } } cout << endl; } return 0; }
一根長度爲1米的木棒上有若干只螞蟻在爬動。它們的速度爲每秒一釐米或靜止不動,方向只有兩種,向左或者向右。若是兩隻螞蟻碰頭,則它們當即交換速度並繼續爬動。三隻螞蟻碰頭,則兩邊的螞蟻交換速度,中間的螞蟻仍然靜止。若是它們爬到了木棒的邊緣(0或100釐米處)則會從木棒上墜落下去。在某一時刻螞蟻的位置各不相同且均在整數釐米處(即1,2,3,…99釐米),有且只有一隻螞蟻A速度爲0,其餘螞蟻均在向左或向右爬動。給出該時刻木棒上的全部螞蟻位置和初始速度,找出螞蟻A今後時刻到墜落所須要的時間。
第一行包含一個整數表示螞蟻的個數N(2<=N<=99),以後共有N行,每一行描述一隻螞蟻的初始狀態。每一個初始狀態由兩個整數組成,中間用空格隔開,第一個數字表示初始位置釐米數P(1<=P<=99),第二個數字表示初始方向,-1表示向左,1表示向右,0表示靜止。
螞蟻A從開始到墜落的時間。若不會墜落,輸出「Cannot fall!」
4 10 1 90 0 95 -1 98 -1
98
#include<cstdio> #include<vector> #include<algorithm> using namespace std; struct Ant { int position; int direct; //方向 bool operator<(const Ant &a) const { return position < a.position; } }; int main() { int n; while (scanf("%d", &n) != EOF) { vector<Ant> ant(n); for (int i = 0; i < n; i++) scanf("%d %d", &ant[i].position, &ant[i].direct); sort(ant.begin(), ant.end()); int target, toLeft = 0; //這裏選用向左走的爲基準來作 for (int i = 0; i < n; i++) //遍歷全部螞蟻 { if (ant[i].direct == 0) target = i; if (ant[i].direct == -1) toLeft++; }//如今的target就是靜止的螞蟻左邊的數量了 bool flag = false; int ans; if (toLeft == target) flag = true; else if (toLeft > target)//這樣的話咱們要找的就是全部向左走的螞蟻中,第target螞蟻 { int cnt = 0;//計數器 for (int i = 0; i < n; i++) { if (ant[i].direct == -1 && cnt == target) { ans = ant[i].position; break; } else if (ant[i].direct == -1) cnt++; } } else //向左走的螞蟻少,那麼目標螞蟻會向右落下 { int cnt = 0; for (int i = n - 1; i >= 0; i--) { if (ant[i].direct == 1 && cnt == n - target - 1)//相應的變化,cnt要變成靜止螞蟻右邊的螞蟻數量 { ans = 100 - ant[i].position; break; } else if (ant[i].direct == 1) cnt++; } } if (flag) printf("Cannot fall!\n"); else printf("%d\n", ans); } return 0; }