首页 > 试题广场 >

整型存储

[编程题]整型存储
  • 热度指数:5314 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

写一个类,能接受int 型的变量,接收变量后能存储原变量(譬如12345)和其反向变量(54321),最多处理数量为10 个,当输入达到10 个或者输入变量为0 的时候停止。并且在类销毁前输出存储的所有变量。


输入描述:
输入若干个整数。


输出描述:
按类里存储的数个数m输出m行。每行两个数,表示原变量及其反向变量。
示例1

输入

12 3442 0

输出

12 21
3442 2443

备注:
注意反向后可能会有前导0
#include<stdio.h>
int main()
{
    char s[100];int i,key,m=10;
    while(m--)//最多10个
    {
        scanf("%s",s);
        if(s[0]=='0') break;
        printf("%s ",s);
        for(i=strlen(s)-1;i>=0;i--)
            if(s[i]!='0')
            {key=i;break;}//找到第一个不为0的位置
        for(i=key;i>=0;i--)//倒序输出
            printf("%c",s[i]);
        printf("\n");
    }
}

发表于 2020-05-10 12:35:51 回复(0)
#include <stdio.h>

int main()
{
    int n,i=0,r;
    while(scanf("%d",&n)!=EOF&&n!=0&&i<10)
    {
        printf("%d ",n);
        r=0;
        while(n>0)
        {
            r*=10;
            r+=n%10;
            n/=10;
        }
        printf("%d\n",r);
        i++;
    }
    return 0;
}

发表于 2021-03-13 11:53:29 回复(0)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<queue>
#include<iostream>
#include<string>
#include<limits.h>

using namespace std;

class Reverse{
    public:
        int x;
        int y;
        Reverse(){}
        Reverse(int x){
            this -> x = x;
            this -> y = getRev();
        }
    private:
        int getRev(){
            int t = x, r, q, tem = 0;
            while(t){
                r = t % 10;
                q = t / 10;
                t = q;
                tem = (tem + r) * 10;
            }
            return tem / 10;
        }
};

int main(){
    int c = 0, m;
    while((cin >> m) && (m != 0) && (c != 10)){
        c++;
        Reverse r(m);
        cout << r.x << " " << r.y << endl;
    }
    return 0;
}

发表于 2019-08-17 10:37:16 回复(0)
#include <iostream>
using namespace std;

class processInt{
    public:
        processInt(){
            i = 0;
        }

        /*
        ~processInt(){
            for(int j = 0;j<i;j++){
                cout << origin[j] << " " << storage[j] << endl;
            }
        }
        */

        void store(int v){
            origin[i] = v;
            storage[i] = negateInt(v);
            i++;
        }

        void output(){
            for(int j = 0;j<i;j++){
                cout << origin[j] << " " << storage[j] << endl;
            }
        }
        int i;
        int storage[10];
        int origin[10];

    private:
        int negateInt(int x){
            int newV = 0;
            while(x){
                newV = newV * 10 + x%10;
                x /= 10;
            }
            return newV;
        }
};

int main(){
    int x;
    int count = 0;
    processInt process;
    while(cin >> x){
        if(x==0){
            process.output();
            break;
        }
        process.store(x);
        count ++;
        if(count == 10){
            process.output();
            break;
        }
    }
    return 0;
}

嘻嘻

发表于 2019-04-29 13:31:57 回复(0)
只是能通过检查点,但没有按要求实现,大家不要学我(*/ω\*)
#include<iostream>
using namespace std;
int main(){
    int count=0;
    int n,a[10];
    while(cin>>n){
        if(count==10||n==0)
            break;
        a[count++]=n;
    }
    for(int i=0;i<count;i++){
        cout<<a[i]<<" ";
        int temp=a[i],reverse=0;
        while(temp>0){
            reverse=reverse*10+temp%10;
            temp/=10;
        }
        cout<<reverse<<endl;
    }
}

发表于 2019-02-25 13:28:53 回复(0)
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<functional>
#include<bitset>
#include<fstream>
#include<sstream>
using namespace std;

class Store
{
public:;
    vector<string> num;
    vector<string> rev;

    ~Store() {
        num.clear();
        rev.clear();
    }

    void store(string s) {
        num.push_back(s);
        string t;
        bool first_digit = false;
        for(int i = s.size() - 1; i >= 0; i--) {
            if (s[i] != '0')
                first_digit = true;
            if (first_digit)
                t += s[i];
        }
        rev.push_back(t);
    }
};

