没有头绪,我写的代码怎么跟粑粑一样

P1307 [NOIP2011 普及组] 数字反转

Hayward @ 2022-12-04 20:01:31

#include<iostream>
using namespace std;
int main()
{
    long long n = 0;
    cin >> n;
    if (n >= 10 && n < 100)
    {
        int shi = n / 10;
        int ge = n % 10 * 10;
        cout << ge + shi;
    }
    if (n >= 100 && n < 1000)
    {
        long long bai = n / 100;
        int shi = n % 100 / 10 * 10;
        int ge = n % 10 * 100;
        int sum = bai + shi + ge;
        cout << sum;
    }
    if (n >= 1000 && n < 10000)
    {
        int qian = n / 1000;
        int bai = n % 1000 / 100;
        int shi = n % 1000 % 100 / 10;
        int ge = n % 1000 % 100 % 10;
        int sum = ge * 1000 + shi * 100 + bai * 10 + qian;
        cout << sum;
    }
    if (n >= 10000 && n < 100000)
    {
        int wan = n / 10000;
        int qian = n / 1000 % 10;
        int bai = n % 1000 / 100;
        int shi = n % 10000 % 1000 % 100 / 10;
        int ge = n % 1000 % 100 % 10;
        int sum = ge * 10000 + shi * 1000 + bai * 100 + qian * 10 + wan;
        cout << sum;
    }
    if (n >= 100000 && n < 1000000)
    {
        int shiwan = n / 10000 / 10;
        int wan = n / 10000 % 10;
        int qian = n / 1000 % 100 % 10;
        int bai = n % 1000 / 100;
        int shi = n % 10000 % 1000 % 100 / 10;
        int ge = n % 1000 % 100 % 10;
        int sum = ge * 100000 + shi * 10000 + bai * 1000 + qian * 100 + wan * 10 + shiwan;
        cout << sum;
    }
    if (n >= 1000000 && n < 10000000)
    {
        int baiwan = n / 100000 / 10;
        int shiwan = n % 1000000 / 100000;
        int wan = n / 10000 % 10;
        int qian = n / 1000 % 100 % 10;
        int bai = n % 1000 / 100;
        int shi = n % 10000 % 1000 % 100 / 10;
        int ge = n % 1000 % 100 % 10;
        int sum = ge * 1000000 + shi * 100000 + bai * 10000 + qian * 1000 + wan * 100 + shiwan*10+baiwan;
        cout << sum;
        //cout << ge << endl << shi << endl << bai << endl << qian << endl << wan << endl << shiwan << endl << baiwan;
    }
    if (n >= 10000000 && n < 100000000)
    {
        int qianwan = n / 1000000/10;
        int baiwan = n % 10000000 / 1000000;
        int shiwan = n % 1000000 / 100000;
        int wan = n / 10000 % 10;
        int qian = n / 1000 % 100 % 10;
        int bai = n % 1000 / 100;
        int shi = n % 10000 % 1000 % 100 / 10;
        int ge = n % 1000 % 100 % 10;
        int sum = ge * 10000000 + shi * 1000000 + bai * 100000 + qian * 10000 + wan * 1000 + shiwan * 100 + baiwan*10+qianwan;
        cout << sum;
        //cout << ge << endl << shi << endl << bai << endl << qian << endl << wan << endl << shiwan << endl << baiwan<<endl<<qianwan;
    }
    if (n >= 100000000 && n < 1000000000)
    {
        long long yi = n / 100000000;
        long long qianwan = n / 1000000 / 10%10;
        long long baiwan = n % 10000000 / 1000000;
        long long shiwan = n % 1000000 / 100000;
        long long wan = n / 10000 % 10;
        long long qian = n / 1000 % 100 % 10;
        long long  bai = n % 1000 / 100;
        long long shi = n % 10000 % 1000 % 100 / 10;
        long long  ge = n % 1000 % 100 % 10;
        long long  sum = ge * 100000000 + shi * 10000000 + bai * 1000000 + qian * 100000 + wan * 10000 + shiwan * 1000 + baiwan * 100 + qianwan*10+yi;
        cout << sum;
        //cout << ge << endl << shi << endl << bai << endl << qian << endl << wan << endl << shiwan << endl << baiwan<<endl<<qianwan<<endl<<yi;
    }
    if (n >= 1000000000 && n < 10000000000)
    {
        long long shiyi = n / 1000000000;
        long long yi = n / 100000000 %10;
        long long qianwan = n / 1000000 / 10 % 10;
        long long baiwan = n % 10000000 / 1000000;
        long long shiwan = n % 1000000 / 100000;
        long long wan = n / 10000 % 10;
        long long qian = n / 1000 % 100 % 10;
        long long  bai = n % 1000 / 100;
        long long shi = n % 10000 % 1000 % 100 / 10;
        long long  ge = n % 1000 % 100 % 10;
        long long  sum = ge * 1000000000 + shi * 100000000 + bai * 10000000 + qian * 1000000 + wan * 100000 + shiwan * 10000 + baiwan * 1000 + qianwan * 100 + yi*10+shiyi;
        cout << sum;
        //cout << ge << endl << shi << endl << bai << endl << qian << endl << wan << endl << shiwan << endl << baiwan<<endl<<qianwan<<endl<<yi<<endl<<shiyi;
    }
    if (n >= -10 && n < -100)
    {
        n = n * -1;
        int shi = n / 10;
        int ge = n % 10 * 10;
        int sum = ge + shi;
        cout << "-" << sum;
    }
    if (n >= -100 && n < -1000)
    {
        n = n * -1;
        long long bai = n / 100;
        int shi = n % 100 / 10 * 10;
        int ge = n % 10 * 100;
        int sum = bai + shi + ge;
        cout <<"-"<< sum;
    }
    if (n >= -1000 && n < -10000)
    {
        n = n * -1;
        int qian = n / 1000;
        int bai = n % 1000 / 100;
        int shi = n % 1000 % 100 / 10;
        int ge = n % 1000 % 100 % 10;
        int sum = ge * 1000 + shi * 100 + bai * 10 + qian;
        cout <<"-"<< sum;
    }
    if (n >= -10000 && n < -100000)
    {
        n = n * -1;
        int wan = n / 10000;
        int qian = n / 1000 % 10;
        int bai = n % 1000 / 100;
        int shi = n % 10000 % 1000 % 100 / 10;
        int ge = n % 1000 % 100 % 10;
        int sum = ge * 10000 + shi * 1000 + bai * 100 + qian * 10 + wan;
        cout << "-" << sum;
    }
    if (n >= -100000 && n < -1000000)
    {
        n = n * -1;
        int shiwan = n / 10000 / 10;
        int wan = n / 10000 % 10;
        int qian = n / 1000 % 100 % 10;
        int bai = n % 1000 / 100;
        int shi = n % 10000 % 1000 % 100 / 10;
        int ge = n % 1000 % 100 % 10;
        int sum = ge * 100000 + shi * 10000 + bai * 1000 + qian * 100 + wan * 10 + shiwan;
        cout << "-" << sum;
    }
    if (n >= -1000000 && n < -10000000)
    {
        n = n * -1;
        int baiwan = n / 100000 / 10;
        int shiwan = n % 1000000 / 100000;
        int wan = n / 10000 % 10;
        int qian = n / 1000 % 100 % 10;
        int bai = n % 1000 / 100;
        int shi = n % 10000 % 1000 % 100 / 10;
        int ge = n % 1000 % 100 % 10;
        int sum = ge * 1000000 + shi * 100000 + bai * 10000 + qian * 1000 + wan * 100 + shiwan * 10 + baiwan;
        cout <<"-"<< sum;
        //cout << ge << endl << shi << endl << bai << endl << qian << endl << wan << endl << shiwan << endl << baiwan;
    }
    if (n >= -10000000 && n < -100000000)
    {
        n = n * -1;
        int qianwan = n / 1000000 / 10;
        int baiwan = n % 10000000 / 1000000;
        int shiwan = n % 1000000 / 100000;
        int wan = n / 10000 % 10;
        int qian = n / 1000 % 100 % 10;
        int bai = n % 1000 / 100;
        int shi = n % 10000 % 1000 % 100 / 10;
        int ge = n % 1000 % 100 % 10;
        int sum = ge * 10000000 + shi * 1000000 + bai * 100000 + qian * 10000 + wan * 1000 + shiwan * 100 + baiwan * 10 + qianwan;
        cout << "-" << sum;
        //cout << ge << endl << shi << endl << bai << endl << qian << endl << wan << endl << shiwan << endl << baiwan<<endl<<qianwan;
    }
    if (n >= -100000000 && n < -1000000000)
    {
        n = n * -1;
        long long yi = n / 100000000;
        long long qianwan = n / 1000000 / 10 % 10;
        long long baiwan = n % 10000000 / 1000000;
        long long shiwan = n % 1000000 / 100000;
        long long wan = n / 10000 % 10;
        long long qian = n / 1000 % 100 % 10;
        long long  bai = n % 1000 / 100;
        long long shi = n % 10000 % 1000 % 100 / 10;
        long long  ge = n % 1000 % 100 % 10;
        long long  sum = ge * 100000000 + shi * 10000000 + bai * 1000000 + qian * 100000 + wan * 10000 + shiwan * 1000 + baiwan * 100 + qianwan * 10 + yi;
        cout <<"-" << sum;
        //cout << ge << endl << shi << endl << bai << endl << qian << endl << wan << endl << shiwan << endl << baiwan<<endl<<qianwan<<endl<<yi;
    }
    if (n >= -1000000000 && n < -10000000000)
    {
        n = n * -1;
        long long shiyi = n / 1000000000;
        long long yi = n / 100000000 % 10;
        long long qianwan = n / 1000000 / 10 % 10;
        long long baiwan = n % 10000000 / 1000000;
        long long shiwan = n % 1000000 / 100000;
        long long wan = n / 10000 % 10;
        long long qian = n / 1000 % 100 % 10;
        long long  bai = n % 1000 / 100;
        long long shi = n % 10000 % 1000 % 100 / 10;
        long long  ge = n % 1000 % 100 % 10;
        long long  sum = ge * 1000000000 + shi * 100000000 + bai * 10000000 + qian * 1000000 + wan * 100000 + shiwan * 10000 + baiwan * 1000 + qianwan * 100 + yi * 10 + shiyi;
        cout << "-" << sum;
        //cout << ge << endl << shi << endl << bai << endl << qian << endl << wan << endl << shiwan << endl << baiwan<<endl<<qianwan<<endl<<yi<<endl<<shiyi;
    }
}

