C++刷题五

(一)Sn=a+aa+aaa+…+aa…aaa(有n个a)之值,其中a是一个数字(1<=a<=9)。例如:2+22+222+2222+22222(a=2,n=5),a和n由键盘输入。
/* Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作者:陈丹妮
 * 完成日期:2015年 4 月 17 日
 * 版 本 号:v1.0
 */
#include <iostream>
using namespace std;
int main()
{
    int a,n,s=0;
    cin>>a>>n;
    int m=a;
    for(int i=0;i<n;i++)
    {
        s=s+m;
        m=m*10+a;
    }
    cout<<s<<endl;
    return 0;
}




(二)有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串。

(1)
#include <iostream>
using namespace std;
void strcpypos(char s2[],char s1[],int pos)
{
    for(int i=0,j=0;s1[i]!='\0';i++,j++,pos++)
    {
        s2[j]=s1[pos];
    }
    return;
}
int main()
{
    char s1[256],s2[256];
    int n,pos,i;
    cin>>n;
    cin.get();
    cin.getline(s1,n+1);
    cin>>pos;
    strcpypos(s2,s1,pos);
    cout<<s2<<endl;
    return 0;
}

(2)
#include <iostream>
using namespace std;
void strcpypos(char s2[], char s1[],int pos)
{
    int i=0,j=pos-1;
    while(s1 [i]!= '\0')
    {
        s2[i++]=s1[j++];
    }
}

int main()
{
    char s1[256],s2[256];
    int n,pos,i;
    cin>>n;
    cin.get();
    cin.getline(s1,n+1);
    cin>>pos;
    strcpypos(s2,s1,pos);
    cout<<s2<<endl;
    return 0;
}


(三)
Description
This is the first problem for test. Since all we know the ASCII code, your job is simple: Input numbers and output corresponding messages
Input
The input will contain a list of positive integers separated by whitespaces(spaces, newlines, TABs). Please process to the end of file (EOF). The integers will be no less than 32.
Output
Output the corresponding message. Note there is NOT a newline character in the end of output.

#include <iostream>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        cout<<(char)n;
    }
    return 0;
}


 

(四)将一个字符串str的内容颠倒过来,并输出。str的长度不超过100个字符。 
Input
输入包括一行。第一行输入的字符串。
Output
输出转换好的逆序字符串。 
Sample Input
I am a student

Sample Output
tneduts a ma I

(1)
#include<iostream>
#include<string>
using namespace std;

int main() {
  int i, l;
  string s;
  getline(cin,s);
  l=s.size();
  for(i=l-1; i>=0; i--)
    cout<<s[i];
}

(2)
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    char s[100];
    int i=0,n=0;
    gets(s);
    while(s[i]!='\0')
    {
        if(s[i]=='\0')
            break;
        i++;
    }
    n=i;
    for(int j=n-1;j>=0;j--)
    {
        cout<<s[j];
    }
    return 0;
}


学习心得:从规模很小的题中获取重要的知识,正如从平凡的生活中感悟经久不衰的真言!继续加油吧!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值