頁:
[1]
期末考題檢討
本帖最後由 zz2242zz 於 2017-1-5 07:26 PM 編輯這次考試我又有一題不會做,想請教各位。
印象中題目是這樣的:設計出一支程式,能反覆輸入一整數值,直到輸入的值為-1或999則停止,並顯示該值有哪些數字重複出現了幾次。
範例:
輸入一整數值:221330
2重複出現2次
3重複出現2次 while(1)
{
cout<<"輸入一個整數值:" ;
cin>>n ;
if(n==-1||n==999)
{
cout<<"第4小題結束!!!" ;
break ;
}
if(n<0)
n*=-1 ;
m=1 ;
y=1 ;
while(1)
{
m*=10 ;
x=n/m ;
if(x==0)
{
break ;
}
y++ ;
}
}這是考完後想出來的,先找出n是幾位數,再來處理有幾個數字重複出現,但這樣好像要宣告很多變數......。
這學期學到的東西:
cout、cin、for、while、do...while、/* */、if、if...else、break、continue、sqrt()
=、%、加減乘除跟括號、算數指定運算子、關係運算子、setw()、巢狀迴圈(跟if)、邏輯運算子
如果可以請盡量用我學過的東西......
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div> 老實說~看完您的題目我嘗試寫了一下~之後看您的程式碼...貌似沒甚麼兩樣....只是您的y是怎麼回事?
因為您沒提到您是否有學到陣列....所以我給了兩版...另外一版用switch....雖然您也沒說您是否有學過switch...只是...因為我實在不想瘋狂的打else if...上網去看一下switch和if else if就知道關聯與差別了
陣列版...如下int main()
{
int sum_buf = { } ; //初始化而沒給值則為0...至少我在VC++上是如此~看不慣則用for迴圈歸0~將來懂memset這函式時就不用靠for了
//另外備註~或許您已經看過有人是寫 { 0 }...其實他們也僅僅只是定義了陣列第一個空間值為0...後面依舊沒去定義
int input;
int ans;
while(true)
{
cout << "輸入一個整數值:" ;
cin >> input ;
if( input==-1 || input==999 )
{
cout<<"收工!下班\n";
break;
}
if( input < 0 )
{
input *= -1;
}
do
{
ans = input % 10;
sum_buf[ ans ] ++;
input /= 10;
}while( input != 0 );
for( int i = 0 ; i < 10 ; i++ )
{
cout<< i << " 出現了 " << sum_buf[ i ] << " 次\n" ;
sum_buf = 0; //順便做清空的動作
}
}
}switch版int main()
{
int a0 = 0 , a1 = 0 , a2 = 0 , a3 = 0 , a4 = 0 , a5 = 0 , a6 = 0 , a7 = 0 , a8 = 0 , a9 = 0 ;
int input;
while(true)
{
cout << "輸入一個整數值:" ;
cin >> input ;
if( input==-1 || input==999 )
{
cout<<"收工!下班\n";
break;
}
if( input < 0 )
{
input *= -1;
}
do
{
switch( input % 10 )
{
case 0:
a0++;
break;
case 1:
a1++;
break;
case 2:
a2++;
break;
case 3:
a3++;
break;
case 4:
a4++;
break;
case 5:
a5++;
break;
case 6:
a6++;
break;
case 7:
a7++;
break;
case 8:
a8++;
break;
case 9:
a9++;
break;
}
input /= 10;
}while( input != 0 );
for( int i = 0 ; i < 10 ; i++ )
{
cout << i << " 出現了 " ;
switch( i )
{
case 0:
cout << a0;
a0 = 0 ;
break;
case 1:
cout << a1;
a1 = 0 ;
break;
case 2:
cout << a2;
a2 = 0 ;
break;
case 3:
cout << a3;
a3 = 0 ;
break;
case 4:
cout << a4;
a4 = 0 ;
break;
case 5:
cout << a5;
a5 = 0 ;
break;
case 6:
cout << a6;
a6 = 0 ;
break;
case 7:
cout << a7;
a7 = 0 ;
break;
case 8:
cout << a8;
a8 = 0 ;
break;
case 9:
cout << a9;
a9 = 0 ;
break;
}
cout << " 次\n" ;
}
}
}若是不想看到0次的數值就用if讓其不顯示吧
至於有否比您我想到更高竿的寫法~那肯定有~只是沒興趣在此墨跡...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div> 如果不考慮 0 開頭的數字,像是 00123 的話
#include <iostream>
using namespace std;
int main()
{
int n;
while(cin >> n && n != -1 && n!= 999)
{
for(int i = 0 ; i < 10 ; ++i)
{
int m = n, cnt = 0;
while(m != 0)
{
cnt += (m % 10) == i;
m /= 10;
}
if(cnt > 1)
cout << i << "重複出現" << cnt << "次\n";
}
}
return 0;
}
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div> 本帖最後由 kiwis 於 2017-1-9 09:53 AM 編輯
直接用string來處理,加上一個紀錄用的 buffer 就可以很簡單的解決這個問題。#include <iostream>
#include <string>
using namespace std;
void PrintResult(string& input)
{
char* pInput = (char*) input.c_str();
unsigned char counter; //紀錄 0~9 的出現次數,陣列的 index 就是數字
memset(counter, 0, 10); //一開始所有數字的出現次數為 0
for(int i(0); i<input.size(); ++i)
{
counter[ (pInput-48) ] ++; //ASCII code - 48 = 數字,字串基本上是ASCII編碼
}
for(int j(0); j<10; ++j)
{
if( counter )
{
cout << "number: " << j << " repeated " << +counter << " time(s)." << endl;
}
}
}
int main(int argc, char* argv[])
{
string str;
cin >> str;
while( strcmp("-1", str.c_str()) && strcmp("999", str.c_str()))
{
PrintResult(str);
cin >> str;
}
cout << "Terminated." << endl;
return 0;
}...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div> cnt += 0 或 cnt += 1 應該知道吧
boolean 做為整數運算時,false 為 0,true 為 1
(m % 10) == i 判斷末位數字是不是 i
是的話 cnt 值加 1,不是加 0
可以寫成
if(m%10 == i)
cnt++;
<br><br><br><br><br><div></div>
頁:
[1]