int main()
{
    Store str;
    int count = 0;
    string s;
        while (cin >> s) {
            if (s == "0")
                break;
            str.store(s);
            count++;
            if (count == 10)
                break;
    }
    for (int i = 0; i != count; i++)
        cout << str.num[i] << ' ' << str.rev[i] << endl;
    return 0;
}
编辑于 2019-03-14 16:54:22 回复(0)
#include<iostream>
#include<stack>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;


int main(){
	string str;
	int count=10;
	while(cin>>str&&count!=0){
		if(str=="0"){
			break;
		}
		cout<<str<<" ";
		int len=str.size();
		reverse(str.begin(),str.end());
		while(str[0]=='0'){
			str=str.substr(1);
		}
		cout<<str<<endl;
		count--;
	}
} 

编辑于 2024-03-23 22:38:53 回复(0)
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
typedef struct Num{
    string num;
    string rnum;
}Num;
int main(){
    string str;
    int count=0;
    while(cin>>str){
        count++;
        if(count>10) break;
        if(str=="0"){
            break;
        }
        cout<<str;
        for(int i=str.size()-1;i>=0;i--){
            if(str[i]=='0') str.erase(i,1);
        }
        reverse(str.begin(),str.end());
        cout<<" "<<str<<endl;
        //vec.push_back(n1);
    }
}

发表于 2024-03-18 21:34:40 回复(0)
import sys

class Aim(object):
    def __init__(self) -> None:
        self.ll = []
    def addNum(self, aa: int):
        self.ll.append(aa)
    def print(self):
        volume = min(10, len(self.ll))
        for l in self.ll[: volume]:
            result = ""
            aa = str(l)[: :-1]
            pos = 0
            for a in aa:
                if a != "0": break
                pos += 1
            aa = aa[pos:]
            result = str(l) + " " + str(aa)
            print(result)

aim = Aim()
while True:
    try:
        a = list(map(int, input().split()))
        # print(a)
        for ss in a:
            if ss != 0:aim.addNum(ss)
    except Exception:
        aim.print()
        break

编辑于 2024-03-11 14:05:51 回复(0)
#include <iostream>
#include <vector>
using namespace std;

class newInt{
public:
    newInt(){
        this->len = 0;
    }
    void fun(){
        int num;
        while (cin >> num) {
            this->len++;
            if(num == 0 || this->len > 10){
                break;
            }
            this->my1.push_back(num);
            int reverse = 0,remain;
            while(num > 0){
                remain = num % 10;
                reverse = reverse * 10 + remain;
                num /= 10;
            }
            this->my2.push_back(reverse);
        }
    }
    ~newInt(){
        for(int i = 0;i < this->my1.size();++i){
            cout << this->my1[i] << " " << this->my2[i] << endl;
        }
    }
    vector<int> my1;
    vector<int> my2;
    int len;
};

int main() {
    newInt n;
    n.fun();

    return 0;
}
// 64 位输出请用 printf("%lld")

发表于 2023-03-29 17:20:09 回复(0)
没有写类,直接用Reverse()函数求逆序数
#include <cstdio>

int Reverse(int n){
    int reverse = 0,remain;
    while(n != 0){
        remain = n%10;
        n /= 10;
        reverse = reverse*10 + remain;
    }
    return reverse;
}

int main(){
    int n, num = 0;
    while(scanf("%d",&n) != EOF){
        num ++;
        if(n==0 || num>10)  break;
        printf("%d %d\n",n,Reverse(n));
    }
    return 0;
}


发表于 2023-03-23 16:38:37 回复(0)
100翻转过来是1,不是001,本来想投机取巧一下,结果玩脱了hhhhh,但是提供一下这个思路
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

class rev{

    public:
    int rongliang;
    string str[10];

    rev(int x){
        this->rongliang = x;
    }

};


int main() {
    
    rev a(0);
    while(1){

        if(a.rongliang == 10){
            break;
        }

        cin >> a.str[a.rongliang];
        if(a.str[a.rongliang] == "0"){
            break;
        }



        cout << a.str[a.rongliang] << " ";
        reverse(a.str[a.rongliang].begin(),a.str[a.rongliang].end());
        cout << a.str[a.rongliang] << endl;
        


        a.rongliang++;
    }



    
}


发表于 2023-03-18 11:15:11 回复(0)
其实我就一个疑问,这种要求使用类的题目,它是怎么检测我又没有用类的呢?复试上机的话不用类来做能给分吗?
#include <cstdio>
#include <iostream>
using namespace std;

int reverse(int n){
    int result=0;
    while(n!=0){
        result*=10;
        result+=n%10;
        n/=10;
    }
    return result;
}

