P1781 宇宙总统
原题题意
思路
简单对字符串大小进行比较:首先比较位数,毫无疑问位数大的代表的数字更大,如果位数相等的话,再依次比较字符串s和ans的各个位的大小
AC代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
#define MAX_INT 2147483647
using namespace std;
int flag = 0;
int n;
typedef struct{
int score;
int no;
int chinese;
}Stu;
void swap(int *a,int *b){
int tmp = *a;
*a = *b;
*b = tmp;
}
int cmp(const void *a,const void *b){
return (*(Stu*)a).score < (*(Stu *)b).score? 1:-1;
}
int main(){
scanf("%d",&n);
char ans[10001];
char s[1000];
for(int i = 1; i <= n; i++){
scanf("%s",s);
if(strlen(s) > strlen(ans)){
strcpy(ans,s);
flag = i;
}else if(strlen(s) == strlen(ans)){
for(int j = 0; j < strlen(s); j++){
if(s[j] > ans[j]){
strcpy(ans,s);
flag = i;
break;
}
if(s[j] < ans[j]){
break;
}
}
}else{
}
}
printf("%d\n%s\n",flag,ans);
return 0;
}
该博客介绍了一道编程题目,涉及字符串的比较。程序通过比较字符串的长度和字符顺序来确定哪个字符串代表的数字更大,并找出这个宇宙总统。主要考察了基本的字符串操作和条件判断。
1402

被折叠的 条评论
为什么被折叠?



