C++刷题五

来源:互联网 发布:淘宝显示历史价格插件 编辑:程序博客网 时间:2024/04/30 05:01
(一)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 0
原创粉丝点击