给定若干字符串去除空格(可能包括空格),按顺序相连,并输出长度

算法入门习题108道_中华文本库
第1页/共17页
本题库来源:大榕树论坛www.mydrs.org
入门习题一百道。
字符串处理
1. 读入N 个字母的字,判断该字是否回文。
2. 有N 组数据,每组中有M 个字串,找出所有符合下列条件的字串S ,条件是:在每一组中,字串S 都是其M 个字串之一。
3. 读入一个字串(长度不大于50),删去字串中相同的字符。
4. 输入十个字符串,长度在20….50之间。检查每个字串,若遇到字母A ,就在其后插一个空格,最后打印全部处理后的字串,并统计26个字母各出现多少次。
5. 接受一个任意长度的字串(小于100个字符),将该字符串中的字符按照ASCII 码表从小到大的顺序重新排列成一个新的字符串并打印出来。
6. 有一段文字由若干字母组成。检查文字中每个字母出现的次数,输出时,按字母出现频率的降序排序,并按以下格式打印(不含未出现字母):
频率 百分比%
7. 读入一行文本,包含若干个单词(以空格间隔,以%j结尾)。将其中以A 开头的单词与以N 结尾的单词,用头尾交换的方法予以置换。
8. 求出一个子串在字符串中的位置。
9. 在若干个字符串中,找出一个长度最长的公共字串。
10. ’DOS ’称为良序子串,因为他之中的字母是按ASCII 码的顺序排列的,而’DATA ’则不是良序字符。
先要求给出一个N*N的字母方阵,从中选出给定长度M 的良序字符。
11. 从键盘上读入一个字符串(称为原串),其长度不超过50个字符,并以字符 ’$’ 作为结束符。
当原串读入后,对原串进行编辑,其功能有(菜单选择):
① 在原串中制定的字符前面插入若干个字符(字串)。若在原串中有若干个指定的字符,则插入在第一个指定字符的前面。
例: 原串 ‘abcd dby’
在指定字符 ‘b ’ 的前面插入 ‘umv ’ ,此时字符串成为 ‘auwvbc dby’
② 在原串中,删除指定的子串,若原串中有多个相同的子串,则删除最后一个子串。
例: 原串 ‘abxyzw cdxyzh,uvxxyzh’
删除子串’xyz ’后得到:
‘abxyzw cdxyzh,uvxh’
③ 替换操作:在原串中,将某个子串用新的子串去替换。若原串中有多个被替换的子串,则应全部替换(但不递归替换)。
例: 原串 ‘abxyz ddxyzxyzhe’
将原串的’xyz ’用’uvw ’替换,替换后成为:
‘abuvw dduvwuvwhe’
第1页/共17页
寻找更多 ""第一行输入一个数字N,接下来要输入N个字符串(包含空格且小于100个字符)。 输出a,e,i,o,u出现次数_百度知道
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。
第一行输入一个数字N,接下来要输入N个字符串(包含空格且小于100个字符)。 输出a,e,i,o,u出现次数
#include &stdio.h&#include &string&int main(){ int n,j,k,a,e,i,o,u; char str[100]; while(scanf(&%d&,&n)!=EOF){
a=e=i=o=u=0;
for(j=0;j&n;j++){
gets(str);
for(k=0;k&strlen(str);k++)
switch(str[k]){
case 'a':{
我有更好的答案
&&&&&&&&//getchar();char&&&&&}&&&&&&&&&&&&&&&&case&'&&&&&&nbsp1;&strlen(str);&nbsp,i;&nbsp,j;&&&&nbsp、输入scanf (&%d&;&&&a'&&e++;&&&&&&&&&&&&&&&&&nbsp、头文件错误;&&&&&&&&&&&&&&&&&nbsp,&n);&&&i'&&nbsp,a;&&&&&&&&&nbsp,#include &&string.h&&&&&&&&nbsp, &&&&&&&&&&&&&&&&&i++;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&case&'o':{&&&&&&&&&&&&&&&&&&&&o++;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&case&'u':{&&&&&&&&&&&&&&&&&&&&u++;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&default:{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&}&&&&&&&&&&&&printf(&%d&%d&%d&%d&%d\n&,a,e,i,o,u);&&&&&&&&}&&&&}&&&&return&0;};&&:{&&&nbsp,e;/////////////////////////&&&&&&&&for(j=0;j&lt,o;&&&&n)后加一个getchar()获得回车或者使用fflush(stdin)清空输入缓冲区#include&&stdio.h&&&nbsp,u;&a++;}&&&&&&&&&&&&&&&&case&'&&&&&&#include&&&&&switch(str[k]){&&&&&&&&&&&&&&&&case&'&:{&&&&:{&&&&&&&nbsp,k;&&&&&n;n;&&&/////////////////int&nbsp.h&2;&&&nbsp!=EOF){&&&&&&j++){&&&&&&&&&&&&gets(str);&&&&&&&&&&&&for(k=0;k&k++)&&&&a=e=i=o=u=0;&&&&&&&&&&str[100];&&&&while(scanf(&%d&;&fflush(stdin);&&&&&&&e'main(){&&&&int&&&nbsp
采纳率:84%
来自团队:
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包当前位置: >>
算法题目及答案
1.题目描述: 对输入的 n 个数进行排序并输出。 输入: 输入的第一行包括一个整数 n(1&=n&=100)。 接下来的一行包括 n 个整数。 输出: 可能有多组测试数据,对于每组数据,将排序后的 n 个整数输出,每个数后面都有一个 空格。 每组测试数据的结果占一行。 样例输入: 4 1432 样例输出: 1234#include &iostream& #include &vector& #include &algorithm& int main() { while(cin&&n&&n&=1&&n&=100) { vector&int& for(int i=0;i&n;i++) { cin&& ivec.push_back(itemp); } sort(ivec.begin(),ivec.end()); for(vector&int&::iterator j=ivec.begin();j!=ivec.end();j++) cout&&*j&&' '; cout&& } return 0; }2.有 N 个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排 序,如果姓名的字母序也相同则按照学生的年龄排序,并输出 N 个学生排序后的信息。 输入: 测试数据有多组,每组输入第一行有一个整数 N(N&=1000) ,接下来的 N 行包括 N 个 学生的数据。 每个学生的数据包括姓名(长度不超过 100 的字符串) 、年龄(整形数) 、成绩(小于等 于 100 的正数) 。 输出: 将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。 然后输出学生信息,按照如下格式: 姓名 年龄 成绩 样例输入: 3 abc 20 99 bcd 19 97 bed 20 97 样例输出: bcd 19 97 bed 20 97 abc 20 99 #include&stdio.h& #include&string.h& struct student { char name[101]; }; int main() { int n,i,j; struct student a[1000], while(scanf(&%d&,&n)!=EOF) { for(i=0;i&n;i++) { scanf(&%s %d %d&,&a[i].name,&a[i].age,&a[i].score); } for(i=1;i&n;i++) { for(j=1;j&=n-i;j++) { if(a[j-1].score&a[j].score) { temp=a[j-1]; a[j-1]=a[j]; a[j]= } else if(a[j-1].score==a[j].score) { if(strcmp(a[j-1].name,a[j].name)&0) { temp=a[j-1]; a[j-1]=a[j]; a[j]= } else if(strcmp(a[j-1].name,a[j].name)==0) { if(a[j-1].age&a[j].age) { temp=a[j-1]; a[j-1]=a[j]; a[j]= } } } } } for(i=0;i&n;i++) { printf(&%s %d %d\n&,a[i].name,a[i].age,a[i].score); } } return 0; }4. 题目描述: 输入一系列整数,将其中最大的数挑出,并将剩下的数进行排序。 输入: 输入第一行包括 1 个整数 N,1&=N&=1000,代表输入数据的个数。 接下来的一行有 N 个整数。 输出: 可能有多组测试数据,对于每组数据, 第一行输出一个整数,代表 N 个整数中的最大值,并将此值从数组中去除,将剩下的数进 行排序。 第二行将排序的结果输出。 样例输入: 4 1342 样例输出: 4 123 #include &stdio.h& int main(){ int i,n,t,j,a[1000]; while(scanf(&%d&,&n)!=EOF){ for(i=0;i&n;i++){ scanf(&%d&,&a[i]); t=a[i]; j=i-1; while(j&=0&&a[j]&t) {a[j+1]=a[j];j--;} a[j+1]=t; } printf(&%d\n&,a[n-1]); if(n==1) printf(&-1\n&); else for(i=0;i&n-1;i++){ printf(&%d&,a[i]); if(i!=n-2) printf(& &); else printf(&\n&);} } return 0; }5. 题目描述: Excel 可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能。 对每个测试用例,首先输出 1 行“Case i:” ,其中 i 是测试用例的编号(从 1 开始) 。随 后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2 时,按 姓名的非递减字典序排序;当 C=3 时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增 排序。 输入: 测试输入包含若干测试用例。每个测试用例的第 1 行包含两个整数 N (N&=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有 N 行,每行包含一条学生纪录。 每条学生纪录由学号(6 位数字,同组测试中没有重复的学号) 、姓名(不超过 8 位且不包 含空格的字符串) 、成绩(闭区间[0, 100]内的整数)组成,每个项目间用 1 个空格隔开。当 读到 N=0 时,全部输入结束,相应的结果不要输出。 输出: 对每个测试用例,首先输出 1 行“Case i:” ,其中 i 是测试用例的编号(从 1 开始) 。随 后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2 时,按 姓名的非递减字典序排序;当 C=3 时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增 排序。 样例输入: 31 000007 James 85 000010 Amy 90 000001 Zoe 60 42 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 98 43 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 90 00 样例输出: Case 1: 000001 Zoe 60 000007 James 85 000010 Amy 90 Case 2: 000010 Amy 90 000002 James 98 000007 James 85 000001 Zoe 60 Case 3: 000001 Zoe 60 000007 James 85 000002 James 90 000010 Amy 90 #include &iostream& #include &fstream& #include &string& #include &algorithm& struct info{ }; bool cmp_id(const struct info a,const struct info b) { return a.id&b. } bool cmp_name(const struct info a,const struct info b) { if(a.name==b.name) return a.id&b. else return a.name&b. } bool cmp_score(const struct info a,const struct info b) { if(a.score==b.score) return a.id&b. else return a.score&b. } int main(int argc, char **argv) { //ifstream cin(&input.txt&); int n,c,count=0; while(cin&&n&&c) { if(n==0&&c==0) count++; struct info data[n]; for(int i=0;i&n;i++) cin&&data[i].id&&data[i].name&&data[i]. switch(c) { case 1: sort(data,data+n,cmp_id); case 2: sort(data,data+n,cmp_name); case 3: sort(data,data+n,cmp_score); } printf(&Case %d:\n&,count); for(int i=0;i&n;i++) printf(&%s %s %d\n&,data[i].id.c_str(),data[i].name.c_str(),data[i].score); } }6.题目描述: 输入一个字符串,长度小于等于 200,然后将输出按字符顺序升序排序后的字符串。 输入: 测试数据有多组,输入字符串。 输出: 对于每组输入,输出处理后的结果。 样例输入: bacd 样例输出: Abcd #include&stdio.h& #include&string.h& int main() { char string[200], int i,j,n; while(scanf(&%s&,string)!=EOF) { n=0; while(string[n]) { n++; } for(i=1;i&n;i++) { for(j=0;j&n-i;j++) { if((string[j]-string[j+1])&0) { temp=string[j]; string[j]=string[j+1]; string[j+1]= } } } printf(&%s\n&,string); } return 0; } 7. 题目描述: 有两个日期, 求两个日期之间的天数, 如果两个日期是连续的我们规定他们之间的天数为两 天 输入: 有多组数据,每组数据有两行,分别表示两个日期,形式为 YYYYMMDD 输出: 每组数据输出一行,即日期差值 样例输入: 10422 样例输出: 11 #include&stdio.h& #include&math.h& #define MAX_DATE_SIZE 9 int Days[12]={31,28,31,30,31,30,31,31,30,30,31}; typedef struct Date { int Y int M int D }D int main(int argc,char *argv) { int i,L; int HLN1,HLN2; int sDay,eD Date sDate,eD char sdate[MAX_DATE_SIZE],edate[MAX_DATE_SIZE]; while(scanf(&%s&,sdate)!=EOF) { sDay=0; eDay=0; //日期格式转化 sDate.Year=(sdate[0]-'0')*1000+(sdate[1]-'0')*100+(sdate[2]-'0')*10+(sdate[3]-'0'); sDate.Month=(sdate[4]-'0')*10+(sdate[5]-'0'); sDate.Day=(sdate[6]-'0')*10+(sdate[7]-'0'); scanf(&%s&,edate); eDate.Year=(edate[0]-'0')*1000+(edate[1]-'0')*100+(edate[2]-'0')*10+(edate[3]-'0'); eDate.Month=(edate[4]-'0')*10+(edate[5]-'0'); eDate.Day=(edate[6]-'0')*10+(edate[7]-'0'); HLN1=(int)(sDate.Year/4)-(int)(sDate.Year/100)+(int)(sDate.Year/400); HLN2=(int)(eDate.Year/4)-(int)(eDate.Year/100)+(int)(eDate.Year/400); for(i=0;i&sDate.Month-1;++i) sDay+=Days[i]; for(i=0;i&eDate.Month-1;++i) eDay+=Days[i]; sDay+=sDate.D eDay+=eDate.D L=(int)fabs((sDate.Year*365+HLN1+sDay)-(eDate.Year*365+HLN2+eDay))+1; printf(&%d\n&,L); } return 1; } 8. 题目描述://蔡勒公式计算星期几公式 #include&iostream& intmain(){ intyear,month, while(cin&&year&&month&&day){ if(month&3){ year-=1; month+=12; } charb[7][10]={&sunday&,&monday&,&tuesday&,&wednesday&,&thursday&,&friday&,&saturday&}; intc=int(year/100) ,y=year-100*c; intw=int(c/4)-2*c+y+int(y/4)+(26*(month+1)/10)+day-1; w=(w%7+7)%7; cout&&b[w]&&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
and 2400 are leap. Years
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. 样例输入: 9 October 2001 14 October 2001 样例输出: Tuesday Sunday #include&iostream& #include&cstring& int main() { char month[12][20]={&January&,&February&,&March&,&April&,&May&,&June&,&July&,&August&,&September &,&October&,&November&,&December&};//egg broken... string week[7]={&Sunday&,&Monday&,&Tuesday&,&Wednesday&,&Thursday&,&Friday&,&Saturday&}; int m_day[12]={0,3,3,6,1,4,6,2,5,0,3,5}; int l_day[12]={0,3,4,0,2,5,0,3,6,1,4,6}; int year,day,m, char mon[20]; while(cin&&day&&mon&&year) { for(i=0;i&12;i++) { if(strcmp(month[i],mon)==0) m=i; } if((year%4==0&&year%100!=0)||(year%400==0)) ans=(year+year/4+year/400-year/100-2+l_day[m]+day)%7; else ans=(year+year/4+year/400-year/100-1+m_day[m]+day)%7; cout&&week[ans]&& } return 0; } 9. 题目描述: 输入年、月、日,计算该天是本年的第几天。 输入: 包括三个整数年(1&=Y&=3000)、月(1&=M&=12)、日(1&=D&=31)。 输出: 输入可能有多组测试数据,对于每一组测试数据, 输出一个整数,代表 Input 中的年、月、日对应本年的第几天。 样例输入:
样例输出: 263 122 #include &iostream& bool fun(int year,int month) { if((year%400==0||(year%4==0&&year%100!=0))&&month&2)//是闰年且月 份大于 2 月 } int main() { int y,m,d; while(cin&&y&&m&&d) { if(y&1||y&3000||m&1||m&12||d&1||d&31)return 1; int sum=0; switch(m) { case 12: sum+=30; case 11: sum+=31; case 10: sum+=30; case 9: sum+=31; case 8: sum+=31; case 7: sum+=30; case 6: sum+=31; case 5: sum+=30; case 4: sum+=31; case 3: sum+=28; case 2: sum+=31; default: } sum+=d; if(fun(y,m))sum++; cout&&sum&& } return 0; } 10. 题目描述: 给出年分 m 和一年中的第 n 天,算出第 n 天是几月几号。 输入: 输入包括两个整数 y(1&=y&=3000),n(1&=n&=366)。 输出: 可能有多组测试数据,对于每组数据, 按 yyyy-mm-dd 的格式将输入中对应的日期打印出来。 样例输入: 0 31 0 60 1 60 样例输出:
#include &stdio.h& #include &stdlib.h& #include &string.h& void showFormatdate(int, int); int main() { int year, while(scanf(&%d %d&, &year, &dates) != EOF) { showFormatdate(year, dates); } } void showFormatdate(int year, int dates) { int sum, i, m, int month[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; if((year % 100 == 0) && (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0)) { //润年 month[1] = 29; } for(i = 0, sum = 0; i & 12; i ++) { if(sum & dates) { sum += month[i]; }else { } } //获取月份 m = //获取当月的天数 d = month[i - 1] - (sum - dates); //格式化输出 printf(&%04d-%02d-%02d\n&,year, m, d); } 11. 题目描述: 读入 N 名学生的成绩,将获得某一给定分数的学生人数输出。 输入: 测试输入包含若干测试用例,每个测试用例的格式为第 1 行:N 第 2 行:N 名学生的成绩,相邻两数字用一个空格间隔。 第 3 行:给定分数 当读到 N=0 时输入结束。其中 N 不超过 1000,成绩分数为(包含)0 到 100 之间的一个整 数。 输出: 对每个测试用例,将获得给定分数的学生人数输出。 样例输入: 3 80 60 90 60 2 85 66 0 5 60 75 90 55 75 75 0 样例输出: 1 0 2 #include &iostream& #include &cstdio& #include &cstring& double t[1005],a; int N; int main() { while(cin&&N && N) { int ans = 0; for(i = 0; i&N; i++) cin&&t[i]; cin&&a; for(i = 0; i&N; i++) if(t[i] == a)ans++; cout&&ans&& } return 0; }12. 题目描述: 输入一个 ip 地址串,判断是否合法。 输入: 输入的第一行包括一个整数 n(1&=n&=500),代表下面会出现的 IP 地址的个数。 接下来的 n 行每行有一个 IP 地址,IP 地址的形式为 a.b.c.d,其中 a、b、c、d 都是整数。 输出: 可能有多组测试数据,对于每组数据,如果 IP 地址合法则输出&Yes!” ,否则输出&No!” 。 样例输入: 2 255.255.255.255 512.12.2.3 样例输出: Yes! No! 提示: 合法的 IP 地址为: a、b、c、d 都是 0-255 的整数。 #include &iostream& int main(){ int p1,p2,p3,p4; while (cin&&n) { for(int i=0;i&n;i++) { cin&&p1&&c&&p2&&c&&p3&&c&&p4; if(p1&=0 && p2&=0 && p3&=0 && p4&=0 && p1&=255 && p2&=255 && p3&=255 && p4&=255){ cout&&&Yes!&&& } else{ cout&&&No!&&& } } } return 0; }13. 题目描述: 给你 n 个整数,请按从大到小的顺序输出其中前 m 大的数。 输入: 每组测试数据有两行,第一行有两个数 n,m(0&n,m&1000000),第二行包含 n 个各不相同, 且都处于区间[-000]的整数。 输出: 对每组测试数据按从大到小的顺序输出前 m 大的数。 样例输入: 53 3 -35 92 213 -644 样例输出: 213 92 3 #include &cstdio& #include &algorithm& #include &functional& #define left(i) ((i + 1) * 2 - 1) #define right(i) (left(i) + 1) #define parent(i) ((i + 1) / 2) void heapify(int a[], int n, int index) { while (true) { int i = int l = left(i), r = right(i); if (l & n) { i = a[i] & a[l] ? i : } if (r & n) { i = a[i] & a[r] ? i : } if (i != index) { std::swap(a[i], a[index]); index = } else { } } } void make_heap(int a[], int n) { for (int i = parent(n - 1); i &= 0; --i) { heapify(a, n, i); } } int main() { int n, while (scanf(&%d %d&, &n, &m) != EOF) { int *a = new int[m]; for (int i = 0; i & ++i) { scanf(&%d&, &a[i]); } make_heap(a, m); for (int i = i & ++i) { scanf(&%d&, &num); if (num & a[0]) { a[0] = heapify(a, m, 0); } } sort(a, a + m, greater&int&()); printf(&%d&, a[0]); for (int i = 1; i & ++i) { printf(& %d&, a[i]); } printf(&\n&); } return 0; }14.题目描述: “臭味相投” ――这是我们描述朋友时喜欢用的词汇。 两个人是朋友通常意味着他们存 在着许多共同的兴趣。然而作为一个宅男,你发现自己与他人相互了解的机会并不太多。幸 运的是,你意外得到了一份北大图书馆的图书借阅记录,于是你挑灯熬夜地编程,想从中发 现潜在的朋友。 首先你对借阅记录进行了一番整理,把 N 个读者依次编号为 1,2,?,N,把 M 本书依次 编号为 1,2,?,M。同时,按照“臭味相投”的原则,和你喜欢读同一本书的人,就是你的潜 在朋友。你现在的任务是从这份借阅记录中计算出每个人有几个潜在朋友。 输入: 每个案例第一行两个整数 N,M,2 &= N ,M&= 200。接下来有 N 行,第 i(i = 1,2,?,N)行 每一行有一个数,表示读者 i-1 最喜欢的图书的编号 P(1&=P&=M) 输出: 每个案例包括 N 行,每行一个数,第 i 行的数表示读者 i 有几个潜在朋友。如果 i 和任 何人都没有共同喜欢的书,则输出“BeiJu” (即悲剧,^ ^) 样例输入: 4 5 2 3 2 1 样例输出: 1 BeiJu 1 BeiJu #include &iostream& #include &set& int main(void) { multiset&int& int N, M; int ss[200]; while (cin && N && M) { for (int i=0; i & N; i++) { cin && ss[i]; record.insert(ss[i]); } for (int i=0; i & N; i++) { if (record.count(ss[i]) == 1) { cout && &BeiJu& && } else { cout && record.count(ss[i])-1 && } } record.clear(); } return 0; } 15. 题目描述: 把 M 个同样的苹果放在 N 个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的 分法?(用 K 表示)5,1,1 和 1,5,1 是同一种分法。 输入: 第一行是测试数据的数目 t(0 &= t &= 20) 。以下每行均包含二个整数 M 和 N,以空格分开。 1&=M,N&=10。 输出: 对输入的每组数据 M 和 N,用一行输出相应的 K。 样例输入: 1 73 样例输出: 8#include &iostream& int f(int m,int n) { if(n==1) return 1; else if(m==1||m==0) return 1; else if(m&0) return 0; else return f(m,n-1)+f(m-n,n); } int main() { int m,n; cin&&t; while(t--) { cin&&m&&n; cout&&f(m,n)&& } return 0; } 16. 有一个长度为整数 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 #include &stdio.h& #define MAXLEN 10001 int main(){ #ifdef ONLINE_JUDGE #else freopen(&E:\\in.txt&, &r&, stdin); #endif int L[MAXLEN]; int l, while (scanf (&%d%d&, &l, &m) != EOF){ for (int i = 0; i &= i++){ //注意 i 的范围是 i&=l not i&l L[i] = 1; }// 初始化,种 l+1 棵树 int a, for (int i = 1; i &= i++){ scanf(&%d%d&, &a, &b); for(int j = j &= j++){ L[j] = 0; }// 移走 }// m 组 int left=0; for (int i = 0; i &= i++){ if (L[i] == 1) left++; } printf(&%d\n&, left); }// while:zu return 0; } 17. 题目描述: 输入一个高度 h,输出一个高为 h,上底边为 h 的梯形。 输入: 一个整数 h(1&=h&=1000)。 输出: h 所对应的梯形。 样例输入: 4 样例输出: **** ****** ******** ********** #include&stdio.h& #include&string.h& int main(void) { while(scanf(&%d&,&h) != EOF) { int i,j; for(i = 1; i &= ++i) { for(j = 1; j &= 2 * h - 2 * ++j) printf(& &); for(j = 2 * h - 2 * i + 1; j &= 3 * h - 2; ++j) printf(&*&); printf(&\n&); } } return 0; } 18. 题目描述: 把一个个大小差一圈的筐叠上去,使得从上往下看时,边筐花色交错。这个工作现在要让计 算机来完成,得看你的了。 输入: 输入是一个个的三元组,分别是,外筐尺寸 n(n 为满足 0&n&80 的奇整数) ,中心花色字符, 外筐花色字符,后二者都为 ASCII 可见字符; 输出: 输出叠在一起的筐图案,中心花色与外筐花色字符从内层起交错相叠,多筐相叠时,最外筐 的角总是被打磨掉。叠筐与叠筐之间应有一行间隔。 样例输入: 11 B A 5@W 样例输出: AAAAAAAAA ABBBBBBBBBA ABAAAAAAABA ABABBBBBABA ABABAAABABA ABABABABABA ABABAAABABA ABABBBBBABA ABAAAAAAABA ABBBBBBBBBA AAAAAAAAA #include &stdio.h& #include &malloc.h& int main(){ char pic[80][80]; char first, while(scanf(&%d %c %c&, &n, &first, &second) != EOF){ int min, max, floor = 0; for(min = max = n / 2 ;min&=0&&max&n; min--, max++, floor ++) { char c = floor%2==0? first: for(int j = j&= j++){ pic[min][j] =c; pic[max][j] =c; pic[j][max] =c; pic[j][min] =c; } } if(n & 1) pic[0][0] = pic[0][n-1] = pic[n-1][0] =pic[n-1][n -1] = ' '; //print pattern for(int i = 0; i& i ++){ for(int j = 0 ; j & j++){ printf(&%c&, pic[i][j]); } printf(&\n&); } printf(&\n&); } } 19. 题目描述: 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. ## # &-template ## So the Level 1 picture will be ## # ## Level 2 picture will be ## ## # # ## ## ## # ## ## ## # # ## ## 输入: 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 . 输出: 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 &stdio.h& #include &algorithm& #include &string& #include &string.h& #include &functional& #include &vector& #include &cmath& void shape_str2char(string src[] , char* dest[], int x, int y, int n){ for(int i=0; i&n; i++) for(int j=0; j&n; j++) dest[x+i][y+j]=src[i].at(j); } void shape_char2char(char* src[], char* dest[], int x, int y, int n){ for(int i=0; i&n; i++)## ## # ## for(int j=0; j&n; j++) dest[x+i][y+j]=src[i][j]; } void shape_recur(int depth, char* templ[], char* dest[], int x, int y, int size){ if(depth==1) shape_char2char(templ, dest, x, y, size); else{ for(int i=0; i& i++) for(int j=0; j& j++) if(templ[i][j]!=' ') shape_recur(depth-1, x+i*pow(double(size), depth-1), y+j*pow(double(size), depth-1), size); } } int main(){ int size_ while(scanf(&%d&, &size_templ) && size_templ!=0){ //sp1 while(getchar() != '\n') char* str[6]; for(int i=0; i&6; i++) str[i] = new char[6]; for(int i=0; i&size_ i++) gets(str[i]); //sp2 char* templ[6]; for(int i=0; i&6; i++) templ[i] = new char[6]; shape_char2char(str, templ, 0, 0, size_templ); //sp3 scanf(&%d&, &num); int size_pic = pow((double)size_templ, num); char *Pic[3001]; for(int i=0; i&3001; i++) Pic[i] = new char[3001]; for(int i=0; i&3001; i++) for(int j=0; j&3001; j++) Pic[i][j]=' '; shape_recur(num, templ, Pic, 0, 0, size_templ); //sp4 for(int i=0; i&size_ i++){ for(int j=0; j&size_ j++) printf(&%c&, Pic[i][j]);templ,dest, printf(&\n&); } for(int i=0; i&6; i++) delete(str[i]); for(int i=0; i&3001; i++) delete(Pic[i]); } } 20. 题目描述: 输入一个数 n,然后输入 n 个数值各不相同,再输入一个值 x,输出这个值在这个数组中的 下标(从 0 开始,若不在数组中则输出-1) 。 输入: 测试数据有多组,输入 n(1&=n&=200),接着输入 n 个数,然后输入 x。 输出: 对于每组输入,请输出结果。 样例输入: 2 13 0 样例输出: -1 #include &stdio.h& int main() { while (scanf(&%d&,&n)!=EOF) { int x,a[200],fla=0; for (int i=0;i&n;i++) { scanf(&%d&,&a[i]); } scanf(&%d&,&x); for (int i=0;i&n;i++) { if (a[i]==x) { printf(&%d\n&,i); fla=1; } } if (fla==0) { printf(&-1\n&); } } return 0; } 21. 题目描述: 输入 N 个学生的信息,然后进行查询。 输入: 输入的第一行为 N,即学生的个数(N&=1000) 接下来的 N 行包括 N 个学生的信息,信息格式如下: 01 李江 男 21 02 刘唐 男 23 03 张军 男 19 04 王娜 女 19 然后输入一个 M(M&=10000),接下来会有 M 行,代表 M 次查询,每行输入一个学号,格式 如下: 02 03 01 04 输出: 输出 M 行,每行包括一个对应于查询的学生的信息。 如果没有对应的学生信息,则输出“No Answer!” 样例输入: 4 01 02 03 04 5 02 03 01 04 03 李江 刘唐 张军 王娜 男 男 男 女 21 23 19 19样例输出: 02 刘唐 男 23 03 张军 男 19 01 李江 男 21 04 王娜 女 19 03 张军 男 19 #include&iostream& #include&fstream& #include&algorithm& #include&string& #include&map& int main() { string id,info, int i,index,n,M; map&string,string& map&string,string&:: fstream cin(&1069.txt&); while (cin&&n) { cin.ignore(); m.clear(); for (i=0;i&n;i++) { getline(cin,str); index=str.find(& &); id=str.substr(0,index); info=str.substr(index+1); m.insert(pair&string,string&(id,info)); } cin&&M; for (i=0;i&M;i++) { cin&& iter=m.find(id); if (iter!=m.end()) { cout&&iter-&first&&& &&&iter-&second&& } else { cout&&&No Answer!&&& } } } return 0;//} 22. 题目描述: 在一个整数数组上,对于下标为 i 的整数,如果它大于所有它相邻的整数, 或者小于所有它相邻的整数,则称为该整数为一个极值点,极值点的下标就是 i。 输入: 每个案例的输入如下: 有 2×n+1 行输入:第一行是要处理的数组的个数 n; 对其余 2×n 行,第一行是此数组的元素个数 k(4&k&80),第二行是 k 个整数,每两个整数之 间用空格分隔。 输出: 每个案例输出为 n 行:每行对应于相应数组的所有极值点下标值,下标值之间用空格分隔。 样例输入: 3 10 10 12 12 11 11 12 23 24 12 12 15 12 12 122 112 222 211 222 221 76 36 31 234 256 76 76 15 12 14 122 112 222 222 222 221 76 36 31 234 256 76 73 样例输出: 07 2 3 4 5 6 10 12 0 2 3 10 12 14 #include&iostream& #include&vector& int main() { while(cin && n) { while(n--) { int k,i; cin && vector&int& vv(k); for(i = 0;i &i++) cin && vv[i]; vector&int& out.reserve(k); if(vv[0] & vv[1]||vv[0] & vv[1]) out.push_back(0); for(i = 1;i &= k-2;i++) { if(vv[i] & vv[i-1]&&vv[i] & vv[i+1]) out.push_back(i); if(vv[i] & vv[i-1]&&vv[i] & vv[i+1]) out.push_back(i); } if(vv[k-1] & vv[k-2]||vv[k-1] & vv[k-2]) out.push_back(k-1); for(i = 0;i & out.size() - 1;i++) cout && out[i] && & &; cout && out[i] && } } return 0; } 23. 题目描述: 输入数组长度 n 输入数组 a[1...n] 输入查找个数 m 输入查找数字 b[1...m] 输出 YES or NO 查找有则 YES 否则 NO 。 输入: 输入有多组数据。 每组输入 n,然后输入 n 个整数,再输入 m,然后再输入 m 个整数(1&=m&=n&=100) 。 输出: 如果在 n 个数组中输出 YES 否则输出 NO。 样例输入: 5
样例输出: YES YES NO #include&iostream& #include&vector& #include&algorithm& int main() { int temp,i,n,m; vector&int& while (cin&&n) { for (i=0;i&n;i++) { cin&& v.push_back(temp); } cin&&m; for (i=0;i&m;i++) { cin&& vector&int&::iterator index=find(v.begin(),v.end(),temp);// 刚 学 的 iterator 迭代器用法,并使用系统提供的 find 方法 if (index==v.end()) { cout&&&NO&&& } else { cout&&&YES&&& } } } return 0; } 23. 题目描述: FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain. 输入: The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000. 输出: For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain. 样例输入: 53 72 43 52 20 3 25 18 24 15 15 10 -1 -1 样例输出: 13.333 31.500 #include &cstdio& #include &vector& #include &algorithm& struct Room { }; struct Cmp { bool operator()(const Room &a, const Room &b) { return a.ratio & b. } }; int main() { C int food, while (scanf(&%d %d&, &food, &n) != EOF && food != -1) { vector&Room& for (int i = 0; i & ++i) { R scanf(&%d %d&, &room.beans, &room.cost); room.ratio = room.beans / (double)room. rooms.push_back(room); } sort(rooms.begin(), rooms.end(), cmp); int index = 0; double beans = 0.0; while (food && index & n) { Room &room = rooms[index]; if (food &= room.cost) { food -= room. beans += room. ++ } else { double ratio = food / (double)room. beans += room.beans * } } printf(&%.3f\n&, beans); } return 0; } 24. 题目描述: “今年暑假不 AC?” “是的。 ” “那你干什么呢?” “看世界杯呀,笨蛋! ” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多 ACMer 也会抛开电脑,奔向电视作 为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其 它的节目,比如新闻联播(永远不要忘记关心国家大事) 、非常 6+7、超级女生,以及王小 丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合 理安排吗?(目标是能看尽量多的完整节目) 输入: 输入数据包含多个测试实例,每个测试实例的第一行只有一个整数 n(n&=100),表示你喜欢 看的节目的总数,然后是 n 行数据,每行包括两个数据 Ti_s,Ti_e (1&=i&=n),分别表示第 i 个 节目的开始和结束时间, 为了简化问题, 每个时间都用一个正整数表示。 n=0 表示输入结束, 不做处理。 输出: 对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。 样例输入: 12 13 34 07 38 15 19 15 20 10 15 8 18 6 12 5 10 4 14 29 0 样例输出: 5 #include &cstdio& #include &algorithm& struct ss{ int st, }node[102]; bool cmp(ss x,ss y){ if(x.ed!=y.ed) return x.ed&y. else return x.st&y. } int main(){ while(scanf(&%d&,&n)!=EOF){ if(n==0) return 0; else{ for(int i=0;i&n;++i){ scanf(&%d %d&,&node[i].st,&node[i].ed); } sort(node,node+n,cmp); int num=1,k=0; for(int j=0;j&n;j++){ if(node[j].st&=node[k].ed){ num++; k=j; } } printf(&%d\n&,num); } } } 24. 题目描述: 通过悬崖的 yifenfei,又面临着幽谷的考验―― 幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅。由于此处长年不见天日,导致空 气中布满了毒素,一旦吸入体内,便会全身溃烂而死。 幸好 yifenfei 早有防备,提前备好了解药材料(各种浓度的万能药水) 。现在只需按照配置成 不同比例的浓度。 现已知 yifenfei 随身携带有 n 种浓度的万能药水,体积 V 都相同,浓度则分别为 Pi%。并且 知道,针对当时幽谷的瘴气情况,只需选择部分或者全部的万能药水,然后配置出浓度不大 于 W%的药水即可解毒。 现在的问题是:如何配置此药,能得到最大体积的当前可用的解药呢? 特别说明:由于幽谷内设备的限制,只允许把一种已有的药全部混入另一种之中(即:不能 出现对一种药只取它的一部分这样的操作) 。 输入: 输入数据的第一行是一个整数 C,表示测试数据的组数; 每组测试数据包含 2 行,首先一行给出三个正整数 n,V,W(1&=n,V,W&=100); 接着一行是 n 个整数,表示 n 种药水的浓度 Pi%(1&=Pi&=100)。 输出: 对于每组测试数据,请输出一个整数和一个浮点数; 其中整数表示解药的最大体积,浮点数表示解药的浓度(四舍五入保留 2 位小数); 如果不能配出满足要求的的解药,则请输出 0 0.00。 样例输入: 3 1 100 10 100 2 100 24 20 30 3 100 24 20 20 30 样例输出: 0 0.00 100 0.20 300 0.23 #include &cstdio& #include &cstring& #include &algorithm& #include &iostream& #include &cmath& int main() { cin && for(;c&0;c--) { int n,v,w; cin && n && v && int buf[100]; for(int i=0;i&n;i++) cin && buf[i]; sort(buf,buf+n);//解药按浓度排序 int currW = 0; int currV = 0; for(int i=0;i&n;i++) { if(buf[i] &= w) { currW = (buf[i]*v + currW); currV += } else if((buf[i]*v + currW) &= w*(currV+v)) { currW = currW + buf[i]*v; currV += } } cout && currV && & 0.&; currW = floor((double)currW/currV + 0.5); if(currV == 0)currW = 0; printf(&%02d&,currW); cout && } //system(&pause&); } 25. 题目描述: Long time ago , Kitty lived in a small village. The air was fresh and the scenery was very beautiful. The only thing that troubled her is the typhoon. When the typhoon came, everything is terrible. It kept blowing and raining for a long time. And what made the situation worse was that all of Kitty's walls were made of wood. One day, Kitty found that there was a crack in the wall. The shape of the crack is a rectangle with the size of 1×L (in inch). Luckly Kitty got N blocks and a saw(锯子) from her neighbors. The shape of the blocks were rectangle too, and the width of all blocks were 1 inch. So, with the help of saw, Kitty could cut down some of the blocks(of course she could use it directly without cutting) and put them in the crack, and the wall may be repaired perfectly, without any gap. Now, Kitty knew the size of each blocks, and wanted to use as fewer as possible of the blocks to repair the wall, could you help her ? 输入: The problem contains many test cases, please process to the end of file( EOF ). Each test case contains two lines. In the first line, there are two integers L(0&L&) and N(0&=N&600) which mentioned above. In the second line, there are N positive integers. The ith integer Ai(0&Ai& ) means that the ith block has the size of 1×Ai (in inch). 输出: For each test case , print an integer which represents the minimal number of blocks are needed. If Kitty could not repair the wall, just print &impossible& instead. 样例输入: 53 321 52 21 样例输出: 2 Impossible #include&stdio.h& #include&algorithm& bool cmp(int a,int b) { return a&b; } int main() { int i,N; long long L,A[601]; while(scanf(&%lld%lld&,&L,&N)!=EOF) { for(i=0;i&N;i++) { scanf(&%lld&,&A[i]); } sort(A,A+N,cmp); bool flag= int sum=0,k=0; for(i=0;i&N;i++) { if((sum+A[i])&L) { k++; sum+=A[i]; } else { k++; flag= } } if(flag) { printf(&impossible\n&); } else { printf(&%d\n&,k); } } return 0; } 26. 题目描述: With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go. 输入: For each case, the first line contains 4 positive numbers: Cmax (&= 100), the maximum
D (&=30000), the distance between Hangzhou and
Davg (&=20), the average distance per unit gas
and N (&= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (&=D), the distance between this station and Hangzhou, for i=1,...N. All the numbers in a line are separated by a space. 输出: For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print &The maximum travel distance = X& where X is the maximum possible distance the car can run, accurate up to 2 decimal places. 样例输入: 50
600 7.00 150 7.10 0 7.20 200 7.50 400 7.30
7.10 0 7.00 600 样例输出: 749.17 The maximum travel distance = 1200.00 #include &stdio.h& #include &stdlib.h& #include &math.h& #define MAXN 501 #define MAXC
typedef struct station{
}S int compare(const void * p, const void * q){ Station * p1 = (Station *)p; Station * q1 = (Station *)q; return p1-&dist - q1-& } int main(void){ int Cmax, D, Davg, N; //??????? à????????? ?? ?? ? ????? à????? ? ? ?? ? ×? ?? Station sta[MAXN]; float sum, remind_gas, int k, while (scanf(&%d %d %d %d&, &Cmax, &D, &Davg, &N) != EOF){ for (i=0; i&N; ++i){ scanf(&%f %d&, &sta[i].price, &sta[i].dist); } sta[N].dist = D; sta[N].price = ; qsort(sta, N, sizeof(Station), compare); //°? ? ??? ? ?? à??? ó? ?? ?? ? ? ?? ? ??? ò if (sta[0].dist & 0){ printf (&The maximum travel distance = 0.00\n&); } sum = 0; //×? ? ??? step = Cmax*D //? ? ?ú? ? ? ? ??×? ? ó? à?? remind_gas = 0; //??? à? ? ?? for (i=0; i&N; ++i){ k = i+1; if (i != 0) remind_gas -= ((float)(sta[i].dist -sta[i-1].dist))/D for (; k&N && sta[k].price&=sta[i]. ++k) if (sta[k].dist-sta[i].dist & step){ sum += (Cmax-remind_gas)*sta[i]. remind_gas = C } else{ tmp = ((float)(sta[k].dist-sta[i].dist))/Davg - remind_ if (fabs(tmp)&1e-5 && tmp&0){ sum += tmp*sta[i]. remind_gas = ((float)(sta[k].dist-sta[i].dist))/D } } if (sta[i+1].dist - sta[i].dist & step){ printf (&The maximum travel distance = %.2f\n&, (float)(sta[i].dist+step)); } } if (i == N){ printf (&%.2f\n&, sum); } } return 0; } 27. 题目描述: 在某个字符串(长度不超过 100)中有左括号、右括号和大小写字母;规定(与常见的 算数式子一样) 任何一个左括号都从内到外与在它右边且距离最近的右括号匹配。 写一个程 序,找到无法匹配的左括号和右括号,输出原来字符串,并在下一行标出不能匹配的括号。 不能匹配的左括号用&$&标注,不能匹配的右括号用&?&标注. 输入: 输入包括多组数据,每组数据一行,包含一个字符串,只包含左右括号和大小写字母, 字符串长度不超过 100。 注意:cin.getline(str,100)最多只能输入 99 个字符! 输出: 对每组输出数据,输出两行,第一行包含原始输入字符,第二行由&$&,&?&和空格组成, &$&和&?&表示与之对应的左括号和右括号不能匹配。 样例输入: )(rttyy())sss)( 样例输出: )(rttyy())sss)( ? ?$ #include&stdio.h& #include&string.h& #include&stdlib.h& #define MAX_LEN 110 int main(void) { char *exp=(char*)malloc(sizeof(char)*MAX_LEN); int *res=(int*)malloc(sizeof(int)*MAX_LEN); while(gets(exp)) { int cur=0,pt=0,len=strlen(exp); for(int i=0;i!=MAX_LEN;++i) res[i]=-1; for(;cur!=++cur) { if(exp[cur]!='('&&exp[cur]!=')') res[cur]=0; if(exp[cur]==')') { for(pt=cur-1;pt&=0;--pt) { if(exp[pt]=='('&&res[pt]) { res[pt]=0; res[cur]=0; } } } } puts(exp); for(int i=0;i!=++i) { if(exp[i]=='('&&res[i]==-1) res[i]=1; else if(exp[i]==')'&&res[i]==-1) res[i]=2; switch(res[i]) { case 0: putchar(' '); case 1: putchar('$'); case 2: putchar('?'); } } printf(&\n&); } return 0; } 28. 题目描述: 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。 输入: 测试输入包含若干测试用例,每个测试用例占一行,每行不超过 200 个字符,整数和运 算符之间用一个空格分隔。没有非法表达式。当一行中只有 0 时输入结束,相应的结果不要 输出。 输出: 对每个测试用例输出 1 行,即该表达式的值,精确到小数点后 2 位。 样例输入: 1+2 4 + 2 * 5 - 7 / 11 0 样例输出: 3.00 13.36 #include &stdio.h& #include &string.h& #include &stdlib.h& #define size 500 int isfu(char a){ return (a=='*' || '-'==a || '+'==a || '/'==a); } int isdig(char a){ return a&='0' && a &= '9'; } int mygets(char *s, int *len) { (*len) = 0; while ('\n' != (ch=getchar()))/*输入处理*/ { if (isfu(ch)||isdig(ch)) s[(*len)++] = } s[*len] = 0; if ((*len) == 1 && s[0] == '0') return 0; return 1; } main() { char str[size], fu[size]; int i, len, nd, nf, total, double dig[size]; while (mygets(str,&len)) { if (len == 0) nd = nf = now = 0; for (i = 0; i & ) { if (isfu(str[i])) { fu[nf++] = str[i]; if ('*' == str[i] || '/' == str[i]) now = 1;/*得到下一个数后马上计算*/ i++; } else { total = 0; while (str[i] &= '0' && str[i] &= '9') { total *= 10; total += (str[i] - '0') ; i++; } dig[nd++] = (double) if (now) {/*立马算*/ nf--, nd--; if ('*' == fu[nf]) dig[nd-1] = dig[nd-1] * dig[nd]; else if ('/' == fu[nf]) dig[nd-1] = dig[nd-1] / dig[nd]; now = 0; } } } i = 0; while (i&nd-1)/*只剩加减法*/ { if ('-' == fu[i]) dig[i+1] = dig[i] - dig[i+1]; else if ('+' == fu[i]) dig[i+1] += dig[i]; i++; } printf(&%.2lf\n&,dig[nd-1]); } } 29. 题目描述: 堆栈是一种基本的数据结构。堆栈具有两种基本操作方式,push 和 pop。Push 一个值 会将其压入栈顶,而 pop 则会将栈顶的值弹出。现在我们就来验证一下堆栈的使用。 输入: 对于每组测试数据,第一行是一个正整数 n,0&n&=10000(n=0 结束)。而后的 n 行, 每行的第一个字符可能是'P’或者'O’或者'A’ ;如果是'P’ ,后面还会跟着一个整数,表示 把这个数据压入堆栈;如果是'O’ ,表示将栈顶的值 pop 出来,如果堆栈中没有元素时,忽 略本次操作;如果是'A’ ,表示询问当前栈顶的值,如果当时栈为空,则输出'E'。堆栈开始 为空。 输出: 对于每组测试数据,根据其中的命令字符来处理堆栈;并对所有的'A’操作,输出当时 栈顶的值,每个占据一行,如果当时栈为空,则输出'E’ 。当每组测试数据完成后,输出一 个空行。 样例输入: 3 A P5 A 4 P3 P6 O A 0 样例输出: E 5 3 #include&iostream& #include&fstream& #include&vector& #include&stack& int main() { //int i,n, vector&int& stack&int& fstream cin(&1108.txt&); while (cin&&n,n) { v.clear(); for (i=0;i&n;i++) { cin&& switch(ch) { case 'A': if (v.empty()) { cout&&&E&&& } else { cout&&v.back()&& } case 'P': { cin&& v.push_back(temp); } case 'O': { if (!v.empty()) { v.pop_back(); } } } } cout&& } return 0;} 30. 题目描述: 对于一个不存在括号的表达式进行计算 输入: 存在多种数据,每组数据一行,表达式不存在空格 输出: 输出结果 样例输入: 6/2+3+3*4 样例输出: 18 #include &iostream& #include &stack& double getNum(){ double v = 0; for(;pos&str.length();pos++){ if(str[pos]&'9'||str[pos]&'0'){ } v *= 10; v += str[pos] - '0'; } return 1.0*v; } int main(int argc,char* argv[]){ double a,b; while(cin&&str){ stack&double& pos = 0; s.push(getNum()); while(pos&str.length()){ if(str[pos]=='*'){ pos++; a = s.top(); s.pop(); b = getNum(); s.push(a*b); } if(str[pos]=='/'){ pos++; a = s.top(); s.pop(); b = getNum(); s.push(a/b); } if(str[pos]=='+'){ pos++; s.push(getNum()); } if(str[pos]=='-'){ pos++; s.push(-1.0*getNum()); } } v = 0; while(!s.empty()){ v += s.top(); s.pop(); } cout&&v&& } return 0; } 31. 题目描述: 哈夫曼树,第一行输入一个数 n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根 据哈夫曼树的概念,这些结点有权值,即 weight,题目需要输出所有结点的值与权值的乘积 之和。 输入: 输入有多组数据。 每组第一行输入一个数 n,接着输入 n 个叶节点(叶节点权值不超过 100,2&=n&=1000) 。 输出: 输出权值。 样例输入: 5 12259 样例输出: 37 #include &iostream& #include &algorithm& int main() {
while(cin&&n){ int sum=0; int a[n]; for(int i=0;i&n;i++) cin&&a[i]; for(int i=1;i&n;i++) { sort(a+i-1,a+n); sum=a[i]+a[i-1]+ a[i]=a[i]+a[i-1]; } cout&&sum&& } return 0; } 32. 题目描述: 在一个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆, 小明决定把所有的水果合成一堆。每一次合并,小明可以把两堆水果合并到一起,消耗的体 力等于两堆水果的重量之和。当然经过 n\1 次合并之后,就变成一堆了。小明在合并水果 时总共消耗的体力等于每次合并所耗体力之和。 假定每个水果重量都为 1, 并且已知水果的种类数和每种水果的数目, 你的任务是设计 出合并的次序方案,使小明耗费的体力最少,并输出这个最小的体力耗费值。例如有 3 种 水果,数目依次为 1,2,9。可以先将 1,2 堆合并,新堆数目为 3,耗费体力为 3。然后 将新堆与原先的第三堆合并得到新的堆, 耗费体力为 12。 所以小明总共耗费体力=3+12=15, 可以证明 15 为最小的体力耗费值。 输入: 每组数据输入包括两行,第一行是一个整数 n(1&=n&=10000),表示水果的种类数,如果 n 等于 0 表示输入结束,且不用处理。第二行包含 n 个整数,用空格分隔,第 i 个整数 (1&=ai&=1000)是第 i 种水果的数目。 输出: 对于每组输入,输出一个整数并换行,这个值也就是最小的体力耗费值。输入数据保证这个 值小于 2^31。 样例输入: 3 912 0 样例输出: 15 #include&iostream& int main(int argc,char* argv[]) {
int a[10000]; int i,j; int t,k; while(cin&&n&&n!=0) { sum=0; for(i=0;i&n;i++) { cin&&a[i]; } t=n-1; while(t&0) { k=0; for(i=0;i&=t;i++) if(a[k]&a[i]) k=i; temp=a[t]; a[t]=a[k]; a[k]= k=0; for(i=0;i&=t-1;i++) if(a[k]&a[i]) k=i; temp=a[t-1]; a[t-1]=a[k]; a[k]= a[t-1]+=a[t]; t--; sum+=a[t]; } cout&&sum&& } return 0; } 33. 题目描述: 二叉树的前序、中序、后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树; //a[t-1]为第二个最小值 //a[t]为第一个最小值 中序遍历:对任一子树,先遍历其左子树,然后访问根,最后遍历其右子树; 后序遍历:对任一子树,先遍历其左子树,然后遍历其右子树,最后访问根。 给定一棵二叉树的前序遍历和中序遍历,求其后序遍历(提示:给定前序遍历与中序遍历能 够唯一确定后序遍历) 。 输入: 两个字符串,其长度 n 均小于等于 26。 第一行为前序遍历,第二行为中序遍历。 二叉树中的结点名称以大写字母表示:A,B,C....最多 26 个结点。 输出: 输入样例可能有多组,对于每组测试样例, 输出一行,为后序遍历的字符串。 样例输入: ABC BAC FDXEAG XDEFAG 样例输出: BCA XEDGAF #include &stdio.h& #include &string.h& #include &stdlib.h& char s1[101],s2[101]; int a[101]; void fen(char *s2,int l,int h); main() { int i,j; while( scanf(&%s %s&,s1,s2) != EOF){ k = -1; for(i = 0;i & strlen(s1);i++){ for(j = 0;j & strlen(s2);j++){ if(s1[i] == s2[j]) a[i] = } } fen(s2,0,strlen(s2) - 1); printf(&\n&); } } void fen(char *s2,int l,int h){ if(h & l) if(h - l & 0) k++; n = a[k]; if(l == h) { printf(&%c&,s2[h]); k++; } fen(s2,l,n-1); fen(s2,n+1,h); printf(&%c&,s2[n]); } 34. 题目描述:如上所示,由正整数 1,2,3??组成了一颗特殊二叉树。我们已知这个二叉树的最后 一个结点是 n。现在的问题是,结点 m 所在的子树中一共包括多少个结点。 比如,n = 12,m = 3 那么上图中的结点 13,14,15 以及后面的结点都是不存在的,结 点 m 所在子树中包括的结点有 3,6,7,12,因此结点 m 的所在子树中共有 4 个结点。 输入: 输入数据包括多行,每行给出一组测试数据,包括两个整数 m,n (1 &= m &= n &= )。最后一组测试数据中包括两个 0,表示输入的结束,这组数据不用处理。 输出: 对于每一组测试数据,输出一行,该行包含一个整数,给出结点 m 所在子树中包括的 结点的数目。 样例输入: 3 12 00 样例输出: 4 #include &math.h& #include &stdio.h& int main() { unsigned long n,sum=0,d; while(scanf(&%d&,&d)) { scanf(&%d&,&n); if (d==0&&n==0) ddeep=(unsigned long)(log(d)/log(2)+1); unsigned long d1,d2; d1=(unsigned long)(log(n)/log(2)+1); if(d&n) { printf(&0\n&); } else { unsigned long j,k; k=d; unsigned long count=0; for(i= i&d1-1; i++) { count++; k=2*k+1; for(j=d*pow(2,count);j&=k;j++) { sum++; } } count++; k=2*k+1; for (j=d*pow(2,count);j&=k&&j&=n;j++) { sum++; } } printf(&%d\n&,++sum); sum=0; } return 0; } 35. 题目描述: 有一棵树,输出某一深度的所有节点,有则输出这些节点,无则输出 EMPTY。该树是完全 二叉树。 输入: 输入有多组数据。 每组输入一个 n(1&=n&=1000), 然后将树中的这 n 个节点依次输入, 再输入一个 d 代表深度。 输出: 输出该树中第 d 层得所有节点,节点间用空格隔开,最后一个节点后没有空格。 样例输入: 4 1234 2 样例输出: 23 #include&iostream& #include&algorithm& #include&string& #include&vector& #include&cmath& #include&fstream& int main() { int i,n,d,temp,sum1,sum2; vector&int& fstream cin(&1176.txt&); while (cin&&n) { v.clear(); for (i=0;i&n;i++) { cin&& v.push_back(temp); } cin&&d;// sum1=pow(2,d-1)-1; sum2=pow(2,d)-1; if (sum1&=n) { cout&&&EMPTY&&& } else if (n&sum1&&n&sum2) { cout&&v.at(sum1); for (i=sum1+1;i&n;i++) { cout&&& &&&v.at(i); } } else if (n&=sum2) { cout&&v.at(sum1); for (i=sum1+1;i&sum2;i++) { cout&&& &&&v.at(i); } } cout&& }return 0; } 36. 题目描述: 输入一系列整数,建立二叉排序数,并进行前序,中序,后序遍历。 输入: 输入第一行包括一个整数 n(1&=n&=100)。 接下来的一行包括 n 个整数。 输出: 可能有多组测试数据,对于每组数据,将题目所给数据建立一个二叉排序树,并对二叉 排序树进行前序、中序和后序遍历。 每种遍历结果输出一行。每行最后一个数据之后有一个空格。 样例输入: 5 16598 样例输出:
58961 #include &iostream& typedef struct Node { struct Node * struct Node * }BTree,*B//前序遍历 void PreOrder(Blink head) { if(head) { cout && head-&data && & &; PreOrder(head-&leftchild); PreOrder(head-&rightchild); } } //中序遍历 void InOrder(Blink head) { if(head) { InOrder(head-&leftchild); cout && head-&data && & &; InOrder(head-&rightchild); } } //后序遍历 void PostOrder(Blink head) { if(head) { PostOrder(head-&leftchild); PostOrder(head-&rightchild); cout && head-&data && & &; } } //撤销二叉树 void deleteTree(Blink &head) { if(head) { deleteTree(head-&leftchild); deleteTree(head-&rightchild); } } //建立二叉排序树 void buildBinarySortTree(Blink &head, int elem[], int length) { Blink p, int flagLeftOrR head = new BT head-&data = elem[0]; head-&leftchild = NULL; head-&rightchild = NULL; //0 表示左孩子,1 表示右孩子//生成头结点for(int i = 1; i & ++i) { success = 0; pre = NULL; p = while(p) //寻找插入的位置 { pre = if(p-&data == elem[i]) //查找成功 { success = 1; } else if(p-&data & elem[i]) //沿右孩子搜索 { p = p-& flagLeftOrRight = 1; } else //沿左孩子搜索 { p = p-& flagLeftOrRight = 0; } } if(!success) { p = new BT p-&data = elem[i]; p-&leftchild = NULL; p-&rightchild = NULL; //生成新结点if(flagLeftOrRight == 0) //新插入的结点作为左孩子 { pre-&leftchild = } else if(flagLeftOrRight == 1) //新插入的结点作为右孩子 { pre-&rightchild = } } } } int main() { int N; int elem[110]; Bwhile(cin && N) { for(int i = 0; i & N; ++i) { cin && elem[i]; } buildBinarySortTree(head,elem,N); PreOrder(head); cout && InOrder(head); cout && PostOrder(head); cout && } return 0; } 37. 题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数 n,(1&=n&=20) 表示有 n 个需要判断,n= 0 的时候输入结束。 接下去一行是一个序列,序列长度小于 10,包含(0~9)的数字,没有重复数字,根据这个序 列可以构造出一颗二叉搜索树。 接下去的 n 行有 n 个序列, 每个序列格式跟第一个序列一样, 请判断这两个序列是否能组成 同一颗二叉搜索树。 输出: 如果序列相同则输出 YES,否则输出 NO 样例输入: 2 267
样例输出: YES NO #include&iostream& int a[1024]; int b[1024]; void createtree(string s,int c[]) { int len=s.length(); for(int i=0;i&i++) { int temp=s[i]-'0'; for(int j=1;j&=1023;) { if(c[j]==-1) {c[j]=} else if(c[j]&temp) j=j*2; else j=j*2+1; } } } int main() { int n,i; while(cin&&n&&n) { for(i=0;i&1024;i++) a[i]=-1; cin&&s; createtree(s,a); while(n--) { for(i=0;i&1024;i++) b[i]=-1; cin&&t; createtree(t,b); for(i=0;i&1024;i++) if(a[i]!=b[i]) if(i==1024) cout&&&YES&&& else cout&&&NO&&& } } return 0; } 38. 题目描述: 读入两个小于 10000 的正整数 A 和 B,计算 A+B。需要注意的是:如果 A 和 B 的末尾 K(不 超过 8)位数字相同,请直接输出-1。 输入: 测试输入包含若干测试用例,每个测试用例占一行,格式为&A B K&,相邻两数字有一个空格 间隔。当 A 和 B 同时为 0 时输入结束,相应的结果不要输出。 输出: 对每个测试用例输出 1 行,即 A+B 的值或者是-1。 样例输入: 121 11 21 1 108 8 2 36 64 3 001 样例输出: 3 -1 -1 100 #include&iostream& #include&string& #include&fstream& #include&algorithm& void split(int num,int k,char *str) { for (i=0;i&k;i++) { str[i]=num%10; num/=10; } str[i]='\0'; } int main() { int a,b,k; char cha[10],chb[10]; //fstream cin(&1015.txt&); while (cin&&a&&b,a+b) { cin&&k; split(a,k,cha); split(b,k,chb); string stra(cha); string strb(chb); if (stra==strb) { cout&&&-1&&& } else { cout&&a+b&& } } return 0; } 39. 题目描述: 守形数是这样一种整数,它的平方的低位部分等于它本身。 比如 25 的平方是 625,低位部分是 25,因此 25 是一个守形数。 编一个程序,判断 N 是否为守形数。 输入: 输入包括 1 个整数 N,2&=N&100。 输出: 可能有多组测试数据,对于每组数据, 输出&Yes!”表示 N 是守形数。 输出&No!”表示 N 不是守形数。 样例输入: 25 4 样例输出: Yes! No! #include&stdio.h& #include&stdlib.h& #include&string.h& int n, int main() { while(~scanf(&%d&, &n)) { t = n * while(n & 0) { if((n % 10) != (t % 10)) n /= 10; t /= 10; } if(!n) puts(&Yes!&); else puts(&No!&); } return 0; } 40. 题目描述: 写个算法,对 2 个小于
的输入,求结果。 特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5 输入: 两个小于
的数 输出: 输入可能有多组数据,对于每一组数据,输出 Input 中的两个数按照题目要求的方法进行 运算后得到的结果。 样例输入: 123 45 样例输出: 54 #include &iostream& #include &string& int main() { string str1,str2; while(cin&&str1&&str2) { int sum=0; for(int i=0;i&str1.size();i++) for(int j=0;j&str2.size();j++) { sum+=(str1[i]-48)*(str2[j]-48); } cout&&sum&& } return 0; } 41. 题目描述: 设 N 是一个四位数,它的 9 倍恰好是其反序数(例如:1234 的反序数是 4321) 求 N 的值 输入: 程序无任何输入数据 输出: 输出题目要求的四位数,如果结果有多组,则每组结果之间以回车隔开 样例输入: 样例输出: #include &cstdio& #include &iostream& // 将一个数反转 int GetReverseNum(int a) { int t = 1000; int p = 1; int ans = 0; for (int i=1; i&=4; i++) { ans += a / t * a %= t /= 10; p *= 10; } } int main() { for (int i=1000; i&=1111; i++) { int t = i * 9; if (GetReverseNum(i) == t) { printf(&%d\n&, i); } } return 0; } 42. 题目描述: 打印所有不超过 n(n&256)的,其平方具有对称性质的数。 如 11*11=121 输入: 无任何输入数据 输出: 输出具有题目要求的性质的数。如果输出数据不止一组,各组数据之间以回车隔开。 样例输入: 样例输出: #include&stdio.h& int main() { int n=256,a[5],b,m,c[256],p=0; for(int i=0;i&n;i++) { int flag=0; m=0; b=i*i; while(b!=0) { a[m++]=b%10; b=(b-a[m-1])/10; } for(int j=0;j&m/2;j++) if(a[j]!=a[m-1-j]){ flag=1; if(flag==0) printf(&%d\n&,i); } return 0; }}43. 题目描述: The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit. For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39. 输入: The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero. 输出: For each integer in the input, output its digital root on a separate line of the output. 样例输入: 24 39 0 样例输出: 6 3 #include &iostream& #include &string& string getRoot(string s){ int v = 0; for(int i=0;i&s.length();i++){ v += s[i] - '0'; } int t = v, res.resize(0); while(t!=0){ tmp = t%10; res.push_back('0'+tmp); t /= 10; } if(res.size()==1){ } else{ return getRoot(res); } } int main(int argc,char* argv[]){ while(cin&&s&&s!=&0&){ cout&&getRoot(s)&& } return 0; } 44. 题目描述: 输入两个不超过整型定义的非负 10 进制整数 A 和 B(&=231-1),输出 A+B 的 m (1 & m &10)进 制数。 输入: 输入格式:测试输入包含若干测试用例。每个测试用例占一行,给出 m 和 A,B 的值。 当 m 为 0 时输入结束。 输出: 输出格式:每个测试用例的输出占一行,输出 A+B 的 m 进制数。 样例输入: 8
0 样例输出:
#include &iostream& #include &string& int main() { unsigned int m,a,b; u unsigned int stack[1000]; int top=-1; while(cin&&m) { if(m==0) return 0; cin&&a&&b; sum=a+b; if(sum==0) stack[++top]=0; while(sum!=0) { stack[++top]=sum%m; sum=sum/m; } while(top!=-1) cout&&stack[top--]; cout&&&&&& } return 1; } 45. 题目描述: 求任意两个不同进制非负整数的转换(2 进制~16 进制) ,所给整数在 long 所能表达的 范围之内。 不同进制的表示符号为(0,1,...,9,a,b,...,f)或者(0,1,...,9,A,B,...,F) 。 输入: 输入只有一行,包含三个整数 a,n,b。a 表示其后的 n 是 a 进制整数,b 表示欲将 a 进制整数 n 转换成 b 进制整数。a,b 是十进制整数,2 =& a,b &= 16。 数据可能存在包含前导零的情况。 输出: 可能有多组测试数据,对于每组数据,输出包含一行,该行有一个整数为转换后的 b 进制数。输出时字母符号全部用大写表示,即(0,1,...,9,A,B,...,F) 。 样例输入: 15 Aab3 7 样例输出: 210306 #include &iostream& #include &string& #include &cmath& int main() { int a,b; long n=0; while(cin&&a&&a&=2&&a&=16) { cin&&str&&b; int size=str.size()-1; for(string::iterator i=str.begin();i!=str.end();i++) { if(*i&64&&*i&71)n+=(*i-55)*pow(a,size--); else if(*i&96&&*i&103)n+=(*i-87)*pow(a,size--); else n+=(*i-48)*pow(a,size--); } str=&&; char ch='0';int tmp=0; if(n!=0)while(n) { tmp=n%b; if(tmp&9)ch=tmp-10+'A'; else ch=tmp+'0'; str+= n/=b; } else str=&0&; for(string::iterator j=str.end()-1;j&=str.begin();j--) cout&&*j; cout&& } return 0; } 46. 题目描述: 将一个长度最多为 30 位数字的十进制非负整数转换为二进制数输出。 输入: 多组数据,每行为一个长度不超过 30 位的十进制非负整数。 (注意是 10 进制数字的个数可能有 30 个,而非 30bits 的整数) 输出: 每行输出对应的二进制数。 样例输入: 0 1 3 8 样例输出: 0 1 11 1000 #include &iostream& #include &cstdio& #include &cstring& #include &bitset& int main(){ int i,j,num[40],len,c,tmp,len_ char s[40],res[200]; // freopen(&1.txt&,&r&,stdin); // freopen(&out.txt&,&r&,stdout); while (scanf(&%s&,s)!=EOF) { for (len=0;s[len];len++) { num[len]=s[len]-'0'; } //跳出循环后长度也就出来了 i=0; len_res=0; while (i&len) { res[len_res++]=num[len-1]%2+'0'; //这里是转化为 2 进制,只用最后一位来除 2 余数就可以了,其 他高位对除 2 余数不影响,因为从高位借过一位来总是能被 2 整除, c=0;//借位 for (j=i;j&j++) { tmp=num[j]; num[j]=(num[j]+c)/2; if (tmp&1)//当这一位是奇数的时候表示不能整除 2,要 借位了 { c=10; }else{ c=0; } } if (num[i]==0)//可能前几次不为 0,再循环 { i++; } } for (i=len_res-1;i&=0;i--)//逆向输出 { printf(&%c&,res[i]); } printf(&\n&); } // // } 47. 题目描述: 输入一个整数,将其转换成八进制数输出。 输入: 输入包括一个整数 N(0&=N&=100000)。 输出: 可能有多组测试数据,对于每组数据, 输出 N 的八进制表示数。 样例输入: 7 8 9 样例输出: 7 10 11 #include&stdio.h& #include&stdlib.h& #include&string.h& fclose(stdout); fclose(stdin); return 0;int main() { while(~scanf(&%d&,&n)) { int num[10]; memset(num,0,sizeof(num)); int i=0; if(n==0) printf(&0\n&); else { while(n) { num[i++]=n%8; n=n/8; } for(int j=i-1;j&=0;j--) if(j==0)printf(&%d\n&,num[j]); else printf(&%d&,num[j]); } } return 0; } 48. 题目描述: 输入两个正整数,求其最大公约数。 输入: 测试数据有多组,每组输入两个正整数。 输出: 对于每组输入,请输出其最大公约数。 样例输入: 49 14 样例输出: 7 #include &iostream& #include &fstream& int main(){ // ifstream cin(&input.txt&); int a,b, while(cin&&a&&b) { while(b&0) { a=a%b; temp=b; b=a; a= } cout&&a&& } } 49. 题目描述: 给定两个正整数,计算这两个数的最小公倍数。 输入: 输入包含多组测试数据,每组只有一行,包括两个不大于 1000 的正整数。 输出: 对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。 样例输入: 10 14 样例输出: 70 #include&stdio.h& int gcd(int a,int b ) { return b!=0 ? gcd(b,a%b) :a; } int main() { int a,b; while(scanf(&%d%d&,&a,&b)!=EOF) printf(&%d\n&,a*b/gcd(a,b)); return 0; }题目描述: The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. 输入: Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 ... nm where m is the number of integers in the set and n1 ... nm are the integers. All integers will be positive and lie within the range of a 32-bit integer. 输出: For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer. 样例输入: 2 3 5 7 15 6 4 87 792 1 样例输出: 105 10296 50. #include &cstdio& #include &cstring& int gcd(int a,int b){ if(b==0) else return gcd(b,a%b); } int main(){ scanf(&%d&,&n); while(n--){ scanf(&%d&,&c); int tmp,k=0; while(c--){ scanf(&%d&,&tmp); if(k==0) k= else k=k/gcd(k,tmp)* } printf(&%d\n&,k); } return 0; } 51. 题目描述: 给定一个数 n,要求判断其是否为素数(0,1,负数都是非素数) 。 输入: 测试数据有多组,每组输入一个数 n。 输出: 对于每组输入,若是素数则输出 yes,否则输入 no。 样例输入: 13 样例输出: Yes #include &iostream& #include &cmath& int N; void solve() { if(N&=1){cout&&&no&&&} else { int M =(int)sqrt((double)N); for(int i = 2; i&=M; i++) if(N%i == 0) { cout&&&no&&& } } cout&&&yes&&& } int main() { while(cin&&N) { solve(); } return 0; } 52. 题目描述: 输入一个整数 n(2&=n&=10000),要求输出所有从 1 到这个整数之间(不包括 1 和这个整数)个 位为 1 的素数,如果没有则输出-1。 输入: 输入有多组数据。 每组一行,输入 n。 输出: 输出所有从 1 到这个整数之间(不包括 1 和这个整数)个位为 1 的素数(素数之间用空格隔开, 最后一个素数后面没有空格),如果没有则输出-1。 样例输入: 100 样例输出: 11 31 41 61 71 #include&iostream& #include&vector& #include&cmath& bool IsPrime(int num) { bool flag = for(int i=2;i&=(int)sqrt((double)num);i++) if(num%i==0) { flag = } } int main() { vector&int& while(cin&&n) { if(n&=10) cout&&&-1&&& else{ val.clear(); for(int i=11;i&n;i=i+10) if(IsPrime(i)) val.push_back(i); for(size_t ind=0;ind&val.size()-1;ind++) cout&&val[ind]&&' '; cout&&val[val.size()-1]&& } } return 0; } 53. 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k-th prime number. 样例输入: 3 7 样例输出: 5 17 #include&iostream& #include&malloc.h& #include&stdio.h& int main() { int *I int i,j,k,m,z; int N=200000, Isprime=(int*)malloc((N+1)*sizeof(int)); for(int n=0;n!=N;n++) { Isprime[n]=1; } for(i=0;i!=N;i++) { if(i%2==0) { Isprime[i]=0; } } Isprime[2]=1; Isprime[1]=0; for(j=3;j*j&N;j++) { if(Isprime[j]==1) { for(k=j+1;k&N;k++) { if(k!=j&&k%j==0) { Isprime[k]=0; } } } } while(cin&&z) { count=0; for(m=0;m!=N;m++) { if(Isprime[m]==1) { count++; if(count==z) } } cout&&m&& } return 0; } 54. 题目描述: Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number. A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs. 输入: An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0. 输出: Each output line should contain an integer number. No other characters should appear in the output. 样例输入: 6 10 12 0 样例输出: 1 2 1 #include &stdio.h& #include &math.h& bool mark[33001]; void init() { for( int i = 2; i &= 32770; i ++ ) mark[i] = for( int i = 2; i &= 32770; i ++ ) { if( !mark[i] ) { for( int j = i * j &= 32770; j += i ) mark[j] = } } } int main() { init(); while (scanf(&%d&, &n) != EOF && n != 0 ) { int cnt = 0; for( int i = 2; i &= n / 2; i ++ ) if( !mark[i] && !mark[n - i] ) cnt ++ ; printf(&%d\n&, cnt); } return 0; } 55.题目描述: 求正整数 N(N&1)的质因数的个数。 相同的质因数需要重复计算。如 120=2*2*2*3*5,共有 5 个质因数。 输入: 可能有多组测试数据,每组测试数据的输入是一个正整数 N,(1&N&10^9)。 输出: 对于每组数据,输出 N 的质因数的个数。 样例输入: 120 样例输出: 5 #include &iostream& #include &cstdio& #include &string& #include &cstring& #include &cctype& #include &cstdlib& #include &string.h& #include &algorithm& #include &cmath& int main() { int n,ans,i; while(scanf(&%d&,&n) == 1) { ans = 0; for(i = 2;i &= sqrt(n);i++) if(n % i == 0) { while(n%i == 0) { ans ++; n = n / } } if(n & 1) ans ++; printf(&%d\n&,ans); } return 0; } 56. 题目描述: 给定 n,a 求最大的 k,使 n!可以被 a^k 整除但不能被 a^(k+1)整除。 输入: 两个整数 n(2&=n&=1000),a(2&=a&=1000) 输出: 一个整数. 样例输入: 6 10 样例输出: 1 #include &iostream& #define MAXSIZE 500 int main() { int n, while(cin && n && cin && a){ int minDivisor[MAXSIZE]; int countMinDiv[MAXSIZE]; int countDiv = 0; int temA = for(int i = 2; i&=temA; i++){ if(temA%i == 0){ minDivisor[countDiv] = countMinDiv[countDiv] = 0; while(temA%i == 0){ temA /= countMinDiv[countDiv]++; } countDiv++; } } int k = 0, count = 0; for(int j=0; j&countD j++){ count = 0; for(int i=2; i&=n; i++){ int tem = while(tem%minDivisor[j] == 0){ count++; tem /= minDivisor[j]; } } countMinDiv[j] = count/countMinDiv[j]; } k = countMinDiv[0]; for(int i=1; i&countD i++){ if(countMinDiv[i] & k){ k = countMinDiv[i]; } } cout && k && } return 0; } 57. 题目描述: 输入 n 个整数,依次输出每个数的约数的个数 输入: 输入的第一行为 N,即数组的个数(N&=1000) 接下来的 1 行包括 N 个整数,其中每个数的范围为(1&=Num&=) 当 N=0 时输入结束。 输出: 可能有多组输入数据,对于每组输入数据, 输出 N 行,其中每一行对应上面的一个数的约数的个数。 样例输入: 5 1 3 4 6 12 样例输出: 1 2 3 4 6 #include &iostream& #include &cstdio& #include &cmath&
const int MAX = 1000 + 5; int a[MAX]; int main() { int n, int i, while (scanf(&%d&, &n) != EOF) { for (j=0; j&n; j++) { scanf(&%d&, &m); int ans = 0; int t = sqrt(m * 1.0); for (i=1; i&=t; i++) { if (m % i == 0) { (m / i == i)? ans++ : ans += 2; } } printf(&%d\n&, ans); } } return 0; } 58. 题目描述: 求 A^B 的最后三位数表示的整数。说明:A^B 的含义是“A 的 B 次方” 输入: 输入数据包含多个测试实例, 每个实例占一行, 由两个正整数 A 和 B 组成 (1&=A,B&=10000) , 如果 A=0, B=0,则表示输入数据的结束,不做处理。 输出: 对于每个测试实例,请输出 A^B 的最后三位表示的整数,每个输出占一行。 样例输入: 23 12 6
00 样例输出: 8 984 1 #include &stdio.h& int main() { int a,b; while(scanf(&%d%d&,&a,&b)!=EOF){ if(a==0&&b==0) int ans=1; while(b!=0){ if(b%2==1){ ans*=a; ans%=1000; } b/=2; a*=a; a%=1000; } printf(&%d\n&,ans); } return 0; } 59. 题目描述: Xinlv wrote some sequences on the paper a long time ago, they might be arithmetic or geometric sequences. The numbers are not very clear now, and only the first three numbers of each sequence are recognizable. Xinlv wants to know some numbers in these sequences, and he needs your help. 输入: The first line contains an integer N, indicting that there are N sequences. Each of the following N lines contain fo

我要回帖

更多关于 字符串包括空格吗 的文章

 

随机推荐