AC Diary--the sum of the numbers of 23--

Source: Internet
Author: User

Problem description a given n positive integers, sorted according to the sum of the numbers and from small to large. Input data has multiple groups, one row per group of data, and the first number of each row is a positive integer n, representing an integer number followed by n positive integers. When n is 0 o'clock, no processing is done and the input ends. N<=10output outputs the results of each set of sorts. Example Input
2 1 23 121 10 1110
Example Output
1 210 111 121
#include <stdio.h> #include <stdlib.h>int f (int n)//write a number that calculates the sum of the figures. {    int sum=0;    while (n!=0)    {        sum+=n%10;        N=N/10;    }    return sum;} int main () {    int n;    int *num, I, J, temp;    while (scanf ("%d", &n)!=eof)    {        if (n==0)        return 0;        Num=malloc (sizeof (int) *n);        for (i=0; i<n; i++)        scanf ("%d", &num[i]);        For (I=1, i<n; i++) for        (j=0; j<n-i; j + +)        {            if (f (num[j]) >f (num[j+1]))            {                temp=num[j +1];                NUM[J+1]=NUM[J];                num[j]=temp;            }        }        for (i=0; i<n; i++)        {            if (i==0)            printf ("%d", Num[i]);            else            printf ("%d", Num[i]);        }            printf ("\ n");}    }


AC Diary--the sum of the numbers of 23--

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.