導航:首頁 > 編程語言 > 怎麼用編程寫逆轉數

怎麼用編程寫逆轉數

發布時間:2022-03-01 03:09:09

⑴ C++編程 編寫字元串反轉函數mystrrev

應用C++的string類對象實現。為體現一般性,對象中就允許空格出現;自定義逆序函數形參應使用引用類型,以便永久性改變對實參對象的操作。舉例代碼如下:

//#include"stdafx.h"//Ifthevc++6.0,withthisline.
#include<string>
#include<iostream>
usingnamespacestd;
voidmystrrev(string&str){//引用形參,以改變實參
for(intj=str.length()-1,i=0;i<j;i++,j--){
chart=str[i];
str[i]=str[j],str[j]=t;
}
}
intmain(intargc,char*argv[]){
strings;
charch;
cout<<"Inputastring... s=";
while((ch=cin.get())!=' ')//輸入可有空格
s+=ch;
cout<<"Theoriginalstring:"<<s<<endl;//逆序前
mystrrev(s);//調用自定義逆序函數
cout<<"Afterreverseorder:"<<s<<endl;//逆序後
return0;
}

運行結果舉例:

⑵ 輸入一個整數,講各位數字反轉後輸出,如何用C語言編寫

#include <stdio.h>
#include <conio.h>
int main()
{
int former,latter=0;
printf("請輸入需要反轉的整數:");
scanf("%d",&former);
do
{
latter*=10;
latter+=former%10;
former/=10;
}
while (former);
printf("反轉後整數為:%d",latter);
getch();
}二樓的方法是從低到高獲取每一位數字逐個輸出,而我的這種方法是計算出反轉之後的數據,然後再輸出。

⑶ 編寫程序,輸入一個正整數,將它各位數反轉輸出

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleAppOputNum
{
class Program
{
static void Main(string[] args)
{
bool IfRight = true;
do
{
Console.WriteLine("請輸入要轉化的正整數:");
int value = 0;
string str_num = Console.ReadLine();
if (Int32.TryParse(str_num, out value))
{
if (value > 0)
{
Console.WriteLine("轉化後的數字是{0}。",ChangeNum(value));
Console.WriteLine("繼續輸入請輸入0,退出請輸入其他任意鍵。");
string str_result = Console.ReadLine();
if (str_result.Equals("0"))
{
IfRight = false;
}
else
{
return;
}
}
else
{
Console.WriteLine("輸入的不是正數,請重新輸入!");
IfRight = false;
}
}
else
{
Console.WriteLine("輸入的不是數字,請重新輸入!");
IfRight = false;
}
}
while (!IfRight);
}

private static string ChangeNum(int num)
{
string str_old = num.ToString();
StringBuilder sb = new StringBuilder();
for (int index = str_old.Length - 1; index >= 0; index--)
{
sb.Append(str_old[index]);
}
return sb.ToString();
}
}
}

⑷ C語言:數字反轉怎麼編程

http://..com/question/625042014382516724
剛才回答問題的時候,發現這個程序有反轉,可以參考一下。

⑸ 編寫程序,輸入一個3位整數,反轉後輸出。 求指教

反轉後輸出並不是指讓你把原來的數通過四則運算變成反過來的數 輸出,如果你想這樣變除非是很特殊的數字否則很難通過四則運算打到你的要求,,除非用數組

反過來輸出是讓你把個位數的數字放到百位 依次類推的輸出就可以了 ,你把每一位的數字求出來,倒著輸一遍就達到這個目的了

⑹ 怎麼編寫一個函數將一個整數反轉為一個新的整數

#include<stdio.h>
int main()
{
int reverse(int n);
int n;
printf("輸入一個整數:");
scanf("%d",&n);
printf("反轉後的整數:%d\n",reverse(n));
return 0;
}

int reverse(int n)
{
int sign=1,m;
if(n<0) sign=-1;
n=(n>=0?n:-n); //取n的絕對值
m=n%10; //取出n的個位數;
while(n>=10)
{
n=n/10; //n的小數點左移一位
m=m*10+n%10; //m的小數點右移一位,並加上當前n的個位數
}
return sign*m;
}

⑺ 編寫程序,輸入一個三位數,把它的個位、百位、十位逆轉後輸出。例如,輸入123,轉換為321輸出

如果是用C語言寫,如果樓主懂數組的話 那個叫「3/4」的答案就可以
如果不用數組的話再給你個 簡單的
#include<stdio.h>
void main()
{
int a,b,c,n;
scanf("%d",&n);
a=n/100;
b=n%100/10;
c=n%10;
printf("%d%d%d\n",c,b,a);
}

⑻ 編寫程序實現字元串的逆轉功能,並統計字元串中大寫字母和小寫字母的個數。

