浙大PAT 1038题 1038. Recover the Smallest Number
来源:互联网 发布:钢琴谱制作软件 编辑:程序博客网 时间:2024/06/16 03:25
快排飘过,代码如下:
#include<stdio.h>#include<string.h>#include<stdlib.h>int cmp(const void *m,const void *n){ char *a=(char *)m; char *b=(char *)n; char tmpa[20],tmpb[20]; strcpy(tmpa,a); strcpy(tmpb,b); strcat(tmpa,b); strcat(tmpb,a); return strcmp(tmpa,tmpb);}char str[10005][10];int main(){ int i,j,n; scanf("%d",&n); for(i=0;i<n;i++){ scanf("%s",str[i]); } qsort(str,n,10*sizeof(char),cmp); int flag=0; for(i=0;i<n;i++){ for(j=0;str[i][j]!='\0';j++){ if(flag==0&&str[i][j]=='0'); else {flag=1;printf("%c",str[i][j]);} } } if(flag==0) printf("0");//如果所有段都为零,那么必须输出一个零 printf("\n"); return 0;}
用Java写了一下,那组万级的测试数据超时,用java做pat真是不公平啊。
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.Scanner;public class Main {public static void main(String[] args) {int i,n;List<String> strList=new ArrayList<String>();Scanner sc=new Scanner(System.in);n=sc.nextInt();for(i=0;i<n;i++){strList.add(sc.next());}Collections.sort(strList, new MyComparator());StringBuilder sb=new StringBuilder();for(String str:strList){sb.append(str);}i=0;while(i<sb.length()&&sb.charAt(i)=='0'){i++;}if(i==sb.length()){System.out.println("0");}else{for(;i<sb.length();i++){System.out.print(sb.charAt(i));}System.out.println();}}}class MyComparator implements Comparator<Object>{//返回一个基本类型的整型,返回负数表示o1 小于o2,返回0 表示o1和o2相等,返回正数表示o1大于o2。 public int compare(Object o1, Object o2) {String s1=(String)o1;String s2=(String)o2;return (s1+s2).compareTo(s2+s1);}}
- 浙大PAT 1038题 1038. Recover the Smallest Number
- 1038. Recover the Smallest Number (30)-PAT
- PAT 1038. Recover the Smallest Number
- PAT 1038. Recover the Smallest Number
- PAT 1038. Recover the Smallest Number
- PAT 1038. Recover the Smallest Number (30)
- 【PAT】1038. Recover the Smallest Number (30)
- [pat]1038. Recover the Smallest Number (30)
- pat 1038. Recover the Smallest Number (30)
- PAT 1038. Recover the Smallest Number
- 【PAT】1038. Recover the Smallest Number
- PAT 1038. Recover the Smallest Number (30)
- PAT--1038. Recover the Smallest Number
- pat 1038 Recover the Smallest Number
- pat 1038 Recover the Smallest Number
- PAT A 1038. Recover the Smallest Number (30)
- PAT (Advanced) 1038. Recover the Smallest Number (30)
- PAT(A) - 1038. Recover the Smallest Number (30)
- 鳥哥的 Linux 私房菜
- SQL中drop,delete和truncate的异同
- MFC OnCtlColor函数
- 牛腩JavaScript
- UVa 10078 - The Art Gallery
- 浙大PAT 1038题 1038. Recover the Smallest Number
- Objective-C 中的NSLog
- 网页设计页面尺寸
- 浙大PAT 1040题 1040. Longest Symmetric String
- 水果疗效
- 浙大PAT 1041题 1041. Be Unique
- 专业课133分过来人谈计算机专业考研各科目复习方法(粘贴自考研秘书网)
- poj 2406 史上最裸KMP 第三弹 给你一串字符串,问它的循环次数
- 函数重载