by Katz @ 2022-12-04 20:03:18

尝试用循环分离N的每一位数,然后输出


by Hayward @ 2022-12-04 20:07:11

判断负数的if语句也有问题


by _colin1112_ @ 2022-12-04 20:08:38

啊这......这么一道简单的题竟然能被你搞得这么复杂......具体代码如下:

#include <iostream>
using namespace std;
int main(){
    int n;
    int s=0;
    cin >>n;
    while(n){
        s=s*10+n%10;
        n/=10;
    }
    cout <<s;
    return 0;
}

by _colin1112_ @ 2022-12-04 20:12:09

这也写的太简单了吧


by Hayward @ 2022-12-04 20:18:00

@PMAC 有头绪了谢谢


by Hayward @ 2022-12-04 20:20:10

@colin1112 相比之下,我的代码太烂了


by VitrelosTia @ 2022-12-04 20:27:14

@colin1112 你觉得你这样对于lz有帮助吗?


by _colin1112_ @ 2022-12-04 20:28:20

@VT_SODC3DC3BSLF ?没听懂


by _colin1112_ @ 2022-12-04 20:29:05

@VT_SODC3DC3BSLF 毕竟我前两天刚注册


by VitrelosTia @ 2022-12-04 20:29:30

@colin1112 你给楼主(提问的人)一个代码,然后这对他有什么帮助吗?


| 下一页