3039. 按整数最高位的值排序

一串萌萌哒的代码

注意qsort的cmp函数为int,大数不能直接return

include

include

long long abs(long long k){
if(k<0){
k=-k;
}
return k;
}
typedef struct{
long long num;
long long numabs;
long long c;
}sort;
int cmp(const void x,const void y){
sort a=(sort )x;
sort b=(sort )y;
if(a->c==b->c){
return a->num>b->num? 1:-1 ;
}
return b->c-a->c;
}
int main(){
int t,d=0;
scanf(“%d”,&t);
while(t>0){
int n,i;
scanf(“%d”,&n);
sort k[10000];
for(i=0;i<n;i++){
scanf(“%lld”,&k[i].num);
k[i].numabs=abs(k[i].num);
long long p=k[i].numabs;
while(1){
if(p<10){
k[i].c=p;
break;
}
p=p/10;
}
}
qsort(k,n,sizeof(k[0]),cmp);
printf(“case #%d:\n%lld”,d,k[0].num);

    for(i=1;i<n;i++){
        printf(" %lld",k[i].num);
    }
    printf("\n");
    t--;d++;
}

}

zzpzbl

厉害了我的昊宇!

Stevenaa

python sorted(iterable,key,reverse)
key太香了(doge)

kukusugar
#include <iostream>
#include <algorithm>
using namespace std;
struct hello{
    long long int p,w;
}num[10010];
bool cmp(hello a,hello b){
    if(a.p!=b.p)return a.p>b.p;
    else return a.w<b.w;
}
int main(){
    long long int n,m,cos,i,j,x;
    cin>>n;
    for(cos=0;cos<n;cos++){
        cin>>m;
        for(i=0;i<m;i++){
            cin>>x;
            num[i].w=x;
            if(x<0)x=-x;
            num[i].p=x%10;
            x/=10;
            while(x>0){
                num[i].p=x%10;
                x/=10;
            }
        }
        sort(num,num+m,cmp);
        cout<<"case #"<<cos<<":"<<endl;
        for(j=0;j<m;j++){
            cout<<num[j].w;
            if(j<m-1)cout<<' ';
        }
        cout<<endl;
    }
}
e_mmmmmm

int GetFirsst(ll n){
if(n<0)
n=abs(n);
while(n>=10)
n/=10;
return int(n);
}

bool cmp(ll a,ll b){
int a_f=GetFirsst(a);
int b_f=GetFirsst(b);
if(a_f==b_f)
return ab_f;
}

int main(int argc, const char * argv[]) {
int cas;
scanf(“%d”,&cas);
for(int i=0;i<cas;i++){
int n;
scanf(“%d”,&n);
ll a[maxn];
for(int j=0;j<n;j++)
scanf(“%lld”,&a[j]);
sort(a, a+n, cmp);
printf(“case #%d:\n”,i);
for(int j=0;j<n-1;j++)
printf(“%lld “,a[j]);
printf(“%lld\n”,a[n-1]);
}
return 0;
}

星渊龙骑

include

int main(){
int T;
scanf(“%d”,&T);
for(int i=0;i<T;i++){
int N;
scanf(“%d”,&N);
long long a[N],b[N];
for(int j=0;j=0){
b[j]=a[j];
}
else if(a[j]<0){
b[j]=-a[j];
}
while(b[j]>9){
b[j]/=10;
}
}
for(int j=0;j<N;j++){
for(int k=j+1;k<N;k++){
if(b[j]a[k]){
long long tempa;
tempa=a[j];
a[j]=a[k];
a[k]=tempa;;
}
}
}
printf(“case #%d:\n”,i);
for(int j=0;j<N;j++){
printf(“%lld “,a[j]);
}

    printf("\n");
}

}

会奔跑的bug

for i in range(int(input())):
N = int(input())
print(‘case #’ + str(i) + ‘:’)
all_nums = []
for one_num in input().split(‘ ‘):
if int(one_num) < 0:
one_s = int(one_num[1])
else:
one_s = int(one_num[0])
all_nums.append([int(one_num),one_s])
all_nums.sort(key=lambda x:x[0])
all_nums.sort(key=lambda x:x[1],reverse=True)
p_num = [str(one_numss[0]) for one_numss in all_nums]
print(‘ ‘.join(p_num))

Li Dao

这题用cin cout会超时,std::ios::sync_with_stdio(false)竟然MLE了,改回了scanf

include

using namespace std;

define LL long long

int T,n;
vector V;
int top(LL aa)
{
aa=abs(aa);
while(aa>=10) aa/=10;
return aa;
}
int cmp(LL aa,LL bb)
{
int ta=top(aa),tb=top(bb);
if(ta!=tb) return ta>tb;
else return aa<bb;
}
void solve()
{
V.clear();
scanf(“%d”,&n);

for(int i=1;i<=n;i++)
{
LL xx;
scanf(“%lld”,&xx);
V.push_back(xx);
}
sort(V.begin(),V.end(),cmp);
int ff=1;
for(int i=0;i<V.size();i++)
{
if(ff) ff=0; else printf(” “);
printf(“%lld”,V[i]);
}
printf(“\n”);
return;
}
int main()
{
scanf(“%d”,&T);
for(int step=0;step<T;step++)
{
printf(“case #%d:\n”,step);
solve();
}
return 0;
}

10175101245

我用python都没超时……

Li Dao

辣是真的牛批

Fifnmar

嗯?我竟然没发代码。

/*******************************************************
 * 0. Encoding:
 *     This file is encoded with UTF-8. If you see any
 *     gibberish code, reopen this file with UTF-8.
 * 
 * 1. 关于 I/O 的一个约定:
 *     C++ 的格式化输出比较麻烦,所以我使用 C 的输出和 C++
 *     的输入。默认已经写了下面两行:
 *     ```cpp
 *     ios::sync_with_stdio(false);
 *     cin.tie(nullptr);
 *     ```
 * 
 * 2. 关于大括号:
 *     一律不省略大括号。
 * 
 * 3. 关于声明和输入在同一行的问题:
 *     这是个人审美啦。我觉得它们如此地强关联,应该放在一行才好看。
 * 
 * 4. 关于注释:
 *     我当时写代码的时候几乎没有写注释,所以回头再加注释可能
 *     解释得不够明白。如果有问题或意见,欢迎联系我呀!
 * 
 * @ QQ:     1390770341
 * @ WeChat: bldsjx
 * @ E-mail: deuchie@foxmail.com
 * 
*******************************************************/
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

// 由于要比较首位数字,而对整型取得首位数字不方便,所以我使用 string。
// 但这样有一个问题,负数的第一个字符是 '-',所以要特别对待。
int main() {
    using namespace std;
    uint32_t t; cin >> t;
    for (uint32_t query = 0; query < t; ++query) {
        printf("case #%u:\n", query);
        uint32_t n; cin >> n;
        // 这个特别判断被我移到下面了,大家自行取舍吧。
        // if (n == 0) {
        //     putchar('\n');
        //     continue;
        // }
        vector<string> nums(n);
        for (auto &i : nums) {
            cin >> i;
        }
        sort(nums.begin(), nums.end(), [](string const& a, string const& b) {
            auto ach = a[0] == '-' ? a[1] : a[0];
            auto bch = b[0] == '-' ? b[1] : b[0];
            if (ach == bch) {
                // 偷懒的写法。如果真的要效率应该写个更高效的,或者至少
                // 记录下来对应的数字而不是每次比较都算一遍。
                // `stoull` means "string to unsigned long long".
                return stoll(a) < stoll(b);
            } else {
                return ach > bch;
            }
        });
        if (n != 0) {
            printf("%s", nums[0].data());
            for (uint32_t i = 1, sz = nums.size(); i != sz; ++i) {
                printf(" %s", nums[i].data());
            }
        }
        putchar('\n');
    }
}
Fifnmar

现在我的代码风格已经发生了变化……

比如我开始用 cout 了。

这只是权宜之计,等 C++20 完全支持了当然要用 {fmt}

Charles__Deng

这题就是处理long long要稳一点就好勒~

你当前正在回复 博客/题目
存在问题!