1、在实际的开发工作中,对于string的处理是最常见的编程任务,本题是要求程序对用户输入的string进行处理,具体要求如下: 1、每个单词的首字母变为大写。 2、将数字与字母之间用下划线隔开,使结构清晰。 3、多个空格变为1个空格。 示例:输入:you and me what cpp2005pragram 则输出:You And Me What Cpp_2005_Program 输入:this is a 99cat 则输出:This Is A 99_Cat*/ 2* A,B,C,D,E,F,G,H,I,J,共10名学生有可能参加计算机竞赛,也可能不参加,因为某种原因他们受到下列条件的约束:1、 如果A参加,B也参加; 2、 如果C不参加,D也不参加; 3、 A和C中只能有1个人参加; 4、 B和D中有且仅有1个人参加; 5、 D、E、F、G、H 中至少有2人参加; 6、 C和G或者都参加,或者都不参加; 7、 C、E、G、I中至多只能2人参加 8、 如果E参加,那么F和G也都参加。 9、 如果F参加,G、H就不能参加 10、 如果I、J都不参加,H必须参加请编程根据这些条件判断10名同学参赛者名单比如LC D G J 代码在Num2里 3 *要求找出具有下列性质的数的个数(包含输入的自然数n): 先输入1个自然数n(n<=500),然后对此自然数按照如下方法进行处理: ①、 不作任何处理;②、 在它的左边加上1个自然数,但该自然数不能超过原数首位数字的1半; ③、 加上数后,继续按此要求进行处理,直到不能再加自然数为止、 样例: 输入: 6 满足条件的数为 6 16 26 126 36 136 输出: 6 */ private function fenjie(cnum: int, count: int , str: String): void{if(cnum == 0)return;if(count == max)trace(str);for (var i:int = cnum; i > 0; i--) {if(count + i > max)continue;fenjie(i, count +i, str + i);}} /*巧排数字。将1、2、...、20这20个数排成1排,使得相邻的两个数之和为1个素数,且首尾两数字之和也为1个素数。编程打印出所有的排法。*/ 有1楼房的楼梯级数很奇特,1步跨二级多1级,1步跨三级多二级,如果分用四、五、六、七去除级数分别余三、三、五、五。问这楼房共有多少级阶梯?(已知不超过400级)。 狼追兔子,兔子躲进了10个环形分布的洞的某1个中。狼在第1个洞中没有找到兔子,就间隔1个洞,到第3个洞中去找,也没找到兔子,就间隔2个洞,到第6个洞中去找。以后狼每次多隔1个洞去找兔子,……。这样狼1直找不到兔子。请问兔子可能躲在哪个洞中? 2、 1位数学家和1些游客共81人不幸落入强盗手中,强盗将俘虏排成1队,宣布每天处理所有第2的N次方个俘虏(N>=0),而只放走剩下的最后1个。由于数学家身怀重任,不得不选择了1个恰当的位置而最终被放走。请问他归初排在第几个位置。 3、 有1堆礼物,工作人员无论是分成二个1份,还是三个、四个、五个、六个1份,总是多1个。请问这堆礼物至少多少个? public class Test9 {public static void main(String[] args) {//求2、3、4、5、6的最小公倍数int i =7;while(true){if(i%2==1&&i%3==1&&i%4==1&&i%5==1&&i%6==1){System、out、println(i);break;}i=i+6;}}} 4、 1付扑克中拿出所有的黑桃A……K按顺序排好。第1次翻出第1张牌——A,放在1边,再拿出第2张放到牌的最下面。以后每次都翻出1张牌,再将1张牌放到最后,问第八次翻出的牌是哪1张? public class Test10 {public static void main(String[] args) {String a[]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};List package test; import java、util、Arrays;import java、util、Scanner; /*验证卡布列克常数,对于1个四位数N,进行下列运算:(1)将组成该四位数的4个数字由大到小排列,形成由这4个数字组成的最大的四位数; (2)将组成该四位数的4个数字由小到大排列,形成由这4个数字组成的最小的四位数(如果高位为0则取得的数不足4位); (3)求两个数的差,得到1个新的四位数(高位0保留),称为对N进行了1次卡布列克运算。有这样的规律: 对1个各位数字不全相同的四位数重复进行若干次卡布列克运算,最后得到的结果总是6174。这个数被称为卡布列克常数。N从键盘输入。 输出每1次的卡布列克运算及得到6174时的运算次数。*/public class Test11 {private static int count = 0; public static void main(String[] args) {Scanner sc = new Scanner(System、in);System、out、println("请输入1个不完全相同的四位数");String str = sc、nextLine();char c1[] = str、toCharArray();verify(c1);Arrays、sort(c1);String strMin = String、copyValueOf(c1);String strMax = "";for (int i = c1、length - 1; i >= 0; i--) {strMax = strMax + c1[i];}// System、out、println(strMax+" "+strMin);int max = Integer、parseInt(strMax);int min = Integer、parseInt(strMin);System、out、println("max=" + max + " " + "min=" + min);kablk(strMax, strMin);} public static void verify(char[] c) {if (c、length < 4 || c、length > 4) {System、out、println("长度不符合要求");System、exit(0);}boolean flag = true;for (int i = 0; i < c、length - 1; i++) {if (c[i] != c[i + 1]) {flag = false;break;}}if (flag) {System、out、println("四位数完全相等");}} public static void kablk(String strMax, String strMin) {count++;int max = Integer、parseInt(strMax);int min = Integer、parseInt(strMin);int temp = max - min;if (temp == 6174) {System、out、println("count="+count);} else if (temp > 0) { char c1[] = String、valueOf(temp)、toCharArray();Arrays、sort(c1);strMin = String、copyValueOf(c1);for (int i = c1、length ; i < 4; i++) {strMin = "0" + strMin;}c1 = strMin、toCharArray();// System、out、println(c1);min = Integer、parseInt(strMin);strMax = "";for (int i = c1、length - 1; i >= 0; i--) {strMax = strMax + c1[i];// System、out、println(c1[i]);}max = Integer、parseInt(strMax);kablk(strMax, strMin);System、out、println("max=" + max + " " + "min=" + min);} else {return;} }} 编1程序,从键盘输入数字R,计算机自动检查在下列算式的“()”中能否填上“+”或“-”号凑成相应的等式。如能凑成,则打印出这些算式。如不能则打印“NO ANSWER”。 1( )2( )3( )4( )5( )6( )7( )8( )9=R “百钱买百鸡”是我国古代的著名数学题。题目这样描述:3文钱可以买1只公鸡,2文钱可以买1只母鸡,1文钱可以买3只小鸡。用100文钱买100只鸡,那么各有公鸡、母鸡、小鸡多少只?与之相似,有"鸡兔同笼"问题。 判断1string是否是回文数,如121、12321、ABA等(string输入时以‘、’结束)。如输入:12321、输出:yes 找数。1个三位数,各位数字互不相同,十位数字比个位、百位数字之和还要大,且十位、百位数字之和不是质数。编程找出所有符合条件的三位数。 注:1、 不能手算后直接打印结果。 2、 “质数”即“素数”,是指除1和自身外,再没有其它因数的大于1的自然数。 选人。1个小组共五人,分别为A、B、C、D、E。现有1项任务,要他们中的3个人去完成。已知:(1)A、C不能都去;(2)B、C不能都不去;(3)如果C去了,D、E就只能去1个,且必须去1个;(4)B、C、D不能都去;(5)如果B去了,D、E就不能都去。编程找出此项任务该由哪三人去完成的所有组合。 李润伟(22048303) 17:45:55 截数问题: 任意1个自然数,我们可以将其平均截取成三个自然数。示例自然数135768,可以截取成13,57,68三个自然数。如果某自然数不能平均截取(位数不能被3整除),可将该自然数高位补零后截取。现编程从键盘上输入1个自然数N(N的位数<12),计算截取后第1个数加第三个数减第2个数的结果。 试编程找出能被各位数字之和整除的1切两位数 1个正整数的个位数字是6,如果将个位数字移到首位,所得到的数是原数的4倍,试编程找出满足条件的最小正整数。 某本书的页码从1开始,小明算了算,总共出现了202个数1,试编程求这本书1共有多少页? 有30个男人女人和小孩同在1家饭馆进餐,共花了五十先令,其中男宾3先令,女宾2先令,小孩1先令。试编程求出男人女人小孩各多少人? 编程找出四个互不相等的自然数, 它们之中任意两数之和为偶数, 任意三数之和可以被3整除, 而且这四个数的和越小越好(已知它们的和不大于50)、 以不同的字母代表0--9之间的数字, 现有如下等式成立: a+bc+def=ghij,编程求出满足上述条件等式的个数并将所有等式打印输出、 下面的竖式表示, 图中的"*"号只能用素数2,3,5,7代替, 因此称为素数乘法竖式、 * * * × * * --------------- * * * * * * * * ---------------- * * * * * 1个四位数是1个完全平方数,减去1个每位数字都相同的四位数( 如 1111, 5555)后, 仍是1个完全平方数、 请编程打印出所有这样的四位数、 有1个八位数12345679, 若它乘以9, 则得九位数111111111, 试求:素数 (1)当这个数乘以什么数时, 才能得到全部由5所组成的九位数? (2)当这个数乘以什么数时, 才能得到全部由9所组成的九位数? 李先生和他的孙子同出生于20世纪, 他的孙子与他的年龄之差为60岁, 李先生和他的孙子出生年份被3,4,5,6除, 余数分别为1,2,3,4、 编程求出李先生和他的孙子各出生在哪1年、 16/64是1个分子和分母都是两位数的真分数, 且分子的个位数与分母的十位数相同、 非常奇怪的是: 如果将该分数的分子的个位数和分母的十位数同时划去, 所得到的结果正好等于原分数约分后的结果、 例 16/64=1/4、 编程找出所有满足上述条件的真分数、 甲去买东西, 要付给乙19元, 而甲只有3元1张的钱, 乙只有5元1张的钱、 请为他们设计1个交换方案、 有六箱货物,重分别是5吨、2吨、3、5吨、1、7吨、1吨、5、1吨。现有1台货车,载重量10吨。设计1个程序,使这次车运走的货物最多。 某电台组织1次智力竞赛,计划安排奖励30人。准备了50件奖品。得1等奖者可得3件,二等奖2件,三等奖1件。希望将所有奖品都发到获奖者手中。请找出所有方案(即各等奖各有多少人)。 1个自然数是素数, 且它的数字位置经过任意对换后仍为素数, 称为绝对素数、 示例 13、 试找出所有这样的四位绝对素数 1个自然数, 若它的质因数至少是两重的(相同的质因数至少个数为二个, 如36=2*2*3*3)则称该数为"漂亮数"、 若相邻两个自然数都是"漂亮数", 就称它们为"孪生漂亮数"、 示例8与9就是1对、 请编程再找出1对"孪生漂亮数"。 某本书的页码从1开始,小明算了算,总共出现了202个数1,试编程求这本书1共有多少页?
public class Test3 {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub char buf[] = { 'a', 'b', 'c'}; int k = buf、length; // 选择几个字母排序 // char r[] = new char[k]; perm(0, buf, k); // 开始的字母在start,几个字母为k
}
public static void perm(int start, char[] buf, int k) { if (start == k) { // 1个字母的全排列 for (int i = 0; i < k; i++) { System、out、print(buf[i]); } System、out、println(); } else if (start < k) {// 多个字母全排列 for (int i = start; i < k; i++) { char temp = buf[start]; // 交换数组的第1个元素和后续元素 buf[start] = buf[i]; buf[i] = temp; perm(start + 1, buf, k); // 后续元素全排列 temp = buf[start]; // 还原 buf[start] = buf[i]; buf[i] = temp; System、out、println("buf[start]="+buf[start]+" buf[i]="+buf[i]); System、out、println("start="+start+" i="+i); } } else { return; } }
}
任意输入二个自然数, 若商为整数, 则直接显示商; 否则将商分解成1个自然数和1个正的既约真分数之和才显示。 示例: 输入: 9, 3 显示: 9/3=3
输入: 8, 6 显示: 8/6=1+1/3
任意输入四个自然数a,b,c,d, 看成二个分数a/b, c/d、 求这二个分数之和、 和的显示格式为: 输入 3,2,1,6 输出: 3/2+1/6=1+2/3。
在自然数中, 各位数字之和的11倍正好等于自身的自然数只有1个、 请找出这个自然数。
求所有不超过1000的这样的整数, 它的平方的末二位数字相同但不为0 Ecol package today;
import java、util、ArrayList; import java、util、List;
public class help13 { static List
static boolean function(int num) { for(int i=0;i<10000;i++){count、add(0);} boolean bl = false; int i = 2; while (num > 1) { if (num % i == 0) { count、set(i-1,count、get(i-1)+1); num /= i; i = 2; } else { i++; }
} for (int k = 0; k < count、size(); k++) { if (count、get(k) <2&&count、get(k)>0) { //bl = true; break; } if(k==count、size() 1){bl=true;}
} count = null; count = new ArrayList return bl;
}
public static void main(String[] args) { for (int i = 3; i <= 9999; i++) { if (function(i) && function(i + 1)) { System、out、println(i); }
} } }
package test;
//1个正整数的个位数字是6,如果将个位数字移到首位,所得到的数是原数的4倍,试编程找出满足条件的最小正整数 public class Test16 {
public static void main(String[] args) { // TODO Auto-generated method stub String str = ""; int n = 0, m = 0; int count = 1; while (true) { n = (int) Math、pow(10, count) + 6; //初始化为16,106,1006,10006,。。。。 for (int i = 1; i < 3;) { str = String、valueOf(n); String temp = str、substring(0, str、length() - 1); str = str、substring(str、length() - 1) + temp; m = Integer、valueOf(str); if (m==n*4) { System、out、println(n + " " + m); System、exit(0); } n = n + 10; i = (int) (n / Math、pow(10, str、length() - 1));//判断首位是否超过2 } count++; } } }
P是1个大于3的质数, 对某个自然数N, PN恰好是五位数, 且至少有三个位上的数字相同, 求P至少是多少。
编程求最小正整数M,N(0 验证下面结论: 1个各位数字不同且都不为0的N位数X(3<=N<=5), 将组成该数的各位数字重新排列成1个最大数和1个最小数作减法, 其差值再重复前述运算, 若干次后必出现1个N位数Y, 使之重复出现、 示例: X=213, 则有 213→321-123=198 981-189=892 982-289=693 963-369=594 954-459=495 954-459=495 这时Y=954、 package test; import java、util、Scanner; /*任意输入二个自然数, 若商为整数, 则直接显示商; 否则将商分解成1个自然数和1个正的既约真分数之和才显示。 示例: 输入: 9, 3 显示: 9/3=3 输入: 8, 6 显示: 8/6=1+1/3*/ public class Test17 { public static void main(String[] args) { int a, b, n, m, l; Scanner sc = new Scanner(System、in); System、out、println("a="); a = sc、nextInt(); System、out、println("b="); b = sc、nextInt(); n = a / b; // 商的整数部分 m = a % b;// 余数 if (m != 0) // 不为0 { l = Gcd(m, b); // 求出余数与被除数的最大公约数 m = m / l; // 分子; b = b / l; // 分母 System、out、println(a + "/" + b + "=" + n + "+" + m + "/" + b); } else System、out、println(a + "/" + b + "=" + n); } static int Gcd(int m, int n) { if (n == 0) return m; else return Gcd(n, m % n); } } 任给1个自然数n,求出这个自然数不同因数的个数M 给出1个数n的不同因数个数m,求最小满足要求的自然数n,即n有m个不同的因数。 示例输入 2 则输出 2 因为2有2个因数。 m,n为自然数,其上限为k,试编写程序,由键盘输入自然数k找出满足条件: (n^2-mn-m^2)^2=1 且使m^2+n^2达到最大的m,n。 输出能被11整除且不含重复数字的三位数。并统计个数。 已知1个四位数为ABCD,若A+C和B+D的值相等,则称这个四位数为交叉数,求四位数的交叉数和个数。 定义 2x6=12 2和6的积是12,因此2和6是12的因数。12是2的倍数,也是6的倍数。 3x4=12 3和4也是12的因数。12是3和4的倍数。 整数A乘以整数B得到整数C,整数A与整数B就称做整数C的因数,反之整数C就为整数A与整数B的倍数。 package eclip; /*在自然数中, 各位数字之和的11倍正好等于自身的自然数只有1个、 请找出这个自然数。*/ public class Test4 { public static long check(long num){ long temp=num; long sum=0; while(temp!=0){ sum+=temp%10; temp=temp/10; } return sum*11; } public static void main(String[] args) { long num=1; while(true){ if(check(num)==num){ System、out、println(num); break; } num++; } } } P是1个大于3的质数, 对某个自然数N, PN恰好是五位数, 且至少有三个位上的数字相同, 求P至少是多少。 package ecol; /*小明的妈妈是负责分发全厂工资的。 为使分发时有足够多的零钞,同时又 尽量不使每个人领到的钱太零碎。每 个月她都要计算出各种面值的钞票( 100元、50元、10元、5元、2元、1元, 假设每个人的工资都是整数元)各需要多少张 。你能否为她设计1个程序,从键盘输入10个人 的工资,再计算出各种面值的钞票各需要多少张?*/ public class mony { static void function(int num){ int array[]=new int[5]; array[0]=100; array[1]=50; array[2]=10; array[3]=5; array[4]=2; System、out、print(num+" = "); for(int i=0;i if(num/array[i]>0){System、out、print(num/array[i]+"*"+array[i]+(num%array[i]>0?"+":""));} num%=array[i]; } if((num/1)>0){System、out、println(num/1+"*"+1);} } public static void main(String[] args) { function(12345); } } 输入1个string,将其中重复偶数次字符个数n,变为n/2,将重复奇数次字符个数m(m>=3),变为3*m+1,直到n=1,m=1,全部停止,输出每次做的1轮奇偶变化的string,示例:“gooddd”: godddddddddd,goddddd,god、、、、d(16个d),godddddddd,godddd,godd,god 有1根长为514CM的钢筋,现在要截成23CM、15CM和19CM的短料,问在各至少截1根的前提下,问各截多少根,使所剩余料最少 package ecol; import java、util、Scanner; public class math { static boolean function(int num){ boolean bl=false;int i; for(i=2;i<=(int)Math、sqrt(num);i++){ if(num%i==0){break;} } if(i>(int)Math、sqrt(num)){bl=true;} return bl; } static boolean function1(int num){ boolean bl=false; int[]array=new int[10]; String str=""+num; for(int i=0;i array[Integer、parseInt(str、substring(i,i+1))]++; } for(int i=0;i<10;i++){ if(array[i]>=3){bl=true;break;} } return bl; } public static void main(String[] args) { Scanner in=new Scanner(System、in); int N=in、nextInt(); for(int i=10000;i<=99999;i++){ if(i%N==0&&function(i/N)&&function1(i)){System、out、println(i/N);break;} } } } 随意生成10个整数(20以内,不重复,随机位置),每个数加上它所存储位置的下标的和为质数: 1、如果这10个整数,满足条件的超过5个,就输出 2、如果改为10以内的数,请输出满足条件的个数和整数排列 求能被11整除,且不含重复数字的三位数?有多少个,并输出(注重算法效率) 求2~1000中的完数,(因子和等于它本身的数为完数。示例28的因子是1,2,4,7,14,且1+2+4+7+14=28,则28是完数)。 找2~1000中的亲密数对(如果A 的因子和等于B,B的因子和等于A,且A不等于B,则称A,B为亲密数对)。 有1个三位数,三个数字和为20,第三个数 3倍与第2个数的2倍及第1个数三者之和为44,第1个数与第2个数和的2倍减去第三个烽的4倍为-14,求这个三位数。 父子二人,已知儿子年龄不大于40岁,父亲年龄不大于100岁,10年前父亲的年龄是儿子年龄的4倍,10年后父亲的年龄是儿子年龄的整数倍。问父子现年多少岁 纯粹素数是这样定义的:1个素数,去掉最高位,剩下的数仍为素数,再去掉剩下的数的最高位,余下的数还是素数。这样下去1直到最后剩下的个位数也还是素数。求出所有小于3000的四位纯粹素数。 求n个最小的连续合数。合数是除了1和本身以外还有其它因子的正整数。 输入样例:3 输出样例: 8 9 10 大家熟知鸡兔同笼问题,输入两个数a,b,a为脚的只数,b为头的个数。编程序输出鸡的只数和兔的只数。 求圆周率π≈1-1/3+1/5-1/7+…+(-1)n-11/(2n-1),求π的近似值,真到某项的绝对值小于10-6为止 编写出打印出右边数字方阵的程序。 1 2 3 4 5 6 2 3 4 5 6 7 3 4 5 6 7 8 5 6 7 8 9 10 6 7 8 9 10 11 找出1~100之间的全部“同构数”。“同构数”是这样1种数:它出现在它的平方数的右端。示例:5的平方是25,5就是同构数,25也是构数。 用筛法求1 到10000的素数 有从1到n依次编号的n个人和n盏灯。我号人将所有的灯都关掉;2号人将编号为2的倍数的灯都打开;3号人则将编号为3的倍数的灯作相反处理;以后的人都将凡是自己编号的倍数的灯作相反处理。问第n个人操作后,哪些灯是打开的? 运动会连续开了n天,1共发了m枚奖章,第1天发1枚并剩下(m-1)枚的1/7,第2天发2枚并剩下的1/7,以后每天按此规律发奖章,在最后1天即第n天发了剩下的n枚奖章。问运动会开了多少天?1共发了几枚奖章? 设有1个数组A:array [0、、N-1] of integer; 存放的元素为0~N-1(1 A[0]编码为0; A[i]编码为:在A[0],A[1],…,A[i-1]中比A[i]的值小的个数 (i=1,2,…,N-1) ∴上面数组A的编码为:B=(0,0,0,3,1,2) 要求编程解决以下问题: (1)给出数组A后,求出其编码; (2)给出数组A的编码后,求出A中的原数据 程序样例: 例1: 输入:Stat=1 {表示要解决的第(1)问题} N=8 {输入8个数} A=1 0 3 2 5 6 7 4 输出:B=0 0 2 2 4 5 6 4 例二: 输入:Stat=2 {表示要解决的第(2)问题} N=7 B=0 1 0 0 4 5 6 输出:A=2 3 1 0 4 5 6 矩阵相乘:已知N×M1矩阵A和M1×M矩阵B(1≤M、M1、N≤10),求矩阵C(=A×B)。示例: 输入:N,M1,M=4 3 4 A= 1 2 3 3 4 5 提示:所谓矩阵相乘(如A×B=C),是指 4 5 6 Cij= ∑(Aik×Bkj)(i=1~N,j=1~M1,k=1~M) 5 –1 –2 B= 1 6 4 2 示例: 2 3 4 1 C11=A11×B11+A12×B21+A13×B31 –1 5 7 –3 =1×1+2×2+3×(– 1) 输出:C= 2 27 33 –5 =2 6 55 63 –5 C42= A41×B12+A42×B22+A43×B32 8 69 78 –5 =5×6+(–1)×3+(–2)×5 5 17 2 15 =17 输入N(2≤N≤100)个数字(在0与9之间),然后统计出这组数中相邻两数字组成的链环数字对出现的次数。示例: 输入:N=20 {表示要输入数的数目} 0 1 5 9 8 7 2 2 2 3 2 7 8 7 8 7 9 6 5 9 输出:(7,8)=2 (8,7)=3 {指(7,8)、(8,7)数字对出现次数分别为2次、3次) (7,2)=1 (2,7)=1 (2,2)=2 (2,3)=1 (3,2)=1 生成1个按蛇形方式排列自然数1,2,3,4,5,……,N2的 (1 输入:N=4 N=7 输出: 1 3 4 10 1 3 4 10 11 21 22 2 5 9 11 2 5 9 12 20 23 34 6 8 12 15 6 8 13 19 24 33 35 7 13 14 16 7 14 18 25 32 36 43 15 17 26 31 37 42 44 16 27 30 38 41 45 48 28 29 39 40 46 47 49 |
1、题目描述: 对输入的n个数进行排序并输出。 输入: 输入的第1行包括1个整数n(1<=n<=100)。 接下来的1行包括n个整数。 输出: 可能有多组测试数据,对于每组数据,将排序后的n个整数输出,每个数后面都有1个空格。 每组测试数据的结果占1行。 样例输入: 4 1 4 3 2 样例输出: 1 2 3 4 #include #include #include using namespace std; int main() { int n; while(cin>>n&&n>=1&&n<=100) { vector int itemp; for(int i=0;i { cin>>itemp; ivec、push_back(itemp); } sort(ivec、begin(),ivec、end()); for(vector cout<<*j<<' '; cout< } return 0; } 2、有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相同则按照学生的年龄排序,并输出N个学生排序后的信息。 输入: 测试数据有多组,每组输入第1行有1个整数N(N<=1000),接下来的N行包括N个学生的数据。 每个学生的数据包括姓名(长度不超过100的string)、年龄(整形数)、成绩(小于等于100的正数)。 输出: 将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。 然后输出学生信息,按照如下格式: 姓名 年龄 成绩 样例输入: 3 abc 20 99 bcd 19 97 bed 20 97 样例输出: bcd 19 97 bed 20 97 abc 20 99 #include #include struct student { char name[101]; int age; int score; }; int main() { int n,i,j; struct student a[1000],temp; while(scanf("%d",&n)!=EOF) { for(i=0;i { scanf("%s %d %d",&a[i]、name,&a[i]、age,&a[i]、score); } for(i=1;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]=temp; } 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]=temp; } 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]=temp; } } } } } for(i=0;i { printf("%s %d %d\n",a[i]、name,a[i]、age,a[i]、score); } } return 0; } 4、 题目描述: 输入1系列整数,将其中最大的数挑出,并将剩下的数进行排序。 输入: 输入第1行包括1个整数N,1<=N<=1000,代表输入数据的个数。 接下来的1行有N个整数。 输出: 可能有多组测试数据,对于每组数据, 第1行输出1个整数,代表N个整数中的最大值,并将此值从数组中去除,将剩下的数进行排序。 第2行将排序的结果输出。 样例输入: 4 1 3 4 2 样例输出: 4 1 2 3 #include int main(){ int i,n,t,j,a[1000]; while(scanf("%d",&n)!=EOF){ for(i=0;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 printf("%d",a[i]); if(i!=n-2) printf(" "); else printf("\n");} } return 0; } 5、 题目描述: Excel可以对1组纪录按任意指定列排序。现请你编写程序实现类似功能。 对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。 输入: 测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (N<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有N行,每行包含1条学生纪录。每条学生纪录由学号(6位数字,同组测试中没有重复的学号)、姓名(不超过8位且不包含空格的string)、成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开。当读到 N=0 时,全部输入结束,相应的结果不要输出。 输出: 对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3 时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。 样例输入: 3 1 000007 James 85 000010 Amy 90 000001 Zoe 60 4 2 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 98 4 3 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 90 0 0 样例输出: 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 #include #include #include using namespace std; struct info{ string id; string name; int score; }; bool cmp_id(const struct info a,const struct info b) { return a、id、id; } bool cmp_name(const struct info a,const struct info b) { if(a、name==b、name) return a、id、id; else return a、name、name; } bool cmp_score(const struct info a,const struct info b) { if(a、score==b、score) return a、id、id; else return a、score、score; } int main(int argc, char **argv) { //ifstream cin("input、txt"); int n,c,count=0; while(cin>>n>>c) { if(n==0&&c==0) break; count++; struct info data[n]; for(int i=0;i cin>>data[i]、id>>data[i]、name>>data[i]、score; switch(c) { case 1: sort(data,data+n,cmp_id); break; case 2: sort(data,data+n,cmp_name); break; case 3: sort(data,data+n,cmp_score); } printf("Case %d:\n",count); for(int i=0;i printf("%s %s %d\n",data[i]、id、c_str(),data[i]、name、c_str(),data[i]、score); } } 6、题目描述: 输入1个string,长度小于等于200,然后将输出按字符顺序升序排序后的string。 输入: 测试数据有多组,输入string。 输出: 对于每组输入,输出处理后的结果。 样例输入: bacd 样例输出: Abcd #include #include int main() { char string[200],temp; int i,j,n; while(scanf("%s",string)!=EOF) { n=0; while(string[n]) { n++; } for(i=1;i { for(j=0;j { if((string[j]-string[j+1])>0) { temp=string[j]; string[j]=string[j+1]; string[j+1]=temp; } } } printf("%s\n",string); } return 0; } 7、 题目描述: 有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天 输入: 有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD 输出: 每组数据输出1行,即日期差值 样例输入: 20110412 20110422 样例输出: 11 #include #include #define MAX_DATE_SIZE 9 int Days[12]={31,28,31,30,31,30,31,31,30,30,31}; typedef struct Date { int Year; int Month; int Day; }Date; int main(int argc,char *argv) { int i,L; int HLN1,HLN2; int sDay,eDay; Date sDate,eDate; 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 for(i=0;i sDay+=sDate、Day; eDay+=eDate、Day; L=(int)fabs((sDate、Year*365+HLN1+sDay) (eDate、Year*365+HLN2+eDay))+1; printf("%d\n",L); } return 1; } 8、 题目描述://蔡勒公式 计算星期几公式 #include usingnamespacestd; intmain(){ intyear,month,day; 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< 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 2004, 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、 样例输入: 9 October 2001 14 October 2001 样例输出: Tuesday Sunday #include #include using namespace std; 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,ans; char mon[20]; while(cin>>day>>mon>>year) { int i; 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< } return 0; } 9、 题目描述: 输入年、月、日,计算该天是本年的第几天。 输入: 包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。 输出: 输入可能有多组测试数据,对于每1组测试数据, 输出1个整数,代表Input中的年、月、日对应本年的第几天。 样例输入: 1990 9 20 2000 5 1 样例输出: 263 122 #include using namespace std; bool fun(int year,int month) { if((year%400==0||(year%4==0&&year%100!=0))&&month>2)return true;//是闰年且月份大于2月 else return false; } 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:break; } sum+=d; if(fun(y,m))sum++; cout< } return 0; } 10、 题目描述: 给出年分m和1年中的第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 #include #include void showFormatdate(int, int); int main() { int year, dates; while(scanf("%d %d", &year, &dates) != EOF) { showFormatdate(year, dates); } } void showFormatdate(int year, int dates) { int sum, i, m, d; 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 { break; } } //获取月份 m = i; //获取当月的天数 d = month[i - 1] - (sum - dates); //格式化输出 printf("%04d-%02d-%02d\n",year, m, d); } 11、 题目描述: 读入N名学生的成绩,将获得某1给定分数的学生人数输出。 输入: 测试输入包含若干测试用例,每个测试用例的格式为 第1行:N 第2行:N名学生的成绩,相邻两数字用1个空格间隔。 第3行:给定分数 当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含)0到100之间的1个整数。 输出: 对每个测试用例,将获得给定分数的学生人数输出。 样例输入: 3 80 60 90 60 2 85 66 0 5 60 75 90 55 75 75 0 样例输出: 1 0 2 #include #include #include using namespace std; double t[1005],a; int N; int main() { int i; while(cin>>N && N) { int ans = 0; for(i = 0; i cin>>t[i]; cin>>a; for(i = 0; i if(t[i] == a)ans++; cout< } return 0; } 12、 题目描述: 输入1个ip地址串,判断是否合法。 输入: 输入的第1行包括1个整数n(1<=n<=500),代表下面会出现的IP地址的个数。 接下来的n行每行有1个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 using namespace std; int main(){ int n; int p1,p2,p3,p4; while (cin>>n) { for(int i=0;i { char c; 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大的数。 输入: 每组测试数据有两行,第1行有两个数n,m(0 输出: 对每组测试数据按从大到小的顺序输出前m大的数。 样例输入: 5 3 3 -35 92 213 -644 样例输出: 213 92 3 #include #include #include using namespace std; #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 = index; int l = left(i), r = right(i); if (l < n) { i = a[i] < a[l] ? i : l; } if (r < n) { i = a[i] < a[r] ? i : r; } if (i != index) { std::swap(a[i], a[index]); index = i; } else { break; } } } void make_heap(int a[], int n) { for (int i = parent(n - 1); i >= 0; --i) { heapify(a, n, i); } } int main() { int n, m; while (scanf("%d %d", &n, &m) != EOF) { int *a = new int[m]; for (int i = 0; i < m; ++i) { scanf("%d", &a[i]); } make_heap(a, m); int num; for (int i = m; i < n; ++i) { scanf("%d", &num); if (num > a[0]) { a[0] = num; heapify(a, m, 0); } } sort(a, a + m, greater printf("%d", a[0]); for (int i = 1; i < m; ++i) { printf(" %d", a[i]); } printf("\n"); } return 0; } 14、题目描述: “臭味相投”——这是我们描述朋友时喜欢用的词汇。两个人是朋友通常意味着他们存在着许多共同的兴趣。然而作为1个宅男,你发现自己与他人相互了解的机会并不太多。幸运的是,你意外得到了1份北大图书馆的图书借阅记录,于是你挑灯熬夜地编程,想从中发现潜在的朋友。 首先你对借阅记录进行了1番整理,将N个读者依次编号为1,2,…,N,将M本书依次编号为1,2,…,M。同时,按照“臭味相投”的原则,和你喜欢读同1本书的人,就是你的潜在朋友。你现在的任务是从这份借阅记录中计算出每个人有几个潜在朋友。 输入: 每个案例第1行两个整数N,M,2 <= N ,M<= 200。接下来有N行,第i(i = 1,2,…,N)行每1行有1个数,表示读者i-1最喜欢的图书的编号P(1<=P<=M) 输出: 每个案例包括N行,每行1个数,第i行的数表示读者i有几个潜在朋友。如果i和任何人都没有共同喜欢的书,则输出“BeiJu”(即悲剧,^ ^) 样例输入: 4 5 2 3 2 1 样例输出: 1 BeiJu 1 BeiJu #include #include using namespace std; int main(void) { multiset 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" << endl; } else { cout << record、count(ss[i]) 1 << endl; } } record、clear(); } return 0; } 15、 题目描述: 将M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同1种分法。 输入: 第1行是测试数据的数目t(0 <= t <= 20)。以下每行均包含二个整数M和N,以空格分开。1<=M,N<=10。 输出: 对输入的每组数据M和N,用1行输出相应的K。 样例输入: 1 7 3 样例输出: 8 #include using namespace std; 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; int t; cin>>t; while(t--) { cin>>m>>n; cout< } return 0; } 16、 有1个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的1个线段,起点是坐标原点,在每个整数坐标点有1棵树,即在0,1,2,、、、,L共L+1个位置上有L+1棵树。 现在要移走1些树,移走的树的区间用1对数字表示,如 100 200表示移走从100到200之间(包括端点)所有的树。 可能有M(1<=M<=100)个区间,区间之间可能有重叠。现在要求移走所有区间的树之后剩下的树的个数。 输入: 两个整数L(1<=L<=10000)和M(1<=M<=100)。 接下来有M组整数,每组有1对数字。 输出: 可能有多组输入数据,对于每组输入数据,输出1个数,表示移走所有区间的树之后剩下的树的个数。 样例输入: 500 3 100 200 150 300 470 471 #include #define MAXLEN 10001 int main(){ #ifdef ONLINE_JUDGE #else freopen("E:\\in、txt", "r", stdin); #endif int L[MAXLEN]; int l, m; while (scanf ("%d%d", &l, &m) != EOF){ for (int i = 0; i <= l; i++){ //注意i的范围是i<=l not i L[i] = 1; }// 初始化,种l+1棵树 int a, b; for (int i = 1; i <= m; i++){ scanf("%d%d", &a, &b); for(int j = a; j <= b; j++){ L[j] = 0; }// 移走 }// m组 int left=0; for (int i = 0; i <= l; i++){ if (L[i] == 1) left++; } printf("%d\n", left); }// while:zu return 0; } 17、 题目描述: 输入1个高度h,输出1个高为h,上底边为h的梯形。 输入: 1个整数h(1<=h<=1000)。 输出: h所对应的梯形。 样例输入: 4 样例输出: **** ****** ******** ********** #include #include int main(void) { int h; while(scanf("%d",&h) != EOF) { int i,j; for(i = 1; i <= h; ++i) { for(j = 1; j <= 2 * h - 2 * i; ++j) printf(" "); for(j = 2 * h - 2 * i + 1; j <= 3 * h - 2; ++j) printf("*"); printf("\n"); } } return 0; } 18、 题目描述: 将1个个大小差1圈的筐叠上去,使得从上往下看时,边筐花色交错。这个工作现在要让计算机来完成,得看你的了。 输入: 输入是1个个的三元组,分别是,外筐尺寸n(n为满足0 输出: 输出叠在1起的筐图案,中心花色与外筐花色字符从内层起交错相叠,多筐相叠时,最外筐的角总是被打磨掉。叠筐与叠筐之间应有1行间隔。 样例输入: 11 B A 5 @ W 样例输出: AAAAAAAAA ABBBBBBBBBA ABAAAAAAABA ABABBBBBABA ABABAAABABA ABABABABABA ABABAAABABA ABABBBBBABA ABAAAAAAABA ABBBBBBBBBA AAAAAAAAA #include #include int main(){ int n; char pic[80][80]; char first, second; while(scanf("%d %c %c", &n, &first, &second) != EOF){ int min, max, floor = 0; for(min = max = n / 2 ;min>=0&&max { char c = floor%2==0? first:second; for(int j = min; j<=max; 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< n; i ++){ for(int j = 0 ; j < n; 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 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 #include #include #include #include #include #include #include using namespace std; void shape_str2char(string src[] , char* dest[], int x, int y, int n){ for(int i=0; i for(int j=0; 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 for(int j=0; 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 for(int j=0; j if(templ[i][j]!=' ') shape_recur(depth-1, templ, dest, x+i*pow(double(size), depth-1), y+j*pow(double(size), depth-1), size); } } int main(){ int size_templ; while(scanf("%d", &size_templ) && size_templ!=0){ //sp1 while(getchar() != '\n') continue; char* str[6]; for(int i=0; i<6; i++) str[i] = new char[6]; for(int i=0; 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 int num; 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 for(int j=0; j printf("%c", Pic[i][j]); printf("\n"); } for(int i=0; i<6; i++) delete(str[i]); for(int i=0; i<3001; i++) delete(Pic[i]); } } 20、 题目描述: 输入1个数n,然后输入n个数值各不相同,再输入1个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。 输入: 测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。 输出: 对于每组输入,请输出结果。 样例输入: 2 1 3 0 样例输出: -1 #include int main() { int n; while (scanf("%d",&n)!=EOF) { int x,a[200],fla=0; for (int i=0;i { scanf("%d",&a[i]); } scanf("%d",&x); for (int i=0;i { if (a[i]==x) { printf("%d\n",i); fla=1; break; } } if (fla==0) { printf("-1\n"); } } return 0; } 21、 题目描述: 输入N个学生的信息,然后进行查询。 输入: 输入的第1行为N,即学生的个数(N<=1000) 接下来的N行包括N个学生的信息,信息格式如下: 01 李江 男 21 02 刘唐 男 23 03 张军 男 19 04 王娜 女 19 然后输入1个M(M<=10000),接下来会有M行,代表M次查询,每行输入1个学号,格式如下: 02 03 01 04 输出: 输出M行,每行包括1个对应于查询的学生的信息。 如果没有对应的学生信息,则输出“No Answer!” 样例输入: 4 01 李江 男 21 02 刘唐 男 23 03 张军 男 19 04 王娜 女 19 5 02 03 01 04 03 样例输出: 02 刘唐 男 23 03 张军 男 19 01 李江 男 21 04 王娜 女 19 03 张军 男 19 #include #include #include #include #include using namespace std; int main() { string id,info,str; int i,index,n,M; map map // fstream cin("1069、txt"); while (cin>>n) { cin、ignore(); m、clear(); for (i=0;i { getline(cin,str); index=str、find(" "); id=str、substr(0,index); info=str、substr(index+1); m、insert(pair } cin>>M; for (i=0;i { cin>>id; iter=m、find(id); if (iter!=m、end()) { cout< } else { cout<<"No Answer!"< } } } return 0; } 22、 题目描述: 在1个整数数组上,对于下标为i的整数,如果它大于所有它相邻的整数, 或者小于所有它相邻的整数,则称为该整数为1个极值点,极值点的下标就是i。 输入: 每个案例的输入如下: 有2×n+1行输入:第1行是要处理的数组的个数n; 对其余2×n行,第1行是此数组的元素个数k(4 输出: 每个案例输出为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 样例输出: 0 7 2 3 4 5 6 10 12 0 2 3 10 12 14 #include #include using namespace std; int main() { int n; while(cin >> n) { while(n--) { int k,i; cin >> k; vector for(i = 0;i < k;i++) cin >> vv[i]; vector 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] << endl; } } 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 1 5 2 4 3 3 2 5 6 样例输出: YES YES NO #include #include #include using namespace std; int main() { int temp,i,n,m; vector while (cin>>n) { for (i=0;i { cin>>temp; v、push_back(temp); } cin>>m; for (i=0;i { cin>>temp; vector 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、 样例输入: 5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1 样例输出: 13、333 31、500 #include #include #include using namespace std; struct Room { int beans; int cost; double ratio; }; struct Cmp { bool operator()(const Room &a, const Room &b) { return a、ratio > b、ratio; } }; int main() { Cmp cmp; int food, n; while (scanf("%d %d", &food, &n) != EOF && food != -1) { vector for (int i = 0; i < n; ++i) { Room room; scanf("%d %d", &room、beans, &room、cost); room、ratio = room、beans / (double)room、cost; 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、cost; beans += room、beans; ++index; } else { double ratio = food / (double)room、cost; beans += room、beans * ratio; break; } } printf("%、3f\n", beans); } return 0; } 24、 题目描述: “今年暑假不AC?”“是的。”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%、、、”确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视作为球迷,1定想看尽量多的完整的比赛,当然,作为新时代的好青年,你1定还会看1些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目) 输入: 输入数据包含多个测试实例,每个测试实例的第1行只有1个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用1个正整数表示。n=0表示输入结束,不做处理。 输出: 对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占1行。 样例输入: 12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 0 样例输出: 5 #include #include using namespace std; struct ss{ int st,ed; }node[102]; bool cmp(ss x,ss y){ if(x、ed!=y、ed) return x、ed else return x、st>y、st; } int main(){ int n; while(scanf("%d",&n)!=EOF){ if(n==0) return 0; else{ for(int i=0;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 if(node[j]、st>=node[k]、ed){ num++; k=j; } } printf("%d\n",num); } } } 24、 题目描述: 通过悬崖的yifenfei,又面临着幽谷的考验—— 幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅。由于此处长年不见天日,导致空气中布满了毒素,1旦吸入体内,便会全身溃烂而死。 幸好yifenfei早有防备,提前备好了解药材料(各种浓度的万能药水)。现在只需按照配置成不同比例的浓度。 现已知yifenfei随身携带有n种浓度的万能药水,体积V都相同,浓度则分别为Pi%。并且知道,针对当时幽谷的瘴气情况,只需选择部分或者全部的万能药水,然后配置出浓度不大于 W%的药水即可解毒。 现在的问题是:如何配置此药,能得到最大体积的当前可用的解药呢? 特别说明:由于幽谷内设备的限制,只允许将1种已有的药全部混入另1种之中(即:不能出现对1种药只取它的1部分这样的操作)。 输入: 输入数据的第1行是1个整数C,表示测试数据的组数; 每组测试数据包含2行,首先1行给出三个正整数n,V,W(1<=n,V,W<=100); 接着1行是n个整数,表示n种药水的浓度Pi%(1<=Pi<=100)。 输出: 对于每组测试数据,请输出1个整数和1个浮点数; 其中整数表示解药的最大体积,浮点数表示解药的浓度(四舍五入保留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 #include #include #include #include using namespace std; int main() { int c; cin >> c; for(;c>0;c--) { int n,v,w; cin >> n >> v >> w; int buf[100]; for(int i=0;i cin >> buf[i]; sort(buf,buf+n);//解药按浓度排序 int currW = 0; int currV = 0; for(int i=0;i { if(buf[i] <= w) { currW = (buf[i]*v + currW); currV += v; } else if((buf[i]*v + currW) <= w*(currV+v)) { currW = currW + buf[i]*v; currV += v; } else break; } cout << currV << " 0、"; currW = floor((double)currW/currV + 0、5); if(currV == 0)currW = 0; printf("%02d",currW); cout << endl; } //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 mentioned above、 In the second line, there are N positive integers、 The ith integer Ai(0 输出: 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、 样例输入: 5 3 3 2 1 5 2 2 1 样例输出: 2 Impossible #include #include using namespace std; 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 { scanf("%lld",&A[i]); } sort(A,A+N,cmp); bool flag=true; int sum=0,k=0; for(i=0;i { if((sum+A[i]) { k++; sum+=A[i]; } else { k++; flag=false; break; } } 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 capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; 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 1300 12 8 6、00 1250 7、00 600 7、00 150 7、10 0 7、20 200 7、50 400 7、30 1000 6、85 300 50 1300 12 2 7、10 0 7、00 600 样例输出: 749、17 The maximum travel distance = 1200、00 #include #include #include #define MAXN 501 #define MAXC 30000000、0 typedef struct station{ float price; int dist; }Station; int compare(const void * p, const void * q){ Station * p1 = (Station *)p; Station * q1 = (Station *)q; return p1->dist - q1->dist; } int main(void){ int Cmax, D, Davg, N; //ÈÝÁ¿¡¢¾àÀ롢ÿµ¥Î»ÆøÐÐÊ»µÄ¾àÀë¡¢¼ÓÆøÕ¾×ÜÊý int i; Station sta[MAXN]; float sum, remind_gas, tmp; int k, step; while (scanf("%d %d %d %d", &Cmax, &D, &Davg, &N) != EOF){ for (i=0; i scanf("%f %d", &sta[i]、price, &sta[i]、dist); } sta[N]、dist = D; sta[N]、price = 1000000、0; qsort(sta, N, sizeof(Station), compare); //°´Ó뺼ÖݾàÀë´óС¸ø¼ÓÆøÕ¾ÅÅÐò if (sta[0]、dist > 0){ printf ("The maximum travel distance = 0、00\n"); continue; } sum = 0; //×Ü·ÑÓà step = Cmax*Davg; //¼ÓÂúÓÍÐÐÊ»×î´ó¾àÀë remind_gas = 0; //Ê£ÓàÓÍÁ¿ for (i=0; i k = i+1; if (i != 0) remind_gas -= ((float)(sta[i]、dist -sta[i-1]、dist))/Davg; for (; k continue; if (sta[k]、dist-sta[i]、dist > step){ sum += (Cmax-remind_gas)*sta[i]、price; remind_gas = Cmax; } else{ tmp = ((float)(sta[k]、dist-sta[i]、dist))/Davg - remind_gas; if (fabs(tmp)>1e-5 && tmp>0){ sum += tmp*sta[i]、price; remind_gas = ((float)(sta[k]、dist-sta[i]、dist))/Davg; } } if (sta[i+1]、dist - sta[i]、dist > step){ printf ("The maximum travel distance = %、2f\n", (float)(sta[i]、dist+step)); break; } } if (i == N){ printf ("%、2f\n", sum); } } return 0; } 27、 题目描述: 在某个string(长度不超过100)中有左括号、右括号和大小写字母;规定(与常见的算数式子1样)任何1个左括号都从内到外与在它右边且距离最近的右括号匹配。写1个程序,找到无法匹配的左括号和右括号,输出原来string,并在下1行标出不能匹配的括号。不能匹配的左括号用"$"标注,不能匹配的右括号用"?"标注、 输入: 输入包括多组数据,每组数据1行,包含1个string,只包含左右括号和大小写字母,string长度不超过100。 注意:cin、getline(str,100)最多只能输入99个字符! 输出: 对每组输出数据,输出两行,第1行包含原始输入字符,第2行由"$","?"和空格组成,"$"和"?"表示与之对应的左括号和右括号不能匹配。 样例输入: )(rttyy())sss)( 样例输出: )(rttyy())sss)( ? ?$ #include #include #include #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!=len;++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; break; } } } } puts(exp); for(int i=0;i!=len;++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(' '); break; case 1: putchar('$'); break; case 2: putchar('?'); break; } } printf("\n"); } return 0; } 28、 题目描述: 读入1个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。 输入: 测试输入包含若干测试用例,每个测试用例占1行,每行不超过200个字符,整数和运算符之间用1个空格分隔。没有非法表达式。当1行中只有0时输入结束,相应的结果不要输出。 输出: 对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。 样例输入: 1 + 2 4 + 2 * 5 - 7 / 11 0 样例输出: 3、00 13、36 #include #include #include #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) { char ch; (*len) = 0; while ('\n' != (ch=getchar()))/*输入处理*/ { if (isfu(ch)||isdig(ch)) s[(*len)++] = ch; } 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,now; double dig[size]; while (mygets(str,&len)) { if (len == 0) continue; nd = nf = now = 0; for (i = 0; i < len; ) { if (isfu(str[i])) { fu[nf++] = str[i]; if ('*' == str[i] || '/' == str[i]) now = 1;/*得到下1个数后马上计算*/ i++; } else { total = 0; while (str[i] >= '0' && str[i] <= '9') { total *= 10; total += (str[i] - '0') ; i++; } dig[nd++] = (double)total; 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 { 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、 题目描述: 堆栈是1种基本的数据结构。堆栈具有两种基本操作方式,push 和 pop。Push1个值会将其压入栈顶,而 pop 则会将栈顶的值弹出。现在我们就来验证1下堆栈的使用。 输入: 对于每组测试数据,第1行是1个正整数 n,0 输出: 对于每组测试数据,根据其中的命令字符来处理堆栈;并对所有的'A’操作,输出当时栈顶的值,每个占据1行,如果当时栈为空,则输出'E’。当每组测试数据完成后,输出1个空行。 样例输入: 3 A P 5 A 4 P 3 P 6 O A 0 样例输出: E 5 3 #include #include #include #include using namespace std; int main() { int i,n,temp; char ch; vector stack // fstream cin("1108、txt"); while (cin>>n,n) { v、clear(); for (i=0;i { cin>>ch; switch(ch) { case 'A': if (v、empty()) { cout<<"E"< } else { cout< } break; case 'P': { cin>>temp; v、push_back(temp); } break; case 'O': { if (!v、empty()) { v、pop_back(); } } break; } } cout< } return 0; } 30、 题目描述: 对于1个不存在括号的表达式进行计算 输入: 存在多种数据,每组数据1行,表达式不存在空格 输出: 输出结果 样例输入: 6/2+3+3*4 样例输出: 18 #include #include using namespace std; string str; int pos; double getNum(){ double v = 0; for(;pos if(str[pos]>'9'||str[pos]<'0'){ break; } v *= 10; v += str[pos] - '0'; } return 1、0*v; } int main(int argc,char* argv[]){ double a,b; double v; while(cin>>str){ stack pos = 0; s、push(getNum()); while(pos 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< } return 0; } 31、 题目描述: 哈夫曼树,第1行输入1个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和。 输入: 输入有多组数据。 每组第1行输入1个数n,接着输入n个叶节点(叶节点权值不超过100,2<=n<=1000)。 输出: 输出权值。 样例输入: 5 1 2 2 5 9 样例输出: 37 #include #include using namespace std; int main() { int n; while(cin>>n){ int sum=0; int a[n]; for(int i=0;i cin>>a[i]; for(int i=1;i { sort(a+i-1,a+n); sum=a[i]+a[i-1]+sum; a[i]=a[i]+a[i-1]; } cout< } return 0; } 32、 题目描述: 在1个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆,小明决定将所有的水果合成1堆。每1次合并,小明可以将两堆水果合并到1起,消耗的体力等于两堆水果的重量之和。当然经过 n‐1 次合并之后,就变成1堆了。小明在合并水果时总共消耗的体力等于每次合并所耗体力之和。 假定每个水果重量都为 1,并且已知水果的种类数和每种水果的数目,你的任务是设计出合并的次序方案,使小明耗费的体力最少,并输出这个最小的体力耗费值。示例有 3 种水果,数目依次为 1,2,9。可以先将 1,2 堆合并,新堆数目为3,耗费体力为 3。然后将新堆与原先的第三堆合并得到新的堆,耗费体力为 12。所以小明总共耗费体力=3+12=15,可以证明 15 为最小的体力耗费值。 输入: 每组数据输入包括两行,第1行是1个整数 n(1<=n<=10000),表示水果的种类数,如果 n 等于 0 表示输入结束,且不用处理。第2行包含 n 个整数,用空格分隔,第 i 个整数(1<=ai<=1000)是第 i 种水果的数目。 输出: 对于每组输入,输出1个整数并换行,这个值也就是最小的体力耗费值。输入数据保证这个值小于 2^31。 样例输入: 3 9 1 2 0 样例输出: 15 #include using namespace std; int main(int argc,char* argv[]) { int n; int a[10000]; int temp; int i,j; int t,k; int sum; while(cin>>n&&n!=0) { sum=0; for(i=0;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]=temp; //a[t]为第1个最小值 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]=temp; //a[t-1]为第2个最小值 a[t-1]+=a[t]; t--; sum+=a[t]; } cout< } return 0; } 33、 题目描述: 二叉树的前序、中序、后序遍历的定义: 前序遍历:对任1子树,先访问跟,然后遍历其左子树,最后遍历其右子树; 中序遍历:对任1子树,先遍历其左子树,然后访问根,最后遍历其右子树; 后序遍历:对任1子树,先遍历其左子树,然后遍历其右子树,最后访问根。 给定1棵二叉树的前序遍历和中序遍历,求其后序遍历(提示:给定前序遍历与中序遍历能够唯1确定后序遍历)。 输入: 两个string,其长度n均小于等于26。 第1行为前序遍历,第2行为中序遍历。 二叉树中的结点名称以大写字母表示:A,B,C、、、、最多26个结点。 输出: 输入样例可能有多组,对于每组测试样例, 输出1行,为后序遍历的string。 样例输入: ABC BAC FDXEAG XDEFAG 样例输出: BCA XEDGAF #include #include #include char s1[101],s2[101]; int a[101]; int k; 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] = j; } } fen(s2,0,strlen(s2) - 1); printf("\n"); } } void fen(char *s2,int l,int h){ int n; if(h < l) return ; if(h - l > 0) k++; n = a[k]; if(l == h) { printf("%c",s2[h]); k++; return ; } fen(s2,l,n-1); fen(s2,n+1,h); printf("%c",s2[n]); } 34、 题目描述: 如上所示,由正整数1,2,3……组成了1颗特殊二叉树。我们已知这个二叉树的最后1个结点是n。现在的问题是,结点m所在的子树中1共包括多少个结点。 比如,n = 12,m = 3那么上图中的结点13,14,15以及后面的结点都是不存在的,结点m所在子树中包括的结点有3,6,7,12,因此结点m的所在子树中共有4个结点。 输入: 输入数据包括多行,每行给出1组测试数据,包括两个整数m,n (1 <= m <= n <= 1000000000)。最后1组测试数据中包括两个0,表示输入的结束,这组数据不用处理。 输出: 对于每1组测试数据,输出1行,该行包含1个整数,给出结点m所在子树中包括的结点的数目。 样例输入: 3 12 0 0 样例输出: 4 #include #include int main() { unsigned long n,sum=0,d; while(scanf("%d",&d)) { unsigned long i; scanf("%d",&n); if (d==0&&n==0) break; unsigned long ddeep; 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");break; } else { unsigned long j,k; k=d; unsigned long count=0; for(i=ddeep; 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、 题目描述: 有1棵树,输出某1深度的所有节点,有则输出这些节点,无则输出EMPTY。该树是完全二叉树。 输入: 输入有多组数据。 每组输入1个n(1<=n<=1000),然后将树中的这n个节点依次输入,再输入1个d代表深度。 输出: 输出该树中第d层得所有节点,节点间用空格隔开,最后1个节点后没有空格。 样例输入: 4 1 2 3 4 2 样例输出: 2 3 #include #include #include #include #include #include using namespace std; int main() { int i,n,d,temp,sum1,sum2; vector // fstream cin("1176、txt"); while (cin>>n) { v、clear(); for (i=0;i { cin>>temp; 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 { cout< for (i=sum1+1;i { cout<<" "< } } else if (n>=sum2) { cout< for (i=sum1+1;i { cout<<" "< } } cout< } return 0; } 36、 题目描述: 输入1系列整数,建立二叉排序数,并进行前序,中序,后序遍历。 输入: 输入第1行包括1个整数n(1<=n<=100)。 接下来的1行包括n个整数。 输出: 可能有多组测试数据,对于每组数据,将题目所给数据建立1个二叉排序树,并对二叉排序树进行前序、中序和后序遍历。 每种遍历结果输出1行。每行最后1个数据之后有1个空格。 样例输入: 5 1 6 5 9 8 样例输出: 1 6 5 9 8 1 5 6 8 9 5 8 9 6 1 #include using namespace std; typedef struct Node { int data; struct Node *leftchild; struct Node *rightchild; }BTree,*Blink; //前序遍历 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); delete head; } } //建立二叉排序树 void buildBinarySortTree(Blink &head, int elem[], int length) { Blink p,pre; int flagLeftOrRight; //0表示左孩子,1表示右孩子 int success; head = new BTree; //生成头结点 head->data = elem[0]; head->leftchild = NULL; head->rightchild = NULL; for(int i = 1; i < length; ++i) { success = 0; pre = NULL; p = head; while(p) //寻找插入的位置 { pre = p; if(p->data == elem[i]) //查找成功 { success = 1; break; } else if(p->data < elem[i]) //沿右孩子搜索 { p = p->rightchild; flagLeftOrRight = 1; } else //沿左孩子搜索 { p = p->leftchild; flagLeftOrRight = 0; } } if(!success) { p = new BTree; //生成新结点 p->data = elem[i]; p->leftchild = NULL; p->rightchild = NULL; if(flagLeftOrRight == 0) //新插入的结点作为左孩子 { pre->leftchild = p; } else if(flagLeftOrRight == 1) //新插入的结点作为右孩子 { pre->rightchild = p; } } } } int main() { int N; int elem[110]; Blink head; while(cin >> N) { for(int i = 0; i < N; ++i) { cin >> elem[i]; } buildBinarySortTree(head,elem,N); PreOrder(head); cout << endl; InOrder(head); cout << endl; PostOrder(head); cout << endl; } return 0; } 37、 题目描述: 判断两序列是否为同1二叉搜索树序列 输入: 开始1个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。 接下去1行是1个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出1颗二叉搜索树。 接下去的n行有n个序列,每个序列格式跟第1个序列1样,请判断这两个序列是否能组成同1颗二叉搜索树。 输出: 如果序列相同则输出YES,否则输出NO 样例输入: 2 567432 543267 576342 0 样例输出: YES NO #include using namespace std; int a[1024]; int b[1024]; void createtree(string s,int c[]) { int len=s、length(); for(int i=0;i { int temp=s[i]-'0'; for(int j=1;j<=1023;) { if(c[j]==-1) {c[j]=temp;break;} else if(c[j]>temp) j=j*2; else j=j*2+1; } } } int main() { int n,i; string s; string t; 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]) break; if(i==1024) cout<<"YES"< else cout<<"NO"< } } return 0; } 38、 题目描述: 读入两个小于10000的正整数A和B,计算A+B。需要注意的是:如果A和B的末尾K(不超过8)位数字相同,请直接输出-1。 输入: 测试输入包含若干测试用例,每个测试用例占1行,格式为"A B K",相邻两数字有1个空格间隔。当A和B同时为0时输入结束,相应的结果不要输出。 输出: 对每个测试用例输出1行,即A+B的值或者是-1。 样例输入: 1 2 1 11 21 1 108 8 2 36 64 3 0 0 1 样例输出: 3 -1 -1 100 #include #include #include #include using namespace std; void split(int num,int k,char *str) { int i; |