msgbox mystrreverse_Aa(inputbox("","","abCDeFGhizk"))

function mystrreverse_Aa(text)
d=0
for i=len(text) to 1 step -1
n=mid(text,i,1)
if asc(n)>=65 and asc(n)<=90 then d=d+1
mystrreverse_Aa=mystrreverse_Aa & n
next
x=len(text)-d
mystrreverse_Aa=mystrreverse_Aa &vbcrlf& "大寫字母:" &d&"個" &vbcrlf& "小寫字母:" &x&"個"
end function
'VBS寫的

⑼ 3.編寫程序。從鍵盤輸入一個三位正整數,輸出其逆轉數。

#include<stdio.h>
void main()
{
int a,c;
printf("請輸入一個3位正整數a:");
scanf("%d",&a);
c=a%10*100+(a/10)%10*10+a/100;
printf("三位正整數a的逆序數是:%d\n",c);
}

⑽ C語言編程單詞逆轉

#include <stdio.h>
#include <stdlib.h>

void
reverse_words(char * words)
{
char * h, * t; /* head and tail pointer to the string */

if ( *words != '\n' ) { /* '\n' signals the end of the string */
for(h = words; *words != ' ' && *words != '\n'; words++) ; /* traverse the string word by word */
for(t = words-1; t >= h; t-- ) /* print a word backwards */
putchar( *t );
putchar(' '); /* print a blank space */
if ( *words != '\n' ) /* if string doesn`t end, go on processing the rest part of the string */
reverse_words(words+1);
}
}

int
main(void)
{
char * words;
words = (char *) malloc(1024*sizeof(*words)); /* allocate a space to store the input string */

fgets(words, 1024, stdin); /* get the string */
reverse_words(words); /* do your job */
putchar('\n');

return 0;
}

樓主,要學會運用遞歸的思想思考問題。字元串的結構本身就是遞歸的,所以我們在處理字元串的時候要習慣遞歸思想。遞歸是非常有用的一種思考方式,你應該慢慢習慣學習並且運用它來解決諸如此類的問題。你如果看不懂的話,盡管發消息問我。
回答者:day9981 - 秀才 三級 12-18 10:16
修改答復: day9981,您要修改的答復如下: 積分規則 關閉
#include <stdio.h>
#include <stdlib.h>

void
reverse_words(char * words)
{
char * h, * t; /* head and tail pointer to the string */

if ( *words != '\n' ) { /* '\n' signals the end of the string */
for(h = words; *words != ' ' && *words != '\n'; words++) ; /* traverse the string word by word */
for(t = words-1; t >= h; t-- ) /* print a word backwards */
putchar( *t );
putchar(' '); /* print a blank space */
if ( *words != '\n' ) /* if string doesn`t end, go on processing the rest part of the string */
reverse_words(words+1);
}
}

int
main(void)
{
char * words;
words = (char *) malloc(1024*sizeof(*words)); /* allocate a space to store the input string */

fgets(words, 1024, stdin); /* get the string */
reverse_words(words); /* do your job */
putchar('\n');

return 0;
}

樓主,我得程序是用遞歸寫的,希望你也要學會運用遞歸的思想思考問題。字元串的結構本身就是遞歸的(你拿了一個字元,一部分字元串之後,剩下的部分還是字元串),所以我們在處理字元串的時候要習慣遞歸思想。遞歸是非常有用的一種思考方式,你應該慢慢習慣學習並且運用它來解決諸如此類的問題。你如果看不懂的話,盡管發消息問我。

閱讀全文

與怎麼用編程寫逆轉數相關的資料

熱點內容
優信二手車解壓後過戶 瀏覽:63
Windows常用c編譯器 瀏覽:780
關於改善國家網路安全的行政命令 瀏覽:835
安卓如何下載網易荒野pc服 瀏覽:656
javainetaddress 瀏覽:106
蘋果4s固件下載完了怎麼解壓 瀏覽:1005
命令zpa 瀏覽:288
python編譯器小程序 瀏覽:946
在app上看視頻怎麼光線調暗 瀏覽:542
可以中文解壓的解壓軟體 瀏覽:595
安卓卸載組件應用怎麼安裝 瀏覽:915
使用面向對象編程的方式 瀏覽:342
程序員項目經理的年終總結範文 瀏覽:932
內衣的加密設計用來幹嘛的 瀏覽:435
淮安數據加密 瀏覽:295
魔高一丈指標源碼 瀏覽:984
松下php研究所 瀏覽:171
c回調java 瀏覽:403
夢幻端游長安地圖互通源碼 瀏覽:747
電腦本地文件如何上傳伺服器 瀏覽:315