class obj{
    private:
    int len;
    int origin[10];
    int reorigin[10];
    public:
    obj(){
        int m=10;
        len=0;
        for(int i=0,temp;i<m;i++){
            scanf("%d",&temp);
            if(temp==0){
                break;
            }
            origin[i]=temp;
            reorigin[i]=reverse(temp);
            ++len;
        }
    }
    ~obj(){
        for(int i=0;i<len;i++){
            cout<<origin[i]<<" "<<reorigin[i]<<endl;
        }
    }
};

int main() {
    obj o1;
}


发表于 2023-02-25 22:00:48 回复(0)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    int c=0;
    string s;
    while(cin>>s && s!="0")
    {
        string s1;
        c++;
        cout << s << " ";
        for(int i=s.size()-1;i>0;i--)
        {
            if(s[i]=='0') continue;
            else s1+=s[i];
        }
        s1+=s[0];             
        cout << s1 << endl;
        if(c==10) break;
    }
    return 0;
}

发表于 2021-03-31 16:26:41 回复(0)
最多输入10组测试样例。。。
发表于 2020-09-18 16:13:57 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 10; i++){
            int num = sc.nextInt();
            if (num == 0){
                break;
            } else {
                System.out.println(num + " " + rev(num));
            }
        }
        sc.close();
    }
    static String rev(int num){
        String str = "";
        String s1 = String.valueOf(num);
        int len = s1.length();
        int i;
        for (i = len-1; i >=0; i--){
            if (s1.charAt(i) != '0'){
                break;
            }
        }
        for (int j = i; j >= 0; j--){
            str += s1.charAt(j);
        }
        return str;
    }
}

发表于 2020-05-09 09:19:28 回复(0)
很简单的题目
#include<iostream>
using namespace std;
class Cls{
    private:
    int a[10];
    int index=0;
    public:
    bool place(int x){
        if(index==10 || x==0)
            return false;
        a[index++]=x;
        return true;
    }
    int len(){
        return index;
    }
    int get_i(int i){
        return a[i];
    }
};
int main(){
    Cls cls;
    int x;
    while(scanf("%d",&x)!=EOF){
        if(!cls.place(x))
            break;
    }
    for(int i=0;i<cls.len();i++){
        int t=cls.get_i(i);
        cout<<t<<" ";
        int k=0;
        while(t!=0){
            k=10*k+t%10;
            t=t/10;
        }
        cout<<k<<endl;
    }
    return 0;
}


发表于 2020-04-03 10:06:45 回复(0)
#include <stdio.h>
#
include <string.h>
int main() {
    char str[1010], i = 0;
    while(scanf("%s", str)!=EOF) {
        i++;
        int len = strlen(str);
        if(len == 1 && str[0] == '0' || i == 10) break;
        for(int i = 0; i < len; ++i)
            printf("%c", str[i]);
        printf(" ");
        while(str[len - 1] == '0' && len > 1) len--;
        for(int i = len - 1; i >= 0; --i)
            printf("%c", str[i]);
        printf("\n");
    }    
    return 0;
}


为什么通过率90%???搞不明白啊
发表于 2020-03-12 00:18:54 回复(0)
#include<iostream>
(720)#include<string>
using namespace std;
class IntDispose{
    private:
    int ori[10] = {0};
    int rev[10] = {0};
    int process(int a){
        int b=0;
        int quotient,remainder;
        while(a!=0){
        	b *= 10;
            quotient = a/10;
            remainder = a%10;
            b += remainder;
            a = quotient;
        }
        return b;
    }
    public:
    IntDispose(int a[]){
        for (int i=0;a[i]!=0&&i<10;i++){
            ori[i] = a[i];
            rev[i] = process(ori[i]);
        }
    }
    ~IntDispose(){
    	for(int i=0;ori[i]!=0&&i<10;i++){
    		cout<<ori[i]<<" "<<rev[i]<<endl;
    	}
    }
};
int main(){
    int x,i=0;
    int a[10]={0};
    while(cin>>x){
        if(x==0)break;
        a[i] = x;
        if(i>9)break;
        char c = getchar();
        i++;
    }
    IntDispose In = IntDispose(a);
    return 0;
}

发表于 2020-03-02 22:09:05 回复(0)
请问这里是测试点的问题吗?为什么大家的代码可以通过这个测试点呢?
发表于 2019-08-19 21:45:19 回复(1)

问题信息

上传者:小小
难度:
40条回答 3501浏览

热门推荐

通过挑战的用户

